class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
: _year(year)
, _month(month)
, _day(day)
{}
private:
int _year;
int _month;
int _day;
};
void TestVector1()
{
vector<int> v1(10, 5); //10个5
vector<int> v2(10); //10个0
vector<Date> v3(10); //10个日期类对象
vector<int> v4{ 1, 2, 3 }; // C++11 初始化为1 2 3
}
/*
1. vs中:vector是按照1.5倍方式扩容 linux下是按照2倍方式扩容
2. 放元素时如果已经知道大概要放多少个元素,可以提前将空间设置好
避免:一边插入一边扩容效率低下
*/
void TestVector2()
{
vector<int> v;
v.reserve(100);
size_t cap = v.capacity();
for (size_t i = 0; i < 100; ++i)
{
v.push_back(i);
if (cap != v.capacity())
{
cap = v.capacity();
cout << cap << endl; //什么都没有输出
}
}
}
void TestVector3()
{
vector<int> v{ 1, 2, 3, 4, 5 };
cout << v.front() << endl; //1
cout << v.back() << endl; //5
v.front() = 10;
v.back() = 50;
cout << v.front() << endl; //10
cout << v.back() << endl; //50
//cout << v[13] << endl;
//cout << v.at(14) << endl; //引发异常
v.clear();
//cout << v.front() << endl; //该语句和下面的语句会引发异常
//cout << v.back() << endl;
}
void TestVector4()
{
vector<int> v(3, 5);
int array[] = { 1, 2, 3, 4, 5 };
// v = array; 编译失败
//v.clear();
//for (int i = 0; i < sizeof(array) / sizeof(array[0]); ++i)
//{
// // v[i] = array[i]; // 越界
// v.push_back(array[i]);
//}
v.assign(array, array + sizeof(array) / sizeof(array[0]));
auto it = v.begin();
while (it != v.end())
{
cout << *it << " "; //1 2 3 4 5
it++; //注意要有it++
}
cout << endl;
v.assign(10, 5);
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << " "; //输出10个5
}
cout << endl;
}
void TestVector5()
{
vector<int> v{ 1, 2, 3 };
v.insert(v.begin(), 0); //0 1 2 3
v.insert(v.end(), 4); //0 1 2 3 4
// 0 1 2 3 4
// 在1号元素的位置插入10个5
v.insert(v.begin() + 1, 10, 5); //0 5 5 5 5 5 5 5 5 5 5 1 2 3 4
// 要在data元素所在的位置插入array数组
int data;
cin >> data; //设输入的是2
int array[] = { 10, 20, 30 };
// vector<int>::iterator pos = find(v.begin(), v.end(), data);
auto pos = find(v.begin(), v.end(), data);
if (pos != v.end())
{
v.insert(pos, array, array + sizeof(array) / sizeof(array[0]));
}
// 0 5 5 5 5 5 5 5 5 5 5 1 10 20 30 2 3 4
}
void TestVector6()
{
vector<int> v{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
v.erase(v.begin()); //2 3 4 5 6 7 8 9 0
v.erase(v.begin(), v.begin() + 5); //7 8 9 0
v.erase(v.begin(), v.end()); // clear()
}
void TestVector7()
{
vector<int> v1{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
vector<int> v2{ 12, 23, 34 };
// swap(v1, v2);
v1.swap(v2); //v1和v2交换
}
void TestVector8()
{
vector<int> v1{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
for (size_t i = 0; i < v1.size(); ++i)
{
cout << v1[i] << " "; //1 2 3 4 5 6 7 8 9 0
}
cout << endl;
for (auto e : v1)
{
cout << e << " "; //1 2 3 4 5 6 7 8 9 0
}
cout << endl;
auto it = v1.begin();
while (it != v1.end())
{
cout << *it << " "; //1 2 3 4 5 6 7 8 9 0
++it;
}
cout << endl;
sort(v1.begin(), v1.end());//对v1中元素进行排序
}
// 迭代器失效
void TestVector9()
{
vector<int> v{ 1, 2, 3, 4, 5 };
// it指向的是v的当前的起始位置
auto it = v.begin();
// 扩容之后,可能会导致扩容
// 开辟新空间 拷贝元素 释放旧空间
v.push_back(6);
// it指向v之前的空间已经被释放了,it指向的空间就是非法的
// it的迭代器已经失效了
// 解决迭代器失效的方法:
it = v.begin();
while (it != v.end())
{
cout << *it << " ";
++it;
}
cout << endl;
}
void TestVector10()
{
vector<int> v{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
auto it = v.begin();
while (it != v.end())
{
if (*it & 1)
it = v.erase(it); //如果是奇数就移除 注意erase的返回值
else
++it;
}
for (auto e : v)
{
cout << e << " "; //2 4 6 8 0
}
}
/*
用vector创建二维数组:
1. 矩阵
a. 所有元素都相同
b. 元素不同
2. 每行元素个数不同---比如杨慧三角
*/
// a. 所有元素都相同
void TestVector11()
{
// 5行10列 所有元素都是8
//vector<vector<int>> vv;
//vv.resize(5);
//for (int i = 0; i < 5; ++i)
//{
// for (int j = 0; j < 10; ++j)
// {
// vv[i].push_back(8);
// }
//}
// vector<vector<int>> vv;
// vv.resize(5, vector<int>(10, 8));
vector<vector<int>> vv(5, vector<int>(10, 8));
}
// 2. 每行元素个数不同---比如杨慧三角
void TestVector12()
{
int n;
cin >> n;
// vector(size_t n, const T& val = T())
// vector<vector<int>> vv(n, vector<int>());
vector<vector<int>> vv(n);
for (size_t i = 0; i < vv.size(); ++i)
{
vv[i].resize(i + 1, 1); //容量为i+1,每个数值为1
}
/*
输入5
输出:
0:1
1:1 1
2:1 2 1
3:1 3 3 1
4:1 4 6 4 1
*/
for (size_t i = 2; i < vv.size(); ++i)
{
for (size_t j = 1; j < vv[i].size()-1; ++j)
{
vv[i][j] = vv[i - 1][j] + vv[i - 1][j - 1];
}
}
for (int i = 0; i < vv.size(); i++)
{
for (int j = 0; j < vv[i].size(); j++)
{
cout << vv[i][j] << " ";
}
cout << endl;
}
}
void TestVector13()
{
//vector<int> v;
//v[0] = 1;
//以下写法程序会崩溃
/*
vector<int> v1;
v1.reserve(10);
v1[0] = 1;
*/
//以下是正确写法
vector<int> v;
v.resize(10);
v[0] = 1;
}
int main()
{
//TestVector1();
//TestVector2();
//TestVector3();
//TestVector4();
//TestVector5();
//TestVector6();
//TestVector7();
//TestVector8();
//TestVector9();
//TestVector10();
//TestVector11();
//TestVector12();
TestVector13();
return 0;
}
vector中push_back()、resize()、reserve()、insert()、erase()、front()、back()、assign()、begin()、end()、clear()
最新推荐文章于 2022-12-02 09:32:50 发布