Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 629
  • Last Modified:

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
0
James Atkin
Asked:
James Atkin
3 Solutions
 
ikeworkCommented:
myVar.erase(1);

have a look at:

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


ike
0
 
ZoppoCommented:
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
 
trinitrotolueneCommented:
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now