list<string> mylist;
mylist.push_back("random stuff");
list<string>::iterator it;
it=mylist.begin();
string mystr;
//and this doesn't work:
mystr=*it;
Let’s say I have a list<string> mylist and it has 3 items. Since I can’t work on the characters of each element I must copy what item I want to a simple string or a char buffer. But I can’t find a way at all, I’ve tried with pointers to arrays as well.
So is there a way to copy those items out of the list ?
Edit:
Yeah sorry , revisited my code , the project that is , and found the error to be somewere else, i was copying from listmylist to a string mystr, with the help of an iterator, and i was using a for loop that had the condition to stop when it encountered the character ‘\0’ put when i was copying it, it didn’t copy the ‘\0’ in my string so in the end i had to put it manually so the function would not work outside the string
Good code:
string temp;
list<string>::iterator it;
it=mylist.begin();//let's say myslist has "random stuff"
temp=*it;//this does not copy the '\0'
temp+='\0';//so i add it myself
for(int n(0);temp[n]!='\0';n++)//now the for loop stops properly
cout<<temp[n];
The code works great and outputs the correct result. Also, you can work with characters of each element like this:
You can cycle through the string with an iterator too even, strings have them as well 🙂 Though strings support random-access-iterators, so you can just access them as arrays like a c-string as I showed in the for loop.