Solved

URGENT:  Simple STL Question

Posted on 2003-10-24
4
278 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
  • 2
4 Comments
 
LVL 19

Accepted Solution

by:
Dexstar earned 250 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now