Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

URGENT:  Simple STL Question

Posted on 2003-10-24
4
Medium Priority
?
289 Views
Last Modified: 2013-12-14
I have no idea how to use STL.  So, I'm looking for a basic tutorial if anyone can point me to one, that would be great.  

In particular, I wish to print out the following list<string> that I have, one at a time:  

What is i  in this for loop?  

 for (list<string>::const_iterator i=profiles.begin(); i!=profiles.end(); i++)
 {
          //I want to get pointers to normal char* in here for each profile name that is profiles.
 }
     
I have a routine DebugBox(LPSTR str) that prints out the debug, how can I get a LPSTR out of the string?
0
Comment
Question by:jjacksn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 19

Accepted Solution

by:
Dexstar earned 1000 total points
ID: 9618308
jjacksn:

> What is i  in this for loop?  

i is an iterator for your list.  It points to one element in the list.  You can reference that element like a pointer:
     (*i) will be just like a string.

Try this:

     DebugBox( (*i).c_str() );

Or preferably:

     DebugBox( i->c_str() );

It might give you grief because c_str() returns a const char*, and your function just wants a char*.  If you can just DebugBox( LPSTR str ) to be DebugBox( LPCSTR str ), those issues should go away.


Hope that helps,
Dex*
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 9618309
    s.c_str()
returns an LPCSTR of the string data in s.

The i is an iterator.  
It is just one of those things that make using STL more complicated than necessary.  It makes sense is some complicated collection classes (doubley-linked lists, maps, etc), but in getting items from a list, it's just a headache (obviously a simple int index would do the job just fine).

-- Dan
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9618328
DanRollins:

> It makes sense is some complicated collection classes (doubley-linked lists,
> maps, etc), but in getting items from a list, it's just a headache (obviously
> a simple int index would do the job just fine).

Actually, what you said is true for arrays (vectors, in STL).  But a list is where you need iterators the most.  In a linked list, if you just reference each element by index, performance will be unacceptable because you need the previous element to find the next one.  So, if you want #10, then you have to start at 1, go to 2, etc., until you get to 10.  Then, if you want 11, you'd have to START over and get #1, and then #2, etc.

With an iterator, you can instantly jump to the next record.

Dex*
0
 
LVL 30

Expert Comment

by:Axter
ID: 9619179
If DebugBox is trying to modify the string, then you should not try to pass the contents of a std::string to DebugBox.

If DebugBox does not try to modify the string, then you should change it to accept a constant variable, and moreover, you should always make your variable arguments constant when the variable is not modified within the function.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question