`std::vector::erase` - iterator

std::vector::erase - iterator

1. std::vector::erase - iter--; 必不可少

Given a list of integers, remove all the even elements from the vector and print the vector.
给定一个整数列表,从向量中删除所有偶数元素并打印该向量。

Input: 1, 2, 3, 4, 5, 6, 7, 8, 9
Output: 1 3 5 7 9
Explanation: 2, 4, 6 and 8 which are even are erased from the vector
//============================================================================
// Name        : std::vector::erase
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <vector>

using namespace std;

int main()
{
	vector<int> vector_data	{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16 };

	for (auto iter = vector_data.begin(); iter != vector_data.end(); ++iter)
	{
		cout << *iter << ' ';

		if (*iter % 2 == 0)
		{
			vector_data.erase(iter);
			iter--;
		}
	}

	cout << '\n';

	// Printing the vector
	for (auto it = vector_data.begin(); it != vector_data.end(); ++it)
	{
		cout << *it << ' ';
	}

	return 0;
}

0 1 2 3 4 5 6 7 8 10 12 14 16 
1 3 5 7 

iter--; 必不可少。请一定注意。

Check if the element at each position is divisible by 2, if yes, remove the element and decrement iterator.
检查每个位置的元素是否可被 2 整除,如果是,则删除该元素并递减迭代器。

Time Complexity: O(N) in the worst case when the 1st element is deleted while O(1) in the best case when the last element is deleted.
最坏的情况是删除第一个元素时为 O(N),最坏的情况是删除最后一个元素时为 O(1)

2. std::vector::erase - Return value

An iterator pointing to the new location of the element that followed the last element erased by the function call. This is the container end if the operation erased the last element in the sequence.
指向元素的新位置的迭代器,该元素位于函数调用删除的最后一个元素之后。如果操作删除了序列中的最后一个元素,则这是容器的结尾。

Member type iterator is a random access iterator type that points to elements.
成员类型迭代器是指向元素的随机访问迭代器类型。

3. std::vector::erase - i--; 必不可少

//============================================================================
// Name        : std::vector::erase
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <vector>

using namespace std;

int main()
{
	vector<int> vector_data;
	vector_data.push_back(1);
	vector_data.push_back(2);
	vector_data.push_back(5);
	vector_data.push_back(5);
	vector_data.push_back(3);
	vector_data.push_back(5);
	vector_data.push_back(5);
	vector_data.push_back(4);
	vector_data.push_back(5);
	vector_data.push_back(6);

	// Printing the vector
	for (auto it = vector_data.begin(); it != vector_data.end(); ++it)
	{
		cout << *it << ' ';
	}

	cout << '\n';

	for (unsigned int i = 0; i < vector_data.size(); i++)
	{
		cout << "vector_data.size()" << vector_data.size() << " --> ";

		if (5 == vector_data[i])
		{
			vector_data.erase(vector_data.begin() + i);
			i--;
		}

		cout << vector_data.size() << endl;
	}

	// Printing the vector
	for (auto it = vector_data.begin(); it != vector_data.end(); ++it)
	{
		cout << *it << ' ';
	}

	return 0;
}


1 2 5 5 3 5 5 4 5 6 
vector_data.size()10 --> 10
vector_data.size()10 --> 10
vector_data.size()10 --> 9
vector_data.size()9 --> 8
vector_data.size()8 --> 8
vector_data.size()8 --> 7
vector_data.size()7 --> 6
vector_data.size()6 --> 6
vector_data.size()6 --> 5
vector_data.size()5 --> 5
1 2 3 4 6 

i--; 必不可少。请一定注意。

发布了443 篇原创文章 · 获赞 1685 · 访问量 101万+

猜你喜欢

转载自blog.csdn.net/chengyq116/article/details/104286670
今日推荐