Solved

URGENT:  Simple STL Question

Posted on 2003-10-24
4
287 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 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

622 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