Solved

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

Posted on 2008-10-01
5
613 Views
Last Modified: 2013-11-20
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
Comment
Question by:jatkin
5 Comments
 
LVL 20

Accepted Solution

by:
ikework earned 200 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

by:Zoppo
Zoppo earned 200 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

by:ikework
ID: 22616181
right zoppo .. thanks for correction .. :)
0
 
LVL 12

Assisted Solution

by:trinitrotoluene
trinitrotoluene earned 100 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;

}

Open in new window

0
 
LVL 4

Author Closing Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Compile GLUT with Visual Studio 2015 1 108
scoreUp challenge 14 56
C++ question 3 62
Beginner to Unreal Engine 4 5 60
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now