C++ 容器一图以蔽之
读完C++ primary 容器相关章节,有必要总结一下容器的要点,一图说明。
其中的问题,以下是我的一些想法,欢迎交流。
问题1. STL源码剖析 · vector
问题2. STL源码剖析 · RB-tree
问题4:
#include<iostream> #include<vector> #include<map> using namespace std; int main() { cout << "iterator 访问vector " << endl; vector<int> vec = {1, 3, 5, 7, 9, 2, 4, 6, 8}; vector<int>::iterator it = vec.begin(); for(; it != vec.end(); ++it) cout << *it << " "; cout << "\niterator 访问map " << endl; map<string, string> mp = {{"chen", "zhang"}, {"yoci", "han"}}; auto p = mp.begin(); for(; p != mp.end(); ++p) cout << p->first << " " << p->second << endl; cout << "\niterator 访问复合map " << endl; map<string, vector<string>> mmp; for(int i=0; i<5; i++) { mmp.insert(pair<string, vector<string>>(to_string(i), {to_string(i*i), "这是测试时数据"})); } auto mmp_it = mmp.begin(); for(; mmp_it != mmp.end(); ++mmp_it) { cout << mmp_it->first << " - "; for(auto str : mmp_it->second) cout << str << " "; cout << endl; } return 0; }
概念性理解看这里:https://blog.csdn.net/u013719339/article/details/80615217