C++ with STL
2.5.1vector存放内置数据类型

1 #include<vector> 2 #include<algorithm> 3 #include<iostream> 4 #include<algorithm> 5 using namespace std; 6 7 //vecotr容器存放内置数据类型 8 void myPrint(int val) 9 { 10 cout << val << endl; 11 } 12 13 void test01() 14 { 15 //创建了一个vector容器,数组 16 vector<int> v; 17 18 //向容器中插入数据 19 v.push_back(10); 20 v.push_back(20); 21 v.push_back(30); 22 v.push_back(40); 23 v.push_back(50); 24 25 // vector<int>::iterator itBegin = v.begin();//起始迭代器 指向容器中第一个元素 26 // vector<int>::iterator itEnd = v.end();//结束迭代器,指向容器中最后一个元素的下一个位置 27 // 28 // //第一种遍历方式 29 // while (itBegin != itEnd) 30 // { 31 // cout << *itBegin << endl; 32 // itBegin++; 33 // } 34 35 //第二种遍历方式 36 /*for (vector<int>::iterator it = v.begin(); it != v.end(); it++) 37 { 38 cout << *it << endl; 39 }*/ 40 41 //第三种遍历方式,利用STL提供遍历算法 42 for_each(v.begin(), v.end(), myPrint); 43 } 44 int main() 45 { 46 test01(); 47 return 0; 48 }
2.5.2vector存放自定义数据类型

1 #include<iostream> 2 #include<string> 3 #include<vector> 4 using namespace std; 5 //vector容器存放自定义数据类型 6 class Person 7 { 8 public: 9 Person(string name, int age) 10 { 11 this->m_Name = name; 12 this->m_Age = age; 13 } 14 string m_Name; 15 int m_Age; 16 }; 17 18 void test01() 19 { 20 vector<Person>v; 21 22 Person p1("aaa", 10); 23 Person p2("bbb", 20); 24 Person p3("ccc", 30); 25 Person p4("ddd", 40); 26 Person p5("eee", 50); 27 28 //向容器中添加数据 29 v.push_back(p1); 30 v.push_back(p2); 31 v.push_back(p3); 32 v.push_back(p4); 33 v.push_back(p5); 34 35 //遍历容器中的数据 36 for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) 37 { 38 //cout << "姓名:" << (*it).m_Name << "年龄:" << (*it).m_Age << endl; 39 cout << "姓名:" << it->m_Name << "年龄:" << it->m_Age << endl; 40 } 41 } 42 43 //存放自定义数据类型 指针 44 void test02() 45 { 46 vector<Person*>v; 47 48 Person p1("aaa", 10); 49 Person p2("bbb", 20); 50 Person p3("ccc", 30); 51 Person p4("ddd", 40); 52 Person p5("eee", 50); 53 54 //向容器中添加数据 55 v.push_back(&p1); 56 v.push_back(&p2); 57 v.push_back(&p3); 58 v.push_back(&p4); 59 v.push_back(&p5); 60 61 //遍历容器 62 for (vector<Person*>::iterator it = v.begin(); it != v.end();it++) 63 { 64 cout << "姓名:" << (*it)->m_Name << "年龄:" << (*it)->m_Age << endl; 65 } 66 67 } 68 int main() 69 { 70 test02(); 71 system("pause"); 72 }
2.5.3vector容器嵌套容器

1 #include<iostream> 2 #include<vector> 3 using namespace std; 4 5 //容器嵌套容器 6 void test01() 7 { 8 vector<vector<int>>v; 9 10 //创建小容器 11 vector<int>v1; 12 vector<int>v2; 13 vector<int>v3; 14 vector<int>v4; 15 16 //向容器中添加数据 17 for (int i = 0; i < 4; i++) 18 { 19 v1.push_back(i + 1); 20 v2.push_back(i + 2); 21 v3.push_back(i + 3); 22 v4.push_back(i + 4); 23 } 24 25 //将小容器插入到大容器中 26 v.push_back(v1); 27 v.push_back(v2); 28 v.push_back(v3); 29 v.push_back(v4); 30 31 //通过大容器,把所有数据遍历一遍 32 for (vector<vector<int>>::iterator it = v.begin(); it != v.end(); it++) 33 { 34 //(*it)----容器 vector<int> 35 for (vector<int>::iterator vit = (*it).begin(); vit != (*it).end(); vit++) 36 { 37 cout << *vit << " "; 38 } 39 cout << endl; 40 } 41 } 42 43 int main() 44 { 45 test01(); 46 return 0; 47 }
3.1.2string构造函数

