C++ vector 删除和排序的相关函数

Vector 末尾元素的删除及容器清空释放空间

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

void vtest(vector<int> &v)
{
	cout<<"empty():"<<v.empty()<<endl;
	cout<<"size():"<<v.size()<<endl;
	cout<<"capacity():"<<v.capacity()<<endl<<endl;
}

int main(void)
{
 	vector<int> v1;
	srand(time(0)+rand()); //初始化随机数发生器 
 
	for(int i=0;i<10;i++) v1.push_back(rand()%10); //随机赋10个数 
	for(auto v:v1) cout<<v<<" "; cout<<endl;
	cout<<"首个元素front():"<<v1.front()<<endl;
	cout<<"末尾元素back():"<<v1.back()<<endl;
	vtest(v1);
	 	
 	v1.pop_back(); //删除尾部一个元素 
	for(auto v:v1) cout<<v<<" "; cout<<endl;
	cout<<"末尾元素back():"<<v1.back()<<endl;
	vtest(v1);
	
	cout<<"capacity()没有变小,可以用shrink_to_fit()"<<endl;
	v1.shrink_to_fit(); 
	vtest(v1);
		
 	v1.clear(); //清空,删除所有元素 
 	for(auto v:v1) cout<<v<<"不执行"; cout<<"已删空"<<endl; 
	cout<<"此时back()无意义:"<<v1.back()<<endl;
	vtest(v1);

	vector<int> v2(5); //clear()等价于size()次pop_back()
	for(auto v:v2) cout<<v<<" "; cout<<endl;
	vtest(v2);	
	for(auto v:v2) v2.pop_back();
	for(auto v:v2) cout<<v<<"不执行"; cout<<"已删空"<<endl; 
	vtest(v2);
	
	for(int i=0;i<10;i++) v2.push_back(rand()%10);
	for(auto v:v2) cout<<v<<" "; cout<<endl;
	vtest(v2);
	
	vector<int>().swap(v2); //第三种清空办法,与一个空容器交换 
	for(auto v:v2) cout<<v<<"不执行"; cout<<"已删空"<<endl; 
	vtest(v2);  
	
	//第三种比较彻底,内存空间也已清空 相当于.clear()+.shrink_to_fit()
	
	return 0;
 }

运行结果:

1 5 6 0 9 8 2 8 5 3
首个元素front():1
末尾元素back():3
empty():0
size():10
capacity():16

1 5 6 0 9 8 2 8 5
末尾元素back():5
empty():0
size():9
capacity():16

capacity()没有变小,可以用shrink_to_fit()
empty():0
size():9
capacity():9

已删空
此时back()无意义:201389041
empty():1
size():0
capacity():9

0 0 0 0 0
empty():0
size():5
capacity():5

已删空
empty():1
size():0
capacity():5

4 5 5 5 8 2 6 0 8 7
empty():0
size():10
capacity():10

已删空
empty():1
size():0
capacity():0


--------------------------------
Process exited after 0.4712 seconds with return value 0
请按任意键继续. . .

 

Vector 指定位置的元素删除

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
using namespace std;

template<typename Type>
void v_erase(vector<Type> &v, size_t pos)
{	//删除指定第几个,非位置号,相差1 
	if (pos>v.size()) return;
	vector<Type> tmp(v.begin()+pos,v.end());
	while (v.size()>pos-1) v.pop_back();
	for (auto t:tmp) v.push_back(t);
	vector<Type>().swap(tmp);
}

int main(void)
{
	vector <int> v(10);
	srand(time(0)+rand());
	cout<<"随机产生10个小于100的整数:"<<endl; 
	for_each(v.begin(), v.end(), [](int &i)->void{i=rand()%100;});
	for_each(v.begin(), v.end(), [](int i)->void{cout<<i<<" ";});
	cout<<endl<<endl;
	
	cout<<"删除第4个元素,即v[3]:"<<endl;
 	vector<int>::iterator it = v.begin()+3;
    v.erase(it);
	for (auto i:v) cout<<i<<" "; cout<<endl<<endl;
	
	cout<<"删除第2个元素,即v[1]:"<<endl;
    v.erase(v.begin()+1);
	for (auto i:v) cout<<i<<" "; cout<<endl<<endl;

	cout<<"删除最末尾元素,即v[v.size()-1]:"<<endl;
    v.erase(v.end()-1);  //最未尾的指针v.end()-1,不减一会出错 
	for (auto i:v) cout<<i<<" "; cout<<endl<<endl;

	cout<<"删除倒数第3个元素,即v[v.size()-3]:"<<endl;
    v.erase(v.end()-3); 
	for (auto i:v) cout<<i<<" "; cout<<endl<<endl;

	v.shrink_to_fit(); //释放被删元素的空间 
	
	cout<<"用自定义函数删除第2个元素,即v[1]:"<<endl;
    v_erase(v,2);
	for (auto i:v) cout<<i<<" "; cout<<endl<<endl;
	
	cout<<"用自定义函数删除第1个元素,即v[0]:"<<endl;
    v_erase(v,1);
	for (auto i:v) cout<<i<<" "; cout<<endl<<endl;
	
	cout<<"用自定义函数删除最后1个元素,即v[v.size()-1]:"<<endl;
    v_erase(v,v.size());
	for (auto i:v) cout<<i<<" "; cout<<endl<<endl;
	
	v.shrink_to_fit(); //释放被删元素的空间 
	return 0;

}

运行结果:

随机产生10个小于100的整数:
60 72 27 67 81 60 31 73 1 73

删除第4个元素,即v[3]:
60 72 27 81 60 31 73 1 73

