Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# How do I remove an entry from a vector <CString> if I know it's index?

Posted on 2008-10-01
Medium Priority
625 Views
Hi all,

I have the following :
vector<CString> myVar;
myVar.push_back("A");
myVar.push_back("B");
myVar.push_back("C");

if I want to remove B by its ID, how do I do it?

myVar.RemoveAt(1);
doesn't exist :-)

Thanks

James
0
Question by:James Atkin
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 20

Accepted Solution

ikework earned 800 total points
ID: 22615965
myVar.erase(1);

have a look at:

http://www.cplusplus.com/reference/stl/vector/erase.html

ike
0

LVL 31

Assisted Solution

Zoppo earned 800 total points
ID: 22615983
IMO it must be:

> myVar.erase( myVar.begin() + 1 );

since the 'erase' function expects an iterator.

Hope that helps,

ZOPPO
0

LVL 20

Expert Comment

ID: 22616181
right zoppo .. thanks for correction .. :)
0

LVL 12

Assisted Solution

trinitrotoluene earned 400 total points
ID: 22618556
the following code snippet should help. Also there is function called clear() which can be called to clear and cleanup all elements of t\he vector
``````int main ()
{
unsigned int i;
vector<unsigned int> myvector;

// set some values (from 1 to 10)
for (i=1; i<=10; i++) myvector.push_back(i);

// erase the 6th element
myvector.erase (myvector.begin()+5);

// erase the first 3 elements:
myvector.erase (myvector.begin(),myvector.begin()+3);

cout << "myvector contains:";
for (i=0; i<myvector.size(); i++)
cout << " " << myvector[i];
cout << endl;

return 0;
}
``````
0

LVL 4

Author Closing Comment

ID: 31502024
Thanks!
Hope the points split is ok...
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
###### Suggested Courses
Course of the Month11 days, 21 hours left to enroll