Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

URGENT:  Simple STL Question

Posted on 2003-10-24
4
Medium Priority
?
291 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 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 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.

926 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