Solved

URGENT:  Simple STL Question

Posted on 2003-10-24
4
284 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

829 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