---- 向量容器vector的成员函数pop_back()可以删除最后一个元素.

---- 而函数erase()可以删除由一个iterator指出的元素,也可以删除一个指定范围的元素。

---- 还可以采用通用算法remove()来删除vector容器中的元素,大家可以看到这里说的是算法,而不是方法;

     即vector没有remove()成员,这里的remove是algorithm中的remove函数。

---- 不同的是:采用remove()一般情况下不会改变容器的大小,而pop_back()与erase()等成员函数会改变容器的大小。

1、pop_back()

void pop_back();

Delete last element,Removes the last element in the vector, effectively reducing the container size by one.

删除容器内的最后一个元素,容器的size减1.

This destroys the removed element. 销毁删除的元素

#include <iostream>
#include <vector>
#include <stdio.h>
using namespace std;
int main()
{
	vector<int> vec;
	int sum = 0, i = 0;
	vec.push_back(10);
	vec.push_back(20);
	vec.push_back(30);
	cout << "init vec.size() =  "<< vec.size() << endl;
	while(!vec.empty())
	{
		i++;
		sum += vec.back();	//取最后一个元素	
		vec.pop_back();         //删除最后一个元素
		printf("after %d time, sum: %d size = %d\n", i, sum, vec.size());		
	}
	system("pause");
	return 0;
}

2、erase()

C++98

iterator erase (iterator position);
iterator erase (iterator first, iterator last);

C++11

iterator erase (const_iterator position);
iterator erase (const_iterator first, const_iterator last);

删除指定位置的一个元素或删除指定范围内的元素

Removes from the vector either a single element (position) or a range of elements ([first,last)).  包括first,不包括last。
This effectively reduces the container size by the number of elements removed, which are destroyed. 这个操作会让容器的size减小,减小的个数为删除的元素个数。

迭代器用于erase删除元素后,其后会失效,即不能再用该迭代器操作向量。

第一个erase()的例子

#include <iostream>
#include <vector>
#include <stdio.h>
using namespace std;
int main()
{
	vector<int> vec;
	for( int i = 0; i < 10; i++ )
	{
		vec.push_back( i );
	}
	printf("init vec size: %d\n", vec.size());   //0-9
	
	vec.erase( vec.begin() + 5 );                //删除第6个元素,{0 1 2 3 4 6 7 8 9}
	printf("after erase size: %d\n", vec.size());//9
	vec.erase( vec.begin(), vec.begin() + 3 );   //删除第1个元素,到第4个元素 {3 4 6 7 8 9}
	printf("vec = { ");
	for( unsigned int j = 0; j < vec.size(); j++ )
	{
		
		printf("%d ", vec[j]);
	}
	printf("}\n");
	
	system("pause");
	return 0;
}

第二个erase()的例子

#include <iostream>
#include <vector>
#include <stdio.h>
using namespace std;
int main()
{
	vector<int> vec;
	vec.push_back(100);
	vec.push_back(300);
	vec.push_back(300);
	vec.push_back(300);
	vec.push_back(300);
	vec.push_back(500);
	cout << &vec << endl;
	vector<int>::iterator itor;
	for( itor = vec.begin(); itor != vec.end(); itor++ )
	{
		if( *itor == 300 )
		{
			itor = vec.erase(itor);
		}
	}

	for( itor = vec.begin(); itor != vec.end(); itor++)
	{
		cout<< *itor <<" ";
	}	
	system("pause");
	return 0;
}

解析:

1)初始化后,即调用push_back之后,vec中的元素是{ 100 300 300 300 300 500 }

2)迭代器itor指向第一个元素100,不满足条件,itor++;

     迭代器itor指向第二个元素300,满足条件,调用erase删除300,之后vec是{ 100  300 300 300 500 }

     迭代器itor++,指向第三个元素,会调过一个300,满足条件,调用erase,删除300,之后vec是 { 100 300 300 500 }

     迭代器itor++,指向第四个元素,指向500,所以最终vec中的数据就是{ 100 300 300 500 }