删除第2个元素,即v[1]:
60 27 81 60 31 73 1 73

删除最末尾元素,即v[v.size()-1]:
60 27 81 60 31 73 1

删除倒数第3个元素,即v[v.size()-3]:
60 27 81 60 73 1

用自定义函数删除第2个元素,即v[1]:
60 81 60 73 1

用自定义函数删除第1个元素,即v[0]:
81 60 73 1

用自定义函数删除最后1个元素,即v[v.size()-1]:
81 60 73


--------------------------------
Process exited after 0.5417 seconds with return value 0
请按任意键继续. . .

补充一:erase()可以删除一段区间内的元素,用法: vect.erase(vect.begin()+4,vect.end());  //删除第一个元素后4个起至最后的所有元素,即删除:vect[4]~vect[vect.size()-1]。

补充二:remove()可以移除等于某个值的所有元素,但并未删除移掉元素的空间。比如:{1,0,2,3,0,0,4,0,5}去掉0后剩下的{1,2,3,4,5}去覆盖掉容器最前面的元素{1,2,3,4,5,0,4,0,5},原有顺序并没改变,没被覆盖掉的元素仍保留在原位置处。

补充三:remove()+erase()联用可真正删除等于某个值的所有元素。可用迭代器for auto语句代替,并且if的条件判断更加灵活。见如下代码:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(void)
{
	int a[]={1,0,2,3,0,0,4,0,5};
 	vector <int> v1(a,a+9),v2(a,a+9),v3(a,a+9);
	for(auto v:v1) cout<<v<<" "; cout<<endl;

	remove(v1.begin(),v1.end(),0);  //移除等于0的元素,并未删除空间 
	for(auto v:v1) cout<<v<<" "; cout<<endl;
	
	v2.erase(remove(v2.begin(),v2.end(),0),v2.end()); 
	//remove()+erase()联用,删除所有等于0的元素 
	for(auto v:v2) cout<<v<<" "; cout<<endl;

	for (auto it=v3.begin();it!=v3.end();it++)
		if (*it==0) v3.erase(it--);  //重要:删除后位置前移1 
	//用迭代器循环,删除指定条件的元素,条件可以扩展成(*it>1&&*it<4)等等 
	for(auto v:v3) cout<<v<<" "; cout<<endl;

	return 0;
 }

运行结果:

1 0 2 3 0 0 4 0 5
1 2 3 4 5 0 4 0 5
1 2 3 4 5
1 2 3 4 5

--------------------------------
Process exited after 1.007 seconds with return value 0
请按任意键继续. . .

删除2个元素的自定义函数可用以下这种循环实现:

template<typename Type>
void v_erase(vector<Type> &v, size_t pos, size_t len=2)
{	//删除容器中指定pos位置(含)开始的len个元素 即v[pos]~v[pos+len-1] 
	for (auto it=v.begin()+pos;it!=v.end()&&len;it++){
		v.erase(it--);	
		len--;
	}
}

其实就是一个语句都能搞定的事: v.erase(v.begin()+pos,vbegin()+pos+len);

 

容器元素的排序

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
using namespace std;

string randstr(string &s)
{	//随机产生1-n个字母的字符串 
	const int n=5;
    srand(time(0)+rand()); //time+rand重点 
    for(int i=0;i<rand()%(n+1)+1;i++)
        s.push_back('a'+rand()%26);
    return s;
}

int main(void)
{
	//整型排序 
	vector <int> v(10);
	srand(time(0)+rand());
	cout<<"随机产生10个小于100的整数:"<<endl; 
	for_each(v.begin(), v.end(), [](int &i)->void{i=rand()%100;});
	for_each(v.begin(), v.end(), [](int i)->void{cout<<i<<" ";});
	cout<<endl<<endl;
	
	cout<<"升序排序后:"<<endl;
	sort(v.begin(),v.end());
	for (auto i:v) cout<<i<<" "; cout<<endl;

	cout<<"降序排序后:"<<endl;
	sort(v.begin(),v.end(),[](int x,int y)->bool{return x>y;});
	for (auto i:v) cout<<i<<" "; cout<<endl<<endl;

	string t(32,'-'); cout<<t<<endl<<endl;
	
	//字符串排序 
	vector <string> s(8);
	cout<<"随机产生8个长度1~5的字符串:"<<endl; 
	for_each(s.begin(), s.end(),randstr);
	for (auto i:s) cout<<i<<" "; cout<<endl<<endl;
	
	cout<<"升序排序后:"<<endl;
	sort(s.begin(),s.end());
	for (auto i:s) cout<<i<<" "; cout<<endl;

	cout<<"降序排序后:"<<endl;
	sort(s.begin(),s.end(),[](string x,string y)->bool{return x>y;});
	for (auto i:s) cout<<i<<" "; cout<<endl;
		
}

运行结果:

随机产生10个小于100的整数:
92 37 72 84 37 83 55 12 47 92

升序排序后:
12 37 37 47 55 72 83 84 92 92
降序排序后:
92 92 84 83 72 55 47 37 37 12

--------------------------------

随机产生8个长度1~5的字符串:
naha pgb uh wtd otmit bznv qtla pba

升序排序后:
bznv naha otmit pba pgb qtla uh wtd
降序排序后:
wtd uh qtla pgb pba otmit naha bznv

--------------------------------
Process exited after 0.3823 seconds with return value 0
请按任意键继续. . .

vector 相关文章:

C++ vector声明和赋值的相关函数

C++ vector容器的多种遍历方式

C++ vector 删除和排序的相关函数

C++ vector 赋值、删除、排序类之外的其他函数

C++ vector 容器的全排列算法 next_permutation

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hann Yang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值