std::list<std::string>
for the life of me i can not figure out how to do this. i have a string, say "foo = 1", and another string in a list, say "foo = 2". i want to add the "foo = 1" std::string to the list, but if there already a string that specifies what foo is equal too, i want to remove the old string.
You would have to iterate through each node in the list and compare teh string to the new string you want to place in. So in your case you would look for the string "foo = 1" and if you find it then remove it.
that should do it
:::: [ Triple Buffer V2.0 ] ::::
std::list<std::string> foolist;std::string newfoo = "foo = 1"; // To be added to liststd::list<std::string>::iterator i = foolist.beg(); // Declare iteratorfor( ; i < != foolist.end() ++i ){ if( *i == newfoo ) // Check if it''s alread in the list { foolist.erase( i ) // Erase if it is and exit for loop break; }}foolist.push_back( newfoo ); // Add new one
that should do it
:::: [ Triple Buffer V2.0 ] ::::
This sounds more like a job for map.
[edited by - Beer Hunter on July 25, 2003 8:22:05 PM]
#include <iostream>#include <map>#include <string>int main(){ std::map<std::string, int> properties; properties["width"] = 640; properties["height"] = 480; std::cout << properties["width"] << std::endl; std::cout << properties["height"] << std::endl; std::cout << properties["foo"] << std::endl; // outputs 0 std::cin.get();}
[edited by - Beer Hunter on July 25, 2003 8:22:05 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement