参考:https://www.runoob.com/w3cnote/cpp-vector-container-analysis.html
class Student {
public:
private:
string name;
};
int main() {
vector<int>();
vector<int> v1;
vector<Student> v2; // 存放自定义类
vector<string> v3;
vector<vector<int> >; //注意空格。这里相当于二维数组int a[n][n];
vector<int> v5 = {1, 2, 3, 4, 5}; //列表初始化,注意使用的是花括号
vector<string> v6 = {"hi", "my", "name", "is", "lee"};
vector<int> v7(5, -1); //初始化为-1,-1,-1,-1,-1。第一个参数是数目,第二个参数是要初始化的值
vector<string> v8(3, "hi");
vector<int> v9(10); //默认初始化为0
vector<int> v10(4); //默认初始化为空字符串
return 0;
}
int main() {
vector<int> v1;
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
// 使用索引访问元素
for (int i = 0; i < v1.size(); i++) {
cout << v1[i] << endl;
//v1[i] = 100;
//cout << v1[i] << endl;
}
/*reference at(int pos):返回pos位置元素的引用
reference front():返回首元素的引用
reference back():返回尾元素的引用
iterator begin():返回向量头指针,指向第一个元素*/
cout << ".................." << endl;
cout << v1.at(2) << endl; // 返回pos位置元素的引用
cout << v1.front() << endl; // 返回首元素的引用
int a = v1.back(); //返回尾元素的引用
cout << a << endl;
// 使用迭代器访问元素
vector<string> v6 = {"hi", "my", "name", "is", "lee"};
for (vector<string>::iterator iter = v6.begin(); iter != v6.end(); iter++) {
cout << *iter << endl;
//下面两种方法都都可以检查迭代器是否为空
cout << (*iter).empty() << endl;
cout << iter->empty() << endl;
}
// 使用迭代器反向访问元素
for (vector<string>::reverse_iterator iter = v6.rbegin(); iter != v6.rend(); iter++) {
cout << *iter << endl;
}
return 0;
}
#include
#include
using namespace std;
using namespace cv;
int main() {
std::vector<int> demo{1, 2};
//第一种格式用法: 向量中迭代器指向元素前增加一个元素x
demo.insert(demo.begin() + 1, 3);//{1,3,2}
//第二种格式用法: 向量中迭代器指向元素前增加n个相同的元素x
demo.insert(demo.end(), 2, 5);//{1,3,2,5,5}
//第三种格式用法:向量中迭代器指向元素前插入另一个相同类型向量的[first,last)间的数据
std::array<int, 3> test{7, 8, 9};
demo.insert(demo.end(), test.begin(), test.end());//{1,3,2,5,5,7,8,9}
//第四种格式用法:向量中迭代器指向元素前插入一个数组
demo.insert(demo.end(), {10, 11});//{1,3,2,5,5,7,8,9,10,11}
for (int i = 0; i < demo.size(); i++) {
cout << demo[i] << " ";
}
// 输出结果为:1 3 2 5 5 8 9 10 11
cout << "size:" << demo.size() << endl; //10
cout << "capacity:" << demo.capacity() << endl; //10
cout << "max_size:" << demo.max_size() << endl; //4611686018427387903
cout << "empty:" << demo.empty() << endl; //0 判断向量是否为空,若为空,则向量中无元素
//demo.clear(); // 清空向量中所有元素
//cout << "empty:" << demo.empty() << endl; // 1
demo.pop_back(); // 删除向量中最后一个元素
for (int i = 0; i < demo.size(); i++) {
cout << demo[i] << " ";
}
// 输出结果为:1 3 2 5 5 7 8 9 10
cout << "---------------------------------" << endl;
// iterator erase(iterator it):删除向量中迭代器指向元素
demo.erase(demo.begin() + 1);
for (int i = 0; i < demo.size(); i++) {
cout << demo[i] << " ";
}
//输出结果为: 1 2 5 5 7 8 9 10
cout << "---------------------------------" << endl;
//iterator erase(iterator first,iterator last):删除向量中[first,last)中元素
demo.erase(demo.begin() + 1, demo.end() - 2);
for (int i = 0; i < demo.size(); i++) {
cout << demo[i] << " ";
}
//输出结果为: 1 9 10
return 0;
}
int main() {
std::vector<int> demo{12, 32, 23, 58, 52, 17, 28, 39, 10};
// 1. 排序
sort(demo.begin(), demo.end());
for (int i = 0; i < demo.size(); i++) {
cout << demo[i] << " ";
}
// 输出结果为: 10 12 17 23 28 32 39 52 58
cout << endl;
// 倒置
reverse(demo.begin(), demo.end());
for (int i = 0; i < demo.size(); i++) {
cout << demo[i] << " ";
}
// 输出结果为: 58 52 39 32 28 23 17 12 10
// void assign(int n,const T& x):设置向量中前n个元素的值为x,并将集合的size设置为:n
demo.assign(3, -1); //
cout << "size:" << demo.size() << endl; //size:3
for (int i = 0; i < demo.size(); i++) {
cout << demo[i] << " ";
}
// 输出结果为: -1 -1 -1
cout << endl;
//void assign(const_iterator first,const_iterator last):向量中[first,last)中元素设置成当前向量元素
std::vector<int> demo2{12, 32, 23, 58, 52, 17, 28, 39, 10};
demo2.assign(demo.begin(), demo.end());
for (int i = 0; i < demo2.size(); i++) {
cout << demo2[i] << " ";
}
return 0;
}