C++提高编程---3.2 常用容器-vector 容器【P197~P203】
3.2 vector 容器
3.2.1 vector 基本概念
功能:
- vector 数据结构和数组非常相似,也称为单端数组。
vector 与普通数组区别:
- 不同之处在于数组是静态空间,而 vector 可以动态扩展。
动态扩展:
- 并不是在原空间之后续接新空间,而是找更大的内存空间,然后将原来的数据拷贝新空间,释放原空间。
3.2.2 vector 构造函数
# include<iostream>
# include<vector>
using namespace std;
void printVector(vector<int> v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
vector<int> v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
printVector(v1);
vector<int>v2(v1.begin(), v1.end());
printVector(v2);
vector<int>v3(10, 100);
printVector(v3);
vector<int>v4(v3);
printVector(v4);
}
int main()
{
test01();
system("pause");
return 0;
}
3.2.3 vector 赋值操作
# include<iostream>
# include<vector>
using namespace std;
void printVector(vector<int> &v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
vector<int> v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
printVector(v1);
vector<int>v2 = v1;
printVector(v2);
vector<int>v3;
v3.assign(v1.begin(), v1.end());
printVector(v3);
vector<int> v4;
v4.assign(10, 100);
printVector(v4);
}
int main()
{
test01();
system("pause");
return 0;
}
3.2.4 vector 容量和大小
# include<iostream>
# include<vector>
using namespace std;
void printVector(vector<int> &v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
vector<int>v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
printVector(v1);
if (v1.empty())
{
cout << "v1为空" << endl;
}
else
{
cout << "v1不为空" << endl;
cout << "v1的容量为: " << v1.capacity() << endl;
cout << "v1的大小为: " << v1.size() << endl;
}
v1.resize(15);
cout << "v1的新大小为: " << v1.size() << endl;
printVector(v1);
v1.resize(20, 100);
cout << "v1的新新大小为: " << v1.size() << endl;
printVector(v1);
}
int main()
{
test01();
system("pause");
return 0;
}
3.2.5 vector 插入和删除
# include<iostream>
# include<vector>
using namespace std;
void printVector(vector<int> &v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
vector<int> v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
printVector(v1);
v1.pop_back();
printVector(v1);
v1.insert(v1.begin(), 100);
printVector(v1);
v1.insert(v1.begin(), 2, 1000);
printVector(v1);
v1.erase(v1.begin());
printVector(v1);
v1.erase(v1.begin(), v1.end());
v1.clear();
printVector(v1);
}
int main()
{
test01();
system("pause");
return 0;
}
3.2.6 vector 数据存取
# include<iostream>
# include<vector>
using namespace std;
void test01()
{
vector<int> v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
for (int i = 0; i < v1.size(); i++)
{
cout << v1[i] << " ";
}
cout << endl;
for (int i = 0; i < v1.size(); i++)
{
cout << v1.at(i) << " ";
}
cout << endl;
cout << "第一个元素为: " << v1.front() << endl;
cout << "最后一个元素为: " << v1.back() << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
3.2.7 vector 互换容器
# include<iostream>
# include<vector>
using namespace std;
void printVector(vector<int> &v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
vector<int> v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
cout << "交换前:" << endl;
printVector(v1);
vector<int> v2;
for (int i = 10; i > 0; i--)
{
v2.push_back(i);
}
printVector(v2);
cout << "交换后:" << endl;
v1.swap(v2);
printVector(v1);
printVector(v2);
}
int main()
{
test01();
system("pause");
return 0;
}
巧用 swap 可以收缩内存空间
# include<iostream>
# include<vector>
using namespace std;
void test02()
{
vector<int>v;
for (int i = 0; i < 100000; i++)
{
v.push_back(i);
}
cout << "v的容量为:" << v.capacity() << endl;
cout << "v的大小为:" << v.size() << endl;
v.resize(3);
cout << "v的容量为:" << v.capacity() << endl;
cout << "v的大小为:" << v.size() << endl;
vector<int>(v).swap(v);
cout << "v的容量为:" << v.capacity() << endl;
cout << "v的大小为:" << v.size() << endl;
}
int main()
{
test02();
system("pause");
return 0;
}
3.2.8 vector 预留空间
|