Archived

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

psykr

std::list<std::string>

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.


std::list<std::string> foolist;
std::string newfoo = "foo = 1"; // To be added to list

std::list<std::string>::iterator i = foolist.beg(); // Declare iterator

for( ; 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 ] ::::

Share this post


Link to post
Share on other sites
This sounds more like a job for map.

#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]

Share this post


Link to post
Share on other sites