C++ vector容器回顾
一、概述
案例:练习vector容器(基本数据类型、自定义类型、容器嵌套容器),并输出vector容器中的内容
开发工具:sublinetext
二、示例图片
三、示例代码
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | #include <iostream> #include <vector> #include <string> #include <algorithm>//标准算法头文件 using namespace std; //原生指针也是迭代器 void test1(){ int arr[5] = {1,2,3,4,5}; int *p = arr; for ( int i=0;i<5;i++){ //此处P++代表地址+1,用*取值 cout << "p:" <<*(p++)<<endl; } } void myPrint( int val){ cout <<val<<endl; } /** * 测试vector * */ void test2(){ vector< int > v; //创建vector容器,容器中存放的内容为int类型 //插入元素 v.push_back(10); v.push_back(30); v.push_back(20); //遍历元素 vector< int >::iterator itBegin = v.begin(); //v.begin()起始迭代器,指向容器中第一个数据 vector< int >::iterator itEnd = v.end(); //v.end()结束迭代器,指向容器中最后一个元素的下一个位置 cout<< "第一种遍历方式:" <<endl; while (itBegin!=itEnd){ cout<<*itBegin<<endl; itBegin++; } cout<< "第二种遍历方式:" <<endl; for (vector< int >::iterator it = v.begin();it!=v.end();it++){ cout << *it<<endl; } cout<< "第三种遍历" <<endl; for_each(v.begin(),v.end(),myPrint); } //自定义数据类型 class Person{ public : string m_name; int m_age; Person(string name, int age){ this ->m_name = name; this ->m_age = age; } }; void test3(){ vector<Person> v; Person p1( "tony" ,30); Person p2( "luoluoyang" ,3); Person p3( "kiki" ,18); v.push_back(p1); v.push_back(p2); v.push_back(p3); for (vector<Person>::iterator it=v.begin();it!=v.end();it++){ cout << "name:" <<it->m_name<< ",age:" <<it->m_age<<endl; } } //存放自定义数据类型指针 void test4(){ vector<Person*> v; Person p1( "tony" ,30); Person p2( "luoluoyang" ,3); Person p3( "kiki" ,18); v.push_back(&p1); v.push_back(&p2); v.push_back(&p3); for (vector<Person*>::iterator it=v.begin();it!=v.end();it++){ cout<< "name:" <<(*it)->m_name<< ", age:" <<(*it)->m_age<<endl; } } //容器嵌套容器 void test5(){ vector<vector<Person>> vv; vector<Person> v1; vector<Person> v2; vector<Person> v3; //添加数据 Person p1( "tony" ,30); Person p2( "luoluoyang" ,3); Person p3( "kiki" ,18); //将内容放入容器 v1.push_back(p1); v2.push_back(p2); v3.push_back(p3); //将小容器放入大容器内 vv.push_back(v1); vv.push_back(v2); vv.push_back(v3); //输出容器内容 for (vector<vector<Person>>::iterator its = vv.begin();its!=vv.end();its++){ for (vector<Person>::iterator it = its->begin();it!=its->end();it++){ cout<< "name:" <<it->m_name<< ", age:" <<it->m_age<<endl; } } } /** * 程序的入口main方法 * */ int main( int argc, char const *argv[]) { // test1(); // test2(); // test3(); // test4(); test5(); return 0; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
2013-10-19 Java UDP和TCP的区别
2012-10-19 Android推送