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

简介: 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
请按任意键继续. . .
目录
相关文章
|
20小时前
|
C++
C++程序中的函数递归调用
C++程序中的函数递归调用
8 1
|
21小时前
|
存储 C++
函数嵌套调用:C++编程的核心技术
函数嵌套调用:C++编程的核心技术
7 1
|
6天前
|
C++ 编译器 程序员
C++ 从零基础到入门(3)—— 函数基础知识
C++ 从零基础到入门(3)—— 函数基础知识
|
6天前
|
编译器 C++ Windows
【C++】vector问题解决(非法的间接寻址,迭代器失效 , memcpy拷贝问题)
不使用memcpy函数不就可以了,然后我们使用简单粗暴的赋值拷贝,这样就不会发生浅拷贝问题了!!!
17 1
|
6天前
|
自然语言处理 编译器 C语言
【C++】C++ 入门 — 命名空间,输入输出,函数新特性
本文章是我对C++学习的开始,很荣幸与大家一同进步。 首先我先介绍一下C++,C++是上个世纪为了解决软件危机所创立 的一项面向对象的编程语言(OOP思想)。
36 1
【C++】C++ 入门 — 命名空间,输入输出,函数新特性
|
6天前
|
存储 算法 对象存储
【C++入门到精通】function包装器 | bind() 函数 C++11 [ C++入门 ]
【C++入门到精通】function包装器 | bind() 函数 C++11 [ C++入门 ]
16 1
|
6天前
|
存储 算法 数据安全/隐私保护
【C++入门到精通】 哈希结构 | 哈希冲突 | 哈希函数 | 闭散列 | 开散列 [ C++入门 ]
【C++入门到精通】 哈希结构 | 哈希冲突 | 哈希函数 | 闭散列 | 开散列 [ C++入门 ]
8 0
|
6天前
|
设计模式 安全 算法
【C++入门到精通】特殊类的设计 | 单例模式 [ C++入门 ]
【C++入门到精通】特殊类的设计 | 单例模式 [ C++入门 ]
19 0
|
4天前
|
测试技术 C++
C++|运算符重载(3)|日期类的计算
C++|运算符重载(3)|日期类的计算
|
5天前
|
C语言 C++ 容器
C++ string类
C++ string类
9 0