Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Vector question 1

Status
Not open for further replies.

fugigoose

Programmer
Jun 20, 2001
241
US
How can i delete an element from a vector that lies in the middle of the vector? Like lets say i have a vector with 5 elements and i want to remove element 3. I assume you have to use iterators, but I can't find any documentation on how to simply delete a random element. Thank you.
 
An example is better than everything :

Code:
#include <vector>
using namespace std;

int main ()
{
	vector<int> integers;
//fill the vector
	integers.push_back (0);
	integers.push_back (1);
	integers.push_back (2);
	integers.push_back (3);

//delete the third element : access via "beginning iterator + index"
	integers.erase (integers.begin () + 2);

	return 0;
}

--
Globos
 
Wow, you can do that? I knew you count increment the iterator with the ++ but that's awesome. Why didn't I try that? Thanks a bunch, once again your my hero Globos. You are the c++ God!
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top