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

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
LVL 4
James AtkinSenior Principle Software EngineerAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
ikeworkConnect With a Mentor Commented:
myVar.erase(1);

have a look at:

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


ike
0
 
ZoppoConnect With a Mentor Commented:
IMO it must be:

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

since the 'erase' function expects an iterator.

Hope that helps,

ZOPPO
0
 
ikeworkCommented:
right zoppo .. thanks for correction .. :)
0
 
trinitrotolueneConnect With a Mentor Director - Software EngineeringCommented:
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;
}

Open in new window

0
 
James AtkinSenior Principle Software EngineerAuthor Commented:
Thanks!
Hope the points split is ok...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.