# Vector problem.

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-14-2008
And3rs
Vector problem.
Code:

```int main() {         char list[100];         char list1[100];         vector<string> gList;         vector<string>::iterator game;         vector<string>::const_iterator myGame;         cout << "Please insert a game you would like to add: ";         cin.getline(list, 100);         gList.push_back(list);         cout << "Please insert another game: ";         cin.getline(list1, 100);         gList.push_back(list1);                         cout << "you got " << gList.size() << " games.";         for(game = gList.begin(); game != gList.end(); ++game)         cout << "\nAnd they are: " << *game;         getch();         system("cls");         char choice[100];         cout << "Would you like to remove " << list << " or " << list1 << "?";         cin.getline(choice, 100);         if(choice == list)         {                 gList.erase((gList.begin(), +0));         }         else if(choice == list1)         {                        const string* str_int = gList;    //I want to make this part an int,                 gList.erase((gList.begin(), +1)); //but how do I do that?.         }         cout << "You now got " << gList.size() << " game left.";         for(myGame = gList.begin(); myGame != gList.end(); --myGame)                 cout << "\nAnd it is " << *myGame;         getch();         return 0; }```
• 10-14-2008
tabstop
That's some interesting C, right there. But anyway, what you want makes no sense. How can you turn a vector of strings into an int?

(Not that this line could ever happen, since choice == list2 must always evaluate false, since you are comparing the addresses of the arrays.)
• 10-14-2008
And3rs
And it should be const char* str_int = whatever

just tried different solutions
• 10-14-2008
And3rs
well, I dont know if the +1 work like when I push_back a string directly to the gList, just trying my way through.
The compiler is complaining(like always!!) that I cant convert std::vector<string> etc etc.
• 10-14-2008
tabstop
Neither I nor the compiler seem to be getting through to you:
gList is something like <"george", "bob", "larry", "larry 2", "larry 3">. Why do you want, or expect, to turn this into an int?
• 10-14-2008
And3rs
hehe im so sorry tabstop. ^^

maybe im reading the error message wrong, it says that
erase(vector<..>::iterator) cannot convert parameter 1 from int to vector<..>::iterator with type string.

• 10-14-2008
tabstop
Right. Because +1 is an int, and shouldn't be. Look at erase again:
Quote:

Originally Posted by cplusplus.com
iterator erase ( iterator first, iterator last );

You need to pass an iterator of where to start, and an iterator one past the thing you want to erase.
OR, you could use the one-parameter version:
Quote:

Originally Posted by cplusplus.com
iterator erase ( iterator position );

which erases ONE thing where the iterator points.

(Note also you can add to iterators, so if you want to point to the second thing of a vector you can do gList.begin()+1. Note the absence of a comma.)
• 10-14-2008
And3rs
and ye, I posted in the wrong group ^^
• 10-14-2008
And3rs
well I can just type
Code:

`gList.erase((gList.end()));`
except that is not erasing anything right now ^^
• 10-14-2008
tabstop
Well, I would hope not. end() does not point at any valid element of a vector. If you want to erase the first element, you would use begin(). If you want to erase the second element, you would use begin()+1. end() is never something you can erase.
• 10-14-2008
And3rs
well ive tried the +1 thingy, that is the problem I had when I posted this
• 10-14-2008
tabstop
Quote:

Originally Posted by And3rs
well ive tried the +1 thingy, that is the problem I had when I posted this

There is still no comma in "+1".

Edit: In other words:
Code:

```erase(gList.begin()+1); //as beautiful as ... well, let's just say "beautiful" erase(gList.begin(), +1); //has no chance on this earth of ever meaning anything```
• 10-14-2008
And3rs
Im sorry if I seems retarted or something, but I still need a comma after
gList.erase((gList.begin(),+1))) ???
• 10-14-2008
tabstop
Quote:

Originally Posted by And3rs
Im sorry if I seems retarted or something, but I still need a comma after
gList.erase((gList.begin(),+1))) ???

There should not be a comma anywhere in that line. Not not not. (When erasing a single thing, erase only takes one argument, not two.)
• 10-14-2008
And3rs
well it&#180;s complaining about that it is missing one ;
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last