How to remove element from vector in C++

2 Answers

0 votes
#include <iostream>
#include <vector>
#include <algorithm>

using std::cout;
using std::endl;
using std::vector;

int main()
{
	vector<int> vec{ 1, 2, 3, 4, 5};
	vector<int>::iterator it, it_end;

	it_end = remove(vec.begin(), vec.end(), 2);

	for (it = vec.begin(); it < it_end; it++)
		cout << *it << " ";
	
	cout << endl;

	return 0;
}

/*
run:

1 3 4 5

*/

 



answered May 1, 2018 by avibootz
0 votes
#include <iostream>
#include <vector>
#include <algorithm>

using std::cout;
using std::endl;
using std::vector;

int main()
{
	vector<int> vec{ 1, 2, 3, 4, 5};

	it_end = remove(vec.begin(), vec.end(), 2);

	for (int val : vec)
		cout << val << " ";
	
	cout << endl;

	return 0;
}

/*
run:

1 3 4 5 5

*/

 



answered May 1, 2018 by avibootz
edited May 1, 2018 by avibootz

Related questions

1 answer 170 views
2 answers 286 views
1 answer 182 views
2 answers 219 views
1 answer 203 views
...