if you used pop_back to do the following "like if the user types in 5 then it will get rid of the first five or something?" looping it 5 times will remove the last 5 elements not the first.
You need to use the erase function...
(Psudo)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
std::vector<int> intVector;
intVectior.push_back(1);
intVectior.push_back(5);
intVectior.push_back(90);
//remove the first 2 values in the vector.
intVector.erase(intVector.begin(), intVector.begin()+1);
std::cout << intVector.at(0); //should be 90
//if you however looped the pop_back...
for (int i=0; i<2; i++)
intVector.pop_back();
std::cout << intVector.at(0); //should be 1
so if i used intVector.at() i could delete any element? i want the user to be able to delete elements from the vector. so i f they want to delete 5 then it will delete 5 only or something like that.
So will it delete the last element then go back 4 more? i only want to delete a certain element, like lets say the 2nd to last element, but still want to keep the last element, will that work?
If you are performing many insertions/deletions vector probably isn't the best container. vectors are good for random access but are slow for insertion/deletion anywhere but the end. Maybe a list or deque would be better.
cout << "Would you like to remove any items?" << endl;
cin >> rem;
strVect.erase(strVect.end()-rem);
for(int i = 0; i < input; i++)
{
cout << strVect[i] << endl;
}
and when i entered 5 choiced it removed the one choice but copied the other choice for the rest of the output like this, this is how it would look all outputted without removing anything:
cake
cookies
punch
banana
brownie
now this is how it looks when i type in 2 element:
cake
cookies
punch
brownie
brownie
why did it change banana to brownie instead of remove it?
The item has been erased, what remains is garbage. For optimal performance, std::vector avoids resizing the internal array, unless explicitly requested.
You see the garbage because you are iterating over the vector using the old size, i < input instead of the vector size strVect.size().
I think you're wanting to use a std::deque rather than a vector.
It offers the benefit of random access as well as being accessible from both the front and the back of the container.