赞
踩
个人主页 : zxctscl
如有转载请先通知
在之前已经介绍了string类 【C++】string类初步介绍和【C++】string进一步介绍,这次来看看C++中的顺序表vector。
构造
构造有: vector<int> v;
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
for (size_t i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
cout << endl;
还有在构造的时候直接赋值vector<int>v(10, 1);
构造一个int类型的顺序表开了10个空间,这10个空间初始化为1:
resize开一个空间,空间开好了以后,进行填值初始化。
举个例子:开10个空间,然后初始化为0:
vector<int> a;
a.resize(10, 0);
for (auto e : a)
{
cout << e << " ";
}
cout << endl;
来看看vector的扩容机制:
void test_vector2() { size_t sz; vector<int> v; sz = v.capacity(); cout << "making v grow:\n"; for (int i = 0; i < 100; ++i) { v.push_back(i); if (sz != v.capacity()) { sz = v.capacity(); cout << "capacity changed: " << sz << '\n'; } } }
这里是以1.5倍扩容,不过这里取整了。
如果不想扩容发生,就用reserve提前先开好空间。
void test_vector2() { size_t sz; vector<int> v; v.reserve(100); sz = v.capacity(); cout << "making v grow:\n"; for (int i = 0; i < 100; ++i) { v.push_back(i); if (sz != v.capacity()) { sz = v.capacity(); cout << "capacity changed: " << sz << '\n'; } } }
这样就不用扩容了:
一般来说resize和reserve都不会缩容:
size_t sz;
vector<int> v;
v.reserve(100);
cout << v.size() << endl;
cout << v.capacity() << endl;
v.reserve(10);
cout << v.size() << endl;
cout << v.capacity() << endl;
cout << "-----------------" << endl;
cout << v.size() << endl;
cout << v.capacity() << endl;
v.resize(10);
cout << v.size() << endl;
cout << v.capacity() << endl;
要想缩容就调用这个接口shrink_to_fit
vector<int> v; v.reserve(100); cout << v.size() << endl; cout << v.capacity() << endl; v.reserve(10); cout << v.size() << endl; cout << v.capacity() << endl; cout << "-----------------" << endl; cout << v.size() << endl; cout << v.capacity() << endl; v.resize(10); cout << v.size() << endl; cout << v.capacity() << endl; cout << "-----------------" << endl; v.shrink_to_fit(); cout << v.size() << endl; cout << v.capacity() << endl;
这样就实现了缩容:
vector只支持单个数据的尾插。
直接来看看代码:
#include<iostream> using namespace std; #include<vector> int main() { vector<int> v; v.push_back(1); v.push_back(2); v.push_back(3); v.push_back(4); for (size_t i = 0; i < v.size(); i++) { cout << v[i] << " "; } cout << endl; vector<int>::iterator it = v.begin(); while (it != v.end()) { cout << *it << " "; ++it; } cout << endl; return 0; }
来试试头插:
vector<int> v; v.push_back(1); v.push_back(2); v.push_back(3); v.push_back(4); v.push_back(5); for (auto e : v) { cout << e << " "; } cout << endl; v.insert(v.begin(), 0); for (auto e : v) { cout << e << " "; } cout << endl;
也可以插入一段迭代区间:
vector<int> v; v.push_back(1); v.push_back(2); v.push_back(3); v.push_back(4); v.push_back(5); for (auto e : v) { cout << e << " "; } cout << endl; v.insert(v.begin(), 0); for (auto e : v) { cout << e << " "; } cout << endl; string s("abcd"); v.insert(v.begin(), s.begin(),s.end()); for (auto e : v) { cout << e << " "; } cout << endl;
这里隐私类型转换,将abcd转换为对应的ascii值:
尾删
来看看代码:
vector<int> v; v.push_back(1); v.push_back(2); v.push_back(3); v.push_back(4); for (size_t i = 0; i < v.size(); i++) { cout << v[i] << " "; } cout << endl; v.pop_back(); for (size_t i = 0; i < v.size(); i++) { cout << v[i] << " "; } cout << endl; v.pop_back(); for (size_t i = 0; i < v.size(); i++) { cout << v[i] << " "; } cout << endl;
在vector里面发现没有find,如果我们想要使用find来查找,那么就得用到算法库里面的就行。使用它得包含一个头文件:#include<algorithm>
举个例子:在3位置插入30
#include<iostream> #include<algorithm> using namespace std; #include<vector> int main() { vector<int> v; v.push_back(1); v.push_back(2); v.push_back(3); v.push_back(4); v.push_back(5); for (auto e : v) { cout << e << " "; } cout << endl; vector<int>::iterator pos=find(v.begin(), v.end(), 3); if (pos != v.end()) { v.insert(pos, 30); } for (auto e : v) { cout << e << " "; } cout << endl; }
举个例子删除3位置:
vector<int> v; v.push_back(1); v.push_back(2); v.push_back(3); v.push_back(4); v.push_back(5); for (auto e : v) { cout << e << " "; } cout << endl; vector<int>::iterator pos = find(v.begin(), v.end(), 3); if (pos != v.end()) { v.erase(pos); } for (auto e : v) { cout << e << " "; } cout << endl;
有问题请指出,大家一起进步!!!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。