1、vector容器,可以理解为数组,可以理解为单端数组,可以动态扩展(重新开辟一片更大空间,把原有的数据再拷贝进去,释放原来的空间)既可以存放内置数据类型,又可以存放自定义数据类型。
(1)vector存放内置数据类型
1 #include<iostream> 2 #include<vector> 3 #include<algorithm> 4 using namespace std; 5 void print(int val){ 6 cout << val << " "; 7 } 8 void test01(){ 9 vector<int> v; //存放内置数据类型 无参构造 vector<int> v1(v.begin(),v.end()); 10 //向容器插入元素 11 v.push_back(1); 12 v.push_back(2); 13 v.push_back(3); 14 //第一种遍历方式 15 vector<int>::iterator itBegin = v.begin(); //起始迭代器 指向容器中的第一个位置 16 vector<int>::iterator itEnd = v.end();//指向容器中最后一个元素的下一个位置 17 while (itBegin!=itEnd) 18 { 19 cout << *itBegin << " "; 20 itBegin++; 21 } 22 cout << endl; 23 //第二种 24 for (vector<int>::iterator it = v.begin(); it != v.end(); it++){ 25 cout << *it << " "; 26 } 27 //第三种 stl提供的算法 28 for_each(v.begin(), v.end() , print); 29 30 } 31 int main(){ 32 test01(); 33 system("pause"); 34 }
上面中例子使用的是vector的默认构造,也就是无参构造,还有三种构造函数,分别区间构造、n个elem构造、拷贝构造,例如:
1 vector<int> v1(v.begin(),v.end()); //区间方式构造 2 3 vector<int > v2(10,1); //v2里面有10个1 4 5 vector<int > v3(v2) //拷贝构造
(2)vector存放自定义数据类型
1 #include<iostream> 2 #include<vector> 3 #include<string> 4 #include<algorithm> 5 using namespace std; 6 class Person{ 7 public: 8 Person(string name,int age){ 9 this->m_Name = name; 10 this->m_Age = age; 11 } 12 string m_Name; 13 int m_Age; 14 }; 15 void print(Person p){ 16 cout << p.m_Name << " "; 17 cout << p.m_Age << endl; 18 } 19 void test01(){ 20 vector<Person>v; 21 Person p1("aa", 12); 22 Person p2("bb", 13); 23 Person p3("cc", 14); 24 //向容器中插入数据 25 v.push_back(p1); 26 v.push_back(p2); 27 v.push_back(p3); 28 29 //遍历 30 for_each(v.begin(), v.end(), print); 31 for (vector<Person>::iterator it = v.begin();it != v.end(); it++){ //it指针 32 cout << it->m_Name << " " << it->m_Age << endl; 33 } 34 } 35 void print1(Person *p){ 36 cout << (p)->m_Age << " " << p->m_Name << endl; 37 } 38 void test02(){ 39 vector<Person *> v1; 40 Person p1("tom", 18); 41 Person p2("tom", 19); 42 v1.push_back(&p1); 43 v1.push_back(&p2); 44 for_each(v1.begin(), v1.end(), print1); 45 for (vector<Person*>::iterator it = v1.begin(); it != v1.end(); it++){ 46 cout << (*it)->m_Name << " " << (*it)->m_Age << endl; 47 } 48 } 49 int main(){ 50 /*test01();*/ 51 test02(); 52 system("pause"); 53 }
(3)vector容器嵌套
1 #include<iostream> 2 #include<vector> 3 #include<string> 4 using namespace std; 5 void test01(){ 6 vector<vector<int>>v; 7 //创建小容器 8 vector<int>v1; 9 vector<int>v2; 10 vector<int>v3; 11 //往小容器中插入数据 12 for (int i = 0; i < 3; i++) 13 { 14 v1.push_back(i + 1); 15 v2.push_back(i + 2); 16 v3.push_back(i + 3); 17 } 18 //在大容器中插入小容器 19 v.push_back(v1); 20 v.push_back(v2); 21 v.push_back(v3); 22 //遍历 23 for (vector<vector<int>>::iterator it = v.begin(); it != v.end();it++) 24 { 25 for (vector<int>::iterator it1 = (*it).begin(); it1!= (*it).end();it1++) 26 { 27 cout << *it1 << " "; 28 } 29 cout << endl; 30 } 31 32 } 33 int main(){ 34 test01(); 35 system("pause"); 36 }
(4)vector赋值操作
1 //1、operator=赋值 2 //把v1里面的值赋值给v2 3 vector<int> v2; //默认构造 4 v2=v1; 5 6 //2、assign区间赋值 7 vector<int> v2 ; //默认构造 8 v2.assign(v1.begin(),v2.end()); //区间开闭后开 9 10 //3、assign n个元素的方式赋值 11 vector<int > v2 ; //默认构造 12 v2.assign(10,2);
(5)vector的容量和大小
1 vector<int>v1; 2 v1.push_back(1); 3 v1.push_back(2); 4 v1.push_back(3); 5 v1.push_back(4); 6 7 if (v1.empty()) 8 { 9 cout << "v1为空" << endl; 10 } 11 //v1.resize(12);//重新指定大小,超出部分默认用0扩充 12 v1.resize(12, 1); //指定用1扩充 13 int cap=v1.capacity(); //容器容量 14 15 cout << "v1的容量" << cap << endl; //容量永远大于或者等于size() 16 int size = v1.size(); //容器大小 17 cout << "size=" << size << endl;
(6)vector的插入和删除
1 vector<int>v1; 2 for (int i = 0; i < 10; i++) 3 { 4 v1.push_back(i); //尾插法 5 } 6v1.insert(v1.begin(),2, 12); //通过迭代器插入数据,在起始位置,插入两个12 7 v1.erase(v1.begin());//删除第一个元素 8 v1.erase(v1.begin(), v1.end()); //删除区间元素相当于清空 9 v1.clear();//清空
(7)vector数据存取
1 vector<int>v1; 2 for (int i = 0; i < 10; i++) 3 { 4 v1.push_back(i); //尾插法 5 } 6 //通过[]访问 7 for (int i = 0; i < v1.size(); i++) 8 { 9 cout << v1[i] << " "; 10 } 11 cout << endl; 12 //通过at方式访问 13 for (int i = 0; i < v1.size(); i++) 14 { 15 cout << v1.at(i) << " "; 16 } 17 cout << endl; 18 cout<<"第一个元素是:"<<v1.front()<<endl; 19 cout<<"最后一个元素:"<<v1.back()<<endl;
2、string容器,内部封装了char*,管理字符串,是一个char*型的容器。
(1)string的构造函数;
1 //空构造 2 string(); 3 string s1; 4 //使用字符串s初始化 5 string(const char*s); 6 const char* s="hi"; 7 string s2(s); 8 //使用string对象初始化另一个string对象,拷贝构造 9 string(const string&s); 10 string s3(s2); 11 //n个字符 12 string (int n,char c); 13 string s4(3,'d');
(2)string赋值操作
1 const char *p = "hello world"; 2 string s1; 3 s1 = p; //s1="hello world" 4 cout << "s1=" << s1 << endl; 5 6 string s2 = s1; 7 cout << "s2=" << s2 << endl; 8 9 string s4; 10 s4.assign("hello world"); 11 cout << "s4=" << s4 << endl; 12 13 string s5; 14 s5.assign("hello world", 5); 15 cout << "s5=" << s5 << endl; 16 17 string s6; 18 s6.assign(s5); 19 cout << "s6=" << s6 << endl; 20 21 string s7; 22 s7.assign(10, 'a'); 23 cout << "s7=" << s7 << endl;
(3)string拼接
1 //1、+=方式 2 string str1 = "I like "; 3 str1 += "cat"; 4 cout << "str1=" << str1 << endl; 5 6 str1 += ':'; 7 cout << "str1=" << str1 << endl; 8 9 string str2 = "tom "; 10 str1 += str2; 11 cout << "str1=" << str1 << endl; 12 13 //2、append方式 14 str1.append("miaomiao"); 15 cout << "str1=" << str1 << endl; 16 17 str1.append("miaowu", 4); 18 cout << "str1=" << str1 << endl; 19 20 str1.append(str2); 21 cout << "str1=" << str1 << endl; 22 23 str1.append(str2, 0, 2); //从0号位置开始截取两位 24 cout << "str1=" << str1 << endl;
(4)string查找和替换
1 string str1 = "abcdefde"; 2 int pos=str1.find("de"); 3 cout << "pos=" << pos << endl; //3 find从左往右查找 4 pos = str1.rfind("de"); 5 cout << "pos=" << pos << endl; //6 rfind从右往左找
1 string str1 = "absdf"; 2 str1.replace(0, 2, "000"); //从0号位置起2个字符替换为000 3 cout << "str1=" << str1 << endl; //000sdf
(5)字符串比较
1 string str1="hello"; 2 string str2 = "hello"; 3 int ret =str1.compare(str2); 4 if(ret==0) 5 { 6 //字符串相同 7 }
(6)字符存取,通过“[]”或者“at”方式对字符串进行读和写。
1 string str1 = "hello"; 2 for (int i = 0; i < str1.size(); i++) 3 { 4 //cout << str1[i] << " "; //通过[]的方式访问 5 cout << str1.at(i) << " "; //通过at的方式访问 6 }
1 str1[0] = 'x'; 2 str1.at(1) = 'x'; 3 cout << "str1=" << str1 << endl; //hello-->xxllo
(7)string 的插入和删除
1 string str="hello"; 2 str.insert(0,"aaa"); //在第0个位置之前插入aaa hello -->aaahello 3 str.erase(0,3); //从0位置起删除3个 aaahello--->hello
(8)从字符串种获取想要的字串
1 string str1 = "abcdef"; 2 string substr = str1.substr(1, 3); //从1号位置截取3个 3 cout << "substr=" << substr << endl; 4 //实用操作 ,从邮箱中获取name 5 string str2 = "liming@sina.com"; 6 int pos = str2.find('@'); //pos=6 7 string name = str2.substr(0, pos); 8 cout << "name=" << name << endl;