Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

alnite

need help with std::list of std::string

This topic is 5739 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


  
std::list< std::string > lstFileList;

// in a loop

char filename[256];
lstFileList.push_back( filename );   // VS.NET IntelliSense does not respond here


// in another loop

std::list< std::string >::iterator p;
for ( p = lstFileList.begin() ; p != lstFileList.end() ; p++ )
{
   (*p).data();   // IntelliSense also does not respond, and exception is throw here

}
  
I am using VS.NET. Any help please??
Current project: 2D in Direct3D engine.
% completed: ~35%
Status: Active.

Share this post


Link to post
Share on other sites
Advertisement
Your code looks right, are you doing anything else to the list in the mean time ?

How about if you write your second loop so :

    
std::list< std::string >::iterator p;
std::list< std::string >::iterator last = lstFileList.end();
for ( p = lstFileList.begin() ; p != last ; p++ )
{
p->data();
}


What if you use a vector ? A deque ?

Using a typedef for std::list< std::string > can be handy. It lets you change to other containers easily - especially if you don't indicate the exact type of the container in the typedefed alias... Hungarian notation sucks

Unrelated : Why use a hungarian notation 'lst' prefix when the name 'FileList' itself implies you have a list ? That's one more reason for me to hate hungarian notation...


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]


[edited by - Fruny on April 1, 2003 9:44:42 PM]

Share this post


Link to post
Share on other sites
wait, just realized, it is not where the problem. it lies somewhere else. thanks anyway


Current project: 2D in Direct3D engine.
% completed: ~35%
Status: Active.

Share this post


Link to post
Share on other sites
quote:
Original post by rypyr
Always preincrement your iterator for-loop variables...


why? it''s a very small optimisation except over a very big loop and the compiler should optimise it anyway.


"That''s not a bug, it''s a feature!"
--me

Share this post


Link to post
Share on other sites
quote:
Original post by ChaosEngine
Original post by rypyr
Always preincrement your iterator for-loop variables...

why? it''s a very small optimisation except over a very big loop and the compiler should optimise it anyway.



If the iterator is anything other than an integer the compiler probably won''t optomise it. Iterators can be classes with overloaded increment and decrement operators. The compiler is unlikely to know it can optimise them. You''re in charge, why not tell the compiler what you really mean? ++p

Share this post


Link to post
Share on other sites
The iterator is really only "just a pointer" for the most simplistic of containers, such as vectors, which use contiguous memory regions. However for containers such as lists and maps, which are more complicated, the iterator is a full blown object that relies on many protected/private container member functions to know what to do. How else is ++p supposed to traverse something as complex as a tree?

Share this post


Link to post
Share on other sites
quote:
Original post by petewood
If the iterator is anything other than an integer the compiler probably won''t optomise it.
Argh petewood! You should know by now that there''s no need to "optimize" integer post-increment in any way. It''s as fast as pre-increment by definition.

Share this post


Link to post
Share on other sites
To make a copy of iterators in STL could be an expensive operation in some STL implementations.

SGI STL std::rope for example:

quote:

"Iterators are on the order of a dozen words in size. This means that copying them, though not tremendously expensive, is not a trivial operation. Avoid postincrementing iterators; use preincrement whenever possible."



A simple rule of thumb is to always use preincrement whenever possible, and only use postincrement if you really need it.



Update GameDev.net system time campaign - success at last

Share this post


Link to post
Share on other sites
In my case, where the iterator is a class, do you think there''s a difference between preincrement and postincrement operators? The operators are overloaded, so the speed depends on how the class overload these operators.


Current project: 2D in Direct3D engine.
% completed: ~35%
Status: Active.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!