在c++中string类和vector类几乎是用的最多的两个标准库类,现对他们进行归纳比较如下:
1.定义和初始化
共同有的初始化类是这些
string s1;
vector<T> v1;
//默认初始化
string s2(s1);
vector<T> v2(v1);
//副本
string s2 = s1;
vector<T> v2 = v1;
//同上
string s4(n, 'c');
vector<T> v3(n, val);
//用给定的值('c'或者val)来初始化n个对象
不同的初始化类有这些
string有
vector有
string s3(" vlaue");
vector<T> v5{a, b, c, d, e,f ....};
string s3 = "vlaue";
vector<T> v5 = {a, b, c, d ...};
vector<T> v4(n);
2.对象上的操作
相同的对象操作
empty()、size()、用下标对元素进行操作([n])、=、==、!=、 <、<=、>、>=
不同的操作
string有
vector有
+
push_back(t);
getline(is, s) ;