3)主要是vec中的数据不断变化,但是itor的指向一直在增加的缘故

那怎么改善这个问题呢?如果我想删除容器中所有的300,应该怎么做?

#include <iostream>
#include <vector>
#include <stdio.h>
using namespace std;
int main()
{
	vector<int> vec;
	vec.push_back(100);
	vec.push_back(300);
	vec.push_back(300);
	vec.push_back(300);
	vec.push_back(300);
	vec.push_back(500);
	vector<int>::iterator itor = vec.begin();
	while( itor != vec.end() )
	{
		if( *itor == 300 )
		{
			itor = vec.erase(itor);
		}
		else
		{
			itor++;
		}
	}
	printf("vec = { ");
	for( itor = vec.begin(); itor != vec.end(); itor++)
	{
		cout<< *itor <<" ";
	}	
	printf("}\n");

	system("pause");
	return 0;
}

3、remove()

先来抄写一段吧:我们可以选择STL方法(特定类的成员函数)或STL函数,通常方法是更好的选择。首先,它更适合于特定的容器;其次,作为成员函数,它可以使用模板类的内存管理工具,从而在需要时调整容器的长度。

例如:假设有一个由数字组成的链表,并要删除链表中某个特定值(4)的所有实例。如果mylist是一个list<int>对象,则可以使用链表的remove()方法。

mylist.remove(4);   //remove all 4 from the list

调用该方法后,链表中所有值为4的元素都将被删除,同时链表的长度将被自动调整。

还有一个名为remove()的STL函数(算法),它不是由对象调用,而是接受区间参数。因此,如果mylist是一个list<int>对象,则调用该函数的代码如下:

remove(mylist.begin(), mylist.end(), 4);

然而,由于该remove()函数不是成员,因此不能调整链表的长度。它将没被删除的元素放在链表的开始位置,并返回一个指向新的超尾值的迭代器。

来举个例子吧

#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <stdio.h>
using namespace std;

void Show(int v)
{
	std::cout << v << ' ';
}

int main()
{
        int age[10] ={ 2, 3, 5, 4, 2, 3, 2, 6, 2, 7 };
	std::list<int> list_a(age, age + 10);
	std::list<int> list_b(list_a);
	printf("Original list_a:\n");
	for_each(list_a.begin(), list_a.end(), Show);
	printf("\n\n");
	list_a.remove( 2 );
	printf("remove() method list_a:\n");
	for_each(list_a.begin(), list_a.end(), Show);
	printf("\n\n");
	list<int>::iterator itor;
	itor = remove(list_b.begin(), list_b.end(), 2);
	printf("remove() function list_b:\n");
	for_each(list_b.begin(), list_b.end(), Show);
	printf("\n\n");
	list_b.erase( itor, list_b.end() );
	printf("erase() method list_b:\n");
	for_each(list_b.begin(), list_b.end(), Show);
	printf("\n");
	system("pause");
	return 0;
}

vector同理:

#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <stdio.h>
using namespace std;

void Show(int v)
{
	std::cout << v << ' ';
}

int main()
{
	int age[10] ={ 2, 3, 5, 4, 2, 3, 2, 6, 2, 7 };
	vector<int> vec_a(age, age + 10);
	printf("Original vec_a:\n");
	for_each(vec_a.begin(), vec_a.end(), Show);
	printf("\n\n");
	
	vector<int>::iterator itor;
	itor = remove(vec_a.begin(), vec_a.end(), 2);
	printf("remove() function vec_a:\n");
	for_each(vec_a.begin(), vec_a.end(), Show);
	printf("\n");
	printf("remove() size: %d\n\n", vec_a.size());
	vec_a.erase( itor, vec_a.end() );
	printf("erase() method vec_a:\n");
	for_each(vec_a.begin(), vec_a.end(), Show);
	printf("\n");
	printf("erase() size: %d\n\n", vec_a.size());
	system("pause");
	return 0;
}

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