C++第10课 STL容器 (二)
2021/9/7 20:08:08
本文主要是介绍C++第10课 STL容器 (二),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1.vector
void testCreateVector() { vector<int> vi; //构造时没有标长度不能直接用下表法访问 //vi[0]=1; vi = { 1,2,3,4 }; vector<string> vs = { "Hello","Hi","loveyou" }; vector<int> arrData(3); //代表长度是3 for (int i = 0; i < 3; i++) { arrData[i] = i; } for (string s : vs) { cout << s << "\t"; } cout << endl; //arrData[3]=23; 溢出访问 //自动扩增只能通过成员函数来做 } void testInsertAndPrint() { vector<string> vs; vs.push_back("Hi"); vs.push_back("Hello"); for (string s : vs) { cout << s << "\t"; } cout << endl; for (int i = 0; i < vs.size(); i++) { cout << vs[i] << endl; } vector<string>::iterator iter; for (iter = vs.begin(); iter != vs.end(); iter++) { cout << *iter << endl; } //用一段内存初始化另一段内存 int arr[3] = { 1,2,3 }; vector<int> vi; vi.assign(arr,arr+3); } void testFun() { vector<string> vs(3); vs[0] = "ILoveYou"; vs[1] = "IMissYou"; vs[2] = "Nothing"; cout << vs.at(0) << endl; cout << vs.size() << endl; cout << boolalpha << vs.empty() << endl; cout << vs.front() << endl; cout << vs.back() << endl; //注意:当创建时带长度,push_back是在长度之后插入,前面没数据的话会空着 vs.push_back("Honey"); for (auto v : vs) { cout << v << "\t"; } cout << endl; } class MM { friend ostream& operator<<(ostream& out, const MM& obj); public: MM(string name, int age) :name(name), age(age) {} protected: string name; int age; }; ostream& operator<<(ostream& out, const MM& obj) { out << obj.name << "\t" << obj.age << endl; return out; } void testUserVector() { vector<MM> vm; vm.push_back(MM("Hello", 19)); for (auto v : vm) { cout << v << endl; } }
这篇关于C++第10课 STL容器 (二)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-07-07Dify + TiDB Vector,快速构建你的AI Agent
- 2024-07-06有没有什么开源的py项目可以对图像进行分类-icode9专业技术文章分享
- 2024-07-05feign默认connecttimeout和readtimeout是多少-icode9专业技术文章分享
- 2024-07-05idea控制台,日志太多,导致部分想看得日志被刷走 搜不到-icode9专业技术文章分享
- 2024-07-05The server selected protocol version Tls10 is not accepted by client preferences [TLs12]-icode9专业技术文章分享
- 2024-07-05怎么清理项目缓存-icode9专业技术文章分享
- 2024-07-04安装 Eyoucms详细图文教程-icode9专业技术文章分享
- 2024-07-04ueditor 复制文章时,图片的链接是一个下载图片地址,该如何处理?-icode9专业技术文章分享
- 2024-07-04怎样判断host有没有对wordpress有缓存呢-icode9专业技术文章分享
- 2024-07-04具有编译功能的系统make后,无法ssh连接-icode9专业技术文章分享