1.vector对象的创建
https://en.cppreference.com/w/cpp/container/vector/vector 对象创建文档
//
// Created by 徐昌真 on 2024/11/18.
//
#include <iostream>
#include <vector>
using namespace std;
void printVector(vector<int>& v){
for (vector<int>::iterator iter = v.begin(); iter != v.end(); ++iter) {
cout << *iter << " ";
}
cout << endl;
}
int main() {
//默认构造函数
vector<int> v1;
cout << "默认构造函数" << ": ";
printVector(v1);
//初始化列表
vector<int>v2_1 = {1,2,3,4,5};; //没括号 有等号
cout << "初始化列表v2_1" << ": ";
printVector(v2_1);
vector<int>v2_2 ({5,4,3,2,1,}); //有括号 没等号
cout << "初始化列表v2_2" << ": ";
printVector(v2_2);
//指定size构造
vector<int>v3_1 (5);
cout << "指定size构造" << ": ";
printVector(v3_1);
vector<int>v3_2(5, 520);
cout << "指定size构造" << ": ";
printVector(v3_2);
//迭代器传参
vector<int>v4 (v2_1.begin(), v2_1.end());
cout << "迭代器传参" << ": ";
printVector(v4);
//拷贝构造函数
vector<int> v5_1 (v2_2);
cout << "拷贝构造函数" << ": ";
printVector(v5_1);
vector<int> v5_2 (std::move(v2_1));
cout << "拷贝构造函数" << ": ";
printVector(v5_2);
cout << "v2_1 size: " << v2_1.size() << endl;
return 0;
}
输出
拷贝构造函数 | 类型 | 是否拷贝 | 是否移动 | 是否带分配器 | 是否 constexpr |
---|---|---|---|---|---|
vector(const vector& other); vector(const vector& other); | 拷贝构造函数 | ✅ | ❌ | ❌ | ❌ |
vector(vector&& other); vector(vector&& other); | 移动构造函数 | ❌ | ✅ | ❌ | ❌ |
vector(const vector& other, const Allocator& alloc); | 带分配器的拷贝构造 | ✅ | ❌ | ✅ | ❌ |
vector(vector&& other, const Allocator& alloc); | 带分配器的移动构造 | ❌ | ✅ | ✅ | ❌ |
constexpr vector(const vector& other, const std::type_identity_t<Allocator>& alloc); | 带类型一致分配器的拷贝构造 | ✅ | ❌ | ✅ | ✅ |
constexpr vector(vector&& other, const std::type_identity_t<Allocator>& alloc); | 带类型一致分配器的移动构造 | ❌ | ✅ | ✅ | ✅ |
2.赋值操作
https://en.cppreference.com/w/cpp/container/vector/operator%3D operator=
https://en.cppreference.com/w/cpp/container/vector/assign assign
//
// Created by 徐昌真 on 2024/11/18.
//
#include <iostream>
#include <vector>
using namespace std;
void printVector(vector<int>& v){
for (vector<int>::iterator iter = v.begin(); iter != v.end(); ++iter) {
cout << *iter << ' ';
}
cout << endl;
}
int main() {
vector<int> source1 = {1,2,3,4,5};
cout << "source11: " ;
printVector(source1);
//operator=
//拷贝赋值
vector<int> v1 = source1;
cout << "v1: " ;
printVector(v1);
//移动赋值
vector<int> v2 = std::move(source1);
cout << "v2: " ;
printVector(v2);
cout << "source1 size: " << source1.size() << endl;
//std::vector<T,Allocator>::assign
vector<int> source2 = {1,2,3,4,5};
cout << "source2: " ;
printVector(source2);
//自定义size初始化
vector<int> v3;
v3.assign(5, 520);
cout << "v3: " ;
printVector(v3);
//迭代器初始化
vector<int> v4;
v4.assign(source2.begin(), source2.end());
cout << "v4: " ;
printVector(v4);
//初始化列表
vector<int> v5;
v5.assign({5,4,3,2,1});
cout << "v5: " ;
printVector(v5);
return 0;
}
3.插入操作
en.cppreference.com/w/cpp/container/vector/insert
//
// Created by 徐昌真 on 2024/11/19.
//
#include <iostream>
#include <vector>
using namespace std;
void printVector(vector<int>& v){
for (vector<int>::iterator iter = v.begin(); iter != v.end(); ++iter) {
cout << *iter << ' ';
}
cout << endl;
}
int main() {
//push_back
vector<int> v1;
for (int i = 0; i < 10; ++i) {
v1.push_back(i);
}
cout << "v1: ";
printVector(v1);
//insert_value
v1.insert(v1.begin(), 888);
v1.insert(v1.begin() + 1, 666);
cout << "v1: ";
printVector(v1);
//insert_initializer
v1.insert(v1.begin() + 2, {985, 211});
cout << "v1: ";
printVector(v1);
//insert_size
v1.insert(v1.begin(), 3, 968);
cout << "v1: ";
printVector(v1);
//左值insert
int a = 168;
v1.insert(v1.begin(), a);
cout << "v1: ";
printVector(v1);
cout << "a: " << a << endl;
return 0;
}
输出
删除操作 同下
sshttps://en.cppreference.com/w/cpp/container/vector/clear
https://en.cppreference.com/w/cpp/container/vector/erase
https://en.cppreference.com/w/cpp/container/vector/pop_back
//
// Created by 徐昌真 on 2024/11/19.
//
#include <iostream>
#include <vector>
using namespace std;
void printVector(vector<int>& v){
for (vector<int>::iterator iter = v.begin(); iter != v.end(); ++iter ){
cout << *iter << ' ';
}
cout << endl;
}
int main() {
vector<int> v1;
for (int i = 0; i < 10; ++i) {
v1.push_back(i);
}
cout << "v1: ";
printVector(v1);
//clear
v1.clear();
cout << "size: " << v1.size() << endl;
for (int i = 0; i < 10; ++i) {
v1.push_back(i);
}
cout << "v1: ";
printVector(v1);
//erase
v1.erase(v1.begin());
cout << "v1: ";
printVector(v1);
v1.erase(v1.begin(), v1.begin() + 2);
cout << "v1: ";
printVector(v1);
//pop_back
v1.pop_back();
cout << "v1: ";
printVector(v1);
return 0;
}
输出
4.扩容机制
1.5倍扩容 resize之后 capacity不会变
//
// Created by 徐昌真 on 2024/11/20.
//
#include <iostream>
#include <vector>
using namespace std;
void printVector(vector<int>& v){
for (vector<int>::iterator iter = v.begin(); iter != v.end(); ++iter ){
cout << *iter << ' ';
}
cout << endl;
}
int main() {
vector<int> v;
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
cout << "v: " ;
printVector(v);
cout << "capacity: " << v.capacity() << endl << "size: " << v.size() << endl;
v.resize(100);
cout << "v: " ;
printVector(v);
cout << "capacity: " << v.capacity() << endl << "size: " << v.size() << endl;
v.resize(5);
cout << "v: " ;
printVector(v);
cout << "capacity: " << v.capacity() << endl << "size: " << v.size() << endl;
return 0;
}
5.随机访问
https://en.cppreference.com/w/cpp/container/vector/operator_at
https://en.cppreference.com/w/cpp/container/vector/at
https://en.cppreference.com/w/cpp/container/vector/front
https://en.cppreference.com/w/cpp/container/vector/back
//
// Created by 徐昌真 on 2024/11/19.
//
#include <iostream>
#include <vector>
using namespace std;
void printVector(vector<int>& v) {
for (vector<int>::iterator iter = v.begin(); iter != v.end(); ++iter){
cout << *iter << ' ';
}
cout << endl;
}
int main() {
vector<int> v1;
for (int i = 0; i < 10; ++i) {
v1.push_back(i);
}
cout << "v1: ";
printVector(v1);
//下标访问[] 不会抛出异常
cout << "v1[1]: " << v1[1] << endl;
cout << "v1[12]: " << v1[12] << endl;
//at //抛出异常
cout << "v1.at(1): " << v1.at(1) << endl;
//cout << "v1.at(12): " << v1.at(12) << endl;
//front back
cout << "front: " << v1.front() << endl;
cout << "back: " << v1.back() << endl;
return 0;
}
输出
异常抛出 数组越界
6.内存交换
//
// Created by 徐昌真 on 2024/11/20.
//
#include <iostream>
#include <vector>
using namespace std;
void printVector(vector<int>& v, int i){
cout << 'v' << i << ": ";
for (vector<int>::iterator iter = v.begin(); iter != v.end(); ++iter){
cout << *iter << ' ';
}
cout << endl;
}
void printCapacity(vector<int>& v){
cout << "capacity: " << v.capacity() << endl;
}
int main() {
//1.变量交换
vector<int> v1 = {1,2,3,4,5,6,7,8,9,0};
vector<int> v2 = {985, 211};
printVector(v1, 1);
printVector(v2, 2);
cout << "----------------------------------------" << endl;
v1.swap(v2);
printVector(v1, 1);
printVector(v2, 2);
cout << "----------------------------------------" << endl;
//2.缩容
printCapacity(v2);
v2.resize(5);
printCapacity(v2);
vector<int>(v2).swap(v2); //等价于vector<int> x(v2).swap(v2); 也就是一个拷贝构造函数 x拷贝了v2的内容 capacity = size swap后 x和v1进行内存交换 后面 swap后的x内存又被清理掉
printCapacity(v2);
//3.清理内存
cout << "----------------------------------------" << endl;
v2.clear();
printCapacity(v2);
vector<int>(v2).swap(v2);
printCapacity(v2);
return 0;
}
7.空间预留
reserve修改的是capacity 避免频繁扩容交换数据
//
// Created by 徐昌真 on 2024/11/20.
//
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v1;
v1.reserve(100);
cout << "capacity: " << v1.capacity() << " size: " << v1.size() << endl;
return 0;
}
8.删除操作
https://en.cppreference.com/w/cpp/container/vector/clear
https://en.cppreference.com/w/cpp/container/vector/erase
https://en.cppreference.com/w/cpp/container/vector/pop_back
//
// Created by 徐昌真 on 2024/11/19.
//
#include <iostream>
#include <vector>
using namespace std;
void printVector(vector<int>& v){
for (vector<int>::iterator iter = v.begin(); iter != v.end(); ++iter ){
cout << *iter << ' ';
}
cout << endl;
}
int main() {
vector<int> v1;
for (int i = 0; i < 10; ++i) {
v1.push_back(i);
}
cout << "v1: ";
printVector(v1);
//clear
v1.clear();
cout << "size: " << v1.size() << endl;
for (int i = 0; i < 10; ++i) {
v1.push_back(i);
}
cout << "v1: ";
printVector(v1);
//erase
v1.erase(v1.begin());
cout << "v1: ";
printVector(v1);
v1.erase(v1.begin(), v1.begin() + 2);
cout << "v1: ";
printVector(v1);
//pop_back
v1.pop_back();
cout << "v1: ";
printVector(v1);
return 0;
}
输出
9.高效删除
//
// Created by 徐昌真 on 2024/11/20.
//
#include <iostream>
#include <vector>
using namespace std;
void printVector(vector<int>& v){
for (vector<int>::iterator iter = v.begin(); iter != v.end(); ++iter){
cout << *iter << ' ';
}
cout << endl;
}
void remove1(vector<int>& v, int index){ //直接删除
v.erase(v.begin() + index);
}
void remove2(vector<int>& v, int index){ //先让index位置元素和back元素交换位置 会打乱顺序 数度很快
swap(v[index], v.back());
v.pop_back();
}
int main() {
//插入元素
vector<int> v;
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
//remove1
remove1(v, 2);
cout << "v: ";
printVector(v);
v.clear();
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
//remove2
remove2(v, 2);
cout << "v: ";
printVector(v);
return 0;
}
快速排序
//
// Created by 徐昌真 on 2024/11/21.
//
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void printVector(vector<int>& v){
for (vector<int>::iterator iter = v.begin(); iter != v.end(); ++iter){
cout << *iter << ' ';
}
cout << endl;
}
bool cmp(int a, int b){
return a > b;
}
int main() {
vector<int> v = {985, 211, 12, 100, 969, 188, 168};
//升序
sort(v.begin(), v.end());
printVector(v);
//降序
sort(v.begin(), v.end(), cmp);
printVector(v);
return 0;
}