C++类型的增删改查(1)--vector

摘要

C++入门 vector类的增删改查

1.写在前面

1.1 头文件

在C++中继承了C语言的头文件,型如.h的拓展后缀,去掉.h在文件前加c即为引用

1
2
3
#include <cmath>
#include <cstdio>
#include <cctype>

1.2 命名空间

1
using namespace std;

类似于python的导入库函数,在python中如果使用import turtle,后面调用该库的函数需要使用turtle.xxx,而使用from turtle import *就导入了所有成员。

而在C++中使用#include <iostream>就可以使用cin(输入)和cout(输出)了,但需要写成

1
2
std::cin >> a;
std::cout << a << endl;
而使用using namespace std;后可以直接使用cincout,不需要加std::,比较方便。

1.3 变量声明

  1. 在C++中,变量在使用前声明即可。

  2. 在for循环里可以使用

1
2
3
for (int i = 0; i < N; i++>){

}

的方式定义i。

  1. auto可以让编译器自己推断变量的类型(需要C++11)

方便用于迭代器

1
2
3
vector<int>::iterator it
等效于
auto it

也可以直接使用auto遍历向量,下面展示两种迭代方式。 看不懂可以先跳过后面会再介绍。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <vector>
using namespace std;

int main(){
vector<int> v(10,2); //生成一个长度为10,每个元素都为2的向量
for (auto it = v.begin(); it != v.end(); it++){
cout << *it << " ";
}
cout << endl; //换行
for (auto it : v){
cout << it << " ";
}
getchar();
return 0;
}
----输出----
2 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2

2.vector类

2.1 简介

C中的数组是定长的,而vector翻译为向量或矢量,是动态数组,可以任意改变长度,使用需要包含头文件#include <vector>使用命名空间std

定义方式为

1
2
3
4
5
6
vector<类型> 名称;
vector<int> v1;
vector<int> v2(10); //定义长度为10,默认元素为0
vector<int> v3(10,2);//定义长度为10,定义元素为2
vector<int> v4(v3); //使用向量v3来创建v4
vector<int> v5(v3.begin(),v3.begin() + 3);//使用向量v3中的第1到3个元素(共三个)来创建v5,对非向量(数组)也可以这样赋值。

2.2 增

  1. 使用.push_back()向向量a末尾增加元素,类似于python列表的.append()方法
1
2
3
4
vector<int> a;
for (int i = 0; i < 10; i++){
a.push_back(i);
}
  1. 使用下标直接增加,需要提前定好vector的大小,不然会出错
1
2
3
vector<int> a(10);
for(int i=0;i<10;i++)
a[i]=i;
  1. 使用.insert()来插入
1
2
3
4
vector<int> a(10,2);
vector<int> v2(v.begin(),v.begin() + 3);//v2 = 2 2 2
v2.insert(v2.begin(),3); //在开头插入3,v2 = 3 2 2 2
v2.insert(v2.begin() + 1,3,4);//在1的位置插入3个4,v2 = 3 4 4 4 2 2 2

2.3 删

  1. 使用.clear()来清空vector
1
a.clear();
  1. 使用.pop_back()来弹出最后一个元素
1
a.pop_back();

注意这里和python的pop不同,该函数没有返回值

  1. 使用.erase()擦除元素
1
2
a.erase(a.begin());//擦除第一个元素
a.erase(a.begin(),a.begin()+3);//擦除第1-3个元素

2.4 改

  1. 直接引用修改
1
a[0] = 1;
  1. 使用.swap()交换
1
v2.swap(v);//将v2和v做交换
  1. 使用sort()函数进行排序
1
2
#include <algorithm>
sort(a.begin(),a.end()); //对a中的元素进行从小到大排列

可以增加第三个函数参数实现自定义排序:按学生成绩排序,如成绩相同则按学号先后

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <algorithm>
using namespace std;
struct stu {
int number;
int score;
};

bool cmp(stu a, stu b){
return a.score != b.score ? a.score > b.score : a.number < b.number;
//判断学生成绩是否相等,不同按成绩排,相同则按学号排
}

int main(){
stu arr[10];
for (int i = 0; i < 10; i++){
arr[i].number = i;
cin >> arr[i].score;
}
sort(arr, arr+10, cmp);
for (int i = 0; i < 10; i++){
cout << arr[i].number << ":" << arr[i].score << endl;
}
getchar();
getchar();
return 0;
}

  1. 使用 reverse()函数对vector进行倒转
1
2
#include <algorithm>
reverse(a.begin(),a.end()); //对a中的元素倒置
  1. 使用copy()函数对vector进行复制
1
2
#include <algorithm>
copy(a.begin(),a.end(),b.begin()+1); //把a中的元素复制到b中(从第一个元素开始),覆盖但不能超过b的最大长度

2.5 查

  1. 使用下标读取,a.size()会返回向量a的长度
1
2
3
for (int i = 0; i < a.size(); i++){
a[i] = 15;
}
  1. 使用迭代器遍历
1
2
3
4
5
6
7
for (auto it = v.begin(); it != v.end(); it++){
cout << *it << " ";
}

for (auto it : v2){
cout << it << " ";
}
  1. 使用find()函数查询是否存在该元素
1
2
3
4
5
#include <algorithm>
find(a.begin(),a.end(),10); //在a中的元素中查找10,若存在返回其在向量中的位置
if (find(a.begin(),a.end(),key) == a.end()){
//a中不存在元素key
}
  1. 使用count()函数实现计数
1
int num=count(a.begin(),a.end(),key);//定义num为a中key元素的个数
  1. 使用count_if()函数计算满足条件的个数

例如:计算vector中奇数的个数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool cmp(int num){
return num%2; //偶数返回False 奇数返回True
}
int main(){
vector <int> v;
for(int i=1;i<=10;i++)
v.push_back(i);
cout<<count_if(v.begin(),v.end(),cmp)<<endl;

getchar();
return 0;
}
----输出----
5