赞
踩
#include<iostream> using namespace std; #include <vector> #include <algorithm>//标准算法的头文件 //vector容器存放内置数据类型 void myPrint(int val) { cout << val << endl; } void test01() { //创建了一个vector容器,数组 vector<int> v; //想容器中插入数据 v.push_back(10); v.push_back(20); v.push_back(30); v.push_back(40); v.push_back(50); //通过迭代器访问容器中的数据‘ //vector<int>::iterator itBegin = v.begin();//起始迭代器 指向容器中第一个元素 //vector<int>::iterator itEnd = v.end();//结束迭代器 指向容器中最后一个元素的下一个地址 第一种遍历方式 //while (itBegin != itEnd) { // cout << *itBegin << endl; // itBegin++; //} //第二种遍历方式 /*for (vector<int>::iterator it = v.begin(); it != v.end(); it++) { cout << *it << endl; }*/ //第三种遍历方式 利用stl提供遍历算法 for_each(v.begin(), v.end(), myPrint);//回调函数 } int main() { test01(); }
学习目标:vector中存放自定义数据类型,并打印输出
#include<iostream> using namespace std; #include <vector> #include <algorithm>//标准算法的头文件 #include <string> class Person { public: Person(string name, int age) { this->m_Name = name; this->m_Age = age; } string m_Name; int m_Age; }; void test01() { vector<Person>v; Person p1("aaa", 11); Person p2("bbb", 22); Person p3("ccc", 33); Person p4("ddd", 44); Person p5("eee", 55); //添加数据 v.push_back(p1); v.push_back(p2); v.push_back(p3); v.push_back(p4); v.push_back(p5); //遍历容器中的数据 for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) { cout << "姓名: " << (*it).m_Name << "年龄: " << (*it).m_Age << endl; } } void test02() { vector<Person*>v; Person p1("aaa", 11); Person p2("bbb", 22); Person p3("ccc", 33); Person p4("ddd", 44); Person p5("eee", 55); //添加数据 v.push_back(&p1); v.push_back(&p2); v.push_back(&p3); v.push_back(&p4); v.push_back(&p5); //遍历容器中的数据 for (vector<Person *>::iterator it = v.begin(); it != v.end(); it++) { Person * p = (*it) cout << "姓名: " << p->m_Name << "年龄: " << p->m_Age << endl; } } int main() { //test01(); test02(); }
学习目标:容器中嵌套容器,我们将所有数据进行遍历输出
#include<iostream> using namespace std; #include <vector> void test01() { vector <vector<int>>v; //创建小容器 vector<int>v1; vector<int>v2; vector<int>v3; vector<int>v4; vector<int>v5; //向小容器添加数据 for(int i = 0; i < 4; i++) { v1.push_back(i + 1); v2.push_back(i + 2); v3.push_back(i + 3); v4.push_back(i + 4); } //将小容器插入到大容器中 v.push_back(v1); v.push_back(v2); v.push_back(v3); v.push_back(v4); //通过大容器,把所有数据遍历一遍 for (vector <vector <int>> ::iterator it = v.begin(); it != v.end(); it++) { // (*it)------容器 vector<int> for (vector<int>::iterator vit = (*it).begin(); vit != (*it).end(); vit++) { cout << *vit << " "; } cout << endl; } } int main() { test01(); }
#include<iostream> using namespace std; #include <string> //string 的构造 void test01() { string s1;//默认构造 const char * str = "hello world"; string s2(str); cout << "s2 = " << s2 << endl; string s3(s2); cout << "S3 = " << s3 << endl; string s4(10, s3); cout << "s4 = " << s4 << endl; } int main() { test01(); }
总结:string的多种构造方式没有可比性,怎么好用怎么来
#include<iostream> using namespace std; //string赋值 void test01() { string str1; str1 = "hello world"; cout << "str1 = " << str1 << endl; string str2; str2 = str1; string str3; str3 = "A"; string str4; str4.assign("hell c++"); cout << "str4 = " << str4 << endl; string str5; str5.assign("hello c++", 5); cout << "str5 = " << str5 << endl; string str6; str6.assign(str5); string str7; str7.assign(10,"w"); } int main() { test01(); }
总结:string的赋值方式很多,operator=这种方式比较实用的
修改可以使用同样的方式
str的插入和删除下标都是从0开始
vector的多种构造方式没有可比性,灵活使用即可
实际使用 收缩内存
总结:sort算法非常实用,使用时包含头文件algorithm即可
总结:
如果不允许插入重复数据可以利用set
如果需要插入重复数据利用multiset
存放内置函数
存放自定义函数
定义类型
修改内置函数
实例化时使用
总结:
对于自定义数据类型,set必须指定排序规则才可以插入数据
总结:map中所有元素都是成对出现,插入数据时候使用对组
不建议使用第四种,可以利用key访问value
仿函数
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4JdS95jz-1667824623562)(C:/Users/62476/AppData/Roaming/Typora/typora-user-images/image-20221107165840580.png)]
一元谓词
二元谓词
3.3.4 逻辑仿函数
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。