Vector的resize用法
resize()语法:
1 2 3
| vector<int> v; v.resize(n); // 将v的维度更改为n,v原来的元素值不变,新增的值为"int"的默认值(=0) v.resize(n, val); // 将v的维度更改为n,v原来的元素值不变,新增的值为val
|
resize()函数特点:设原来vector v的长度为m:
- 如果n>m,则v的前m个元素不变,扩充的部分(n-m)填充元素默认值/默认构造函数或者val。
- 如果n<=m,则进行裁剪操作,只取v的前n个元素,但是这n个元素保留原来的值,这种情况下val参数不生效。
例1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| #include <iostream> #include <vector> using namespace std;
template<class T> ostream &operator<<(ostream &os, vector<T> const &v){ os << "{"; for(auto it = v.begin(); it!=v.end(); it++){ os << (*it); if(it+1 != v.end()) os << ", "; } os << "}"; return os; }
int main(){ vector<int> v1; cout << v1 << endl; v1.resize(5,10); cout << v1 << endl; v1.resize(5,11); cout << v1 << endl; v1.resize(3,123); cout << v1 << endl; v1.resize(7,78); cout << v1 << endl; v1.resize(10); cout << v1 << endl; return 0; }
|
输出为:
1 2 3 4 5 6
| {} {10, 10, 10, 10, 10} {10, 10, 10, 10, 10} {10, 10, 10} {10, 10, 10, 78, 78, 78, 78} {10, 10, 10, 78, 78, 78, 78, 0, 0, 0}
|
例2:类的vector:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| #include <iostream> #include <vector> using namespace std;
class Image { public: Image():_data(0){ cout << "default constructor Image(), _data = " << _data << endl; } Image(int p):_data(p){ cout << "new constructor Image(int), _data = " << _data << endl; } int get() const{ return _data; } private: int _data; };
ostream& operator<<(ostream& os, const Image &img){ os << img.get(); return os; }
template<class T> ostream &operator<<(ostream &os, vector<T> const &v){ os << "{"; for(auto it = v.begin(); it!=v.end(); it++){ os << (*it); if(it+1 != v.end()) os << ", "; } os << "}"; return os; }
int main(){ vector<Image> v1; v1.resize(2); cout << v1 << endl; v1.resize(1, Image(211)); cout << v1 << endl; v1.resize(3,Image(123)); cout << v1 << endl; v1.resize(5); cout << v1 << endl; v1.resize(7, Image(456)); cout << v1 << endl; return 0; }
|
输出:
1 2 3 4 5 6 7 8 9 10 11 12
| default constructor Image(), _data = 0 default constructor Image(), _data = 0 {0, 0} new constructor Image(int), _data = 211 {0} new constructor Image(int), _data = 123 {0, 123, 123} default constructor Image(), _data = 0 default constructor Image(), _data = 0 {0, 123, 123, 0, 0} new constructor Image(int), _data = 456 {0, 123, 123, 0, 0, 456, 456}
|
由于resize()的不修改原来元素的特性,因此当我们需要完全重置一个vector时,常常先vector.clear(),再resize()。
例3:clear()配合resize()实现vector的重置:
1 2 3 4 5 6 7 8 9 10 11
| int main(){ vector<Image> v1; v1.resize(2); v1.resize(3,Image(123)); v1.resize(7, Image(456)); cout << v1 << endl; v1.clear(); v1.resize(7, Image(456)); cout << v1 << endl; return 0; }
|
输出:
1 2 3 4 5 6 7
| default constructor Image(), _data = 0 default constructor Image(), _data = 0 new constructor Image(int), _data = 123 new constructor Image(int), _data = 456 {0, 0, 123, 456, 456, 456, 456} new constructor Image(int), _data = 456 {456, 456, 456, 456, 456, 456, 456}
|
打印变量类型
typeid(var).name() 可以显示变量var的数据类型。typeid operator - cppreference.com
例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #include <iostream> #include <typeinfo> #include <vector> using namespace std;
int main(){ int a = 0; double b = 0.0; vector<int> va; vector<double> vb; cout << "&a = " << &a << endl; cout << "&b = " << &b << endl; cout << "typeid(a).name() = " << typeid(a).name() << endl; cout << "typeid(&a).name() = " << typeid(&a).name() << endl; cout << "typeid(b).name() = " << typeid(b).name() << endl; cout << "typeid(&b).name() = " << typeid(&b).name() << endl; cout << "typeid(va).name() = " << typeid(va).name() << endl; cout << "typeid(&va).name() = " << typeid(&va).name() << endl; cout << "typeid(vb).name() = " << typeid(vb).name() << endl; cout << "typeid(&vb).name() = " << typeid(&vb).name() << endl; return 0; }
|
输出:
1 2 3 4 5 6 7 8 9 10
| &a = 0x61fe0c &b = 0x61fe00 typeid(a).name() = i // int typeid(&a).name() = Pi // pointer to int typeid(b).name() = d // double typeid(&b).name() = Pd // pointer to double typeid(va).name() = St6vectorIiSaIiEE // vector<int> typeid(&va).name() = PSt6vectorIiSaIiEE // pointer to vector<int> typeid(vb).name() = St6vectorIdSaIdEE // vector<double> typeid(&vb).name() = PSt6vectorIdSaIdEE // pointer to vector<double>
|
可以看到,即使&a和&b的都是地址常量,但是二者是数据类型是不同的,不能相互赋值,一个是指向int的地址,一个是指向double的地址。