1 #include<iostream> 2 #include<string> 3 using namespace std; 4 //string的构造函数 5 void test01() 6 { 7 string s1;//默认构造 8 9 const char* str = "hello world"; 10 string s2(str); 11 12 cout << "s2=" << s2 << endl; 13 14 string s3(s2); 15 cout << "s3=" << s2 << endl; 16 17 string s4(10, 'a'); 18 cout << "s4=" << s4 << endl; 19 } 20 21 int main() 22 { 23 test01(); 24 }
3.2.3string赋值操作

1 #include<iostream> 2 #include<string> 3 using namespace std; 4 5 void test01() 6 { 7 string str1; 8 str1 = "hello world"; 9 cout << "str1=" << str1 << endl; 10 11 string str2; 12 str2 = str1; 13 cout << "str2=" << str2 << endl; 14 15 string str3; 16 str3 = 'a'; 17 cout << "str3=" << str3 << endl; 18 19 string str4; 20 str4.assign("hello C++"); 21 cout << "str4=" << str4 << endl; 22 23 string str5; 24 str5.assign("hello C++", 5); 25 cout << "str5=" << str5 << endl; 26 27 string str6; 28 str6.assign(str5); 29 cout << "str6=" << str6 << endl; 30 31 string str7; 32 str7.assign(10,'w'); 33 cout << "str7=" << str7 << endl; 34 } 35 36 int main() 37 { 38 test01(); 39 return 0; 40 }
3.2.4strng字符串拼接

1 #include<iostream> 2 #include<string> 3 using namespace std; 4 void test01() 5 { 6 string str1 = "我"; 7 str1 += "爱玩游戏"; 8 cout << "str1=" << str1 << endl; 9 10 str1 += ':'; 11 cout << "str1=" << str1 << endl; 12 13 string str2 = "lol dnf"; 14 str1 += str2; 15 cout << "str1=" << str1 << endl; 16 17 string str3 = "i"; 18 str3.append(" love "); 19 cout << "str3=" << str3 << endl; 20 21 str3.append("game abcde", 4); 22 cout << "str3=" << str3 << endl; 23 24 str3.append(str2,0,3);//只截取到lol 25 str3.append(str2,4,3);//只截取了 dnf 参数2 从哪个位置开始截取,参数3 截取字符个数 26 27 cout << "str3=" << str3 << endl; 28 } 29 30 int main() 31 { 32 test01(); 33 return 0; 34 }
3.2.5string查找和替换

1 #include<iostream> 2 using namespace std; 3 4 //字符串的查找和替换 5 6 //1.查找 7 8 void test01() 9 { 10 string str1 = "abcdefg"; 11 int pos=str1.find("de"); 12 if (pos == -1) 13 { 14 cout << "未找到字符串" << endl; 15 } 16 cout << "找到字符串,pos=" << pos << endl; 17 18 //rfind和find区别 19 //rfind从右往左查找 find从左往右查找 20 pos=str1.rfind("de"); 21 cout << "pos=" << pos << endl; 22 } 23 //2.替换 24 void test02() 25 { 26 string str1 = "adbdefg"; 27 //从1号位置起,3个字符 替换为“1111” 28 str1.replace(1, 3, "1111"); 29 cout << "str1=" << str1 << endl; 30 } 31 32 int main() 33 { 34 //test01(); 35 test02(); 36 return 0; 37 }
3.2.6string字符串比较

1 #include<iostream> 2 using namespace std; 3 4 //字符串比较 5 6 void test01() 7 { 8 string str1 = "hello"; 9 string str2 = "hello"; 10 11 if (str1.compare(str2) == 0) 12 { 13 cout << "str1等于str2" << endl; 14 } 15 else if (str1.compare(str2) > 0) 16 { 17 cout << "str1大于str2" << endl; 18 } 19 else 20 { 21 cout << "str2小于str2" << endl; 22 } 23 } 24 int main() 25 { 26 test01(); 27 return 0; 28 }