【C++】查看各种容器使用的迭代器是什么类型的 code • 2022-4-19 • C • 阅读 44 #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define NONE "\033[m" #define RED "\033[0;32;31m" #define LIGHT_RED "\033[1;31m" #define GREEN "\033[0;32;32m" #define LIGHT_GREEN "\033[1;32m" #define BLUE "\033[0;32;34m" #define LIGHT_BLUE "\033[1;34m" #define DARY_GRAY "\033[1;30m" #define CYAN "\033[0;36m" #define LIGHT_CYAN "\033[1;36m" #define PURPLE "\033[0;35m" #define LIGHT_PURPLE "\033[1;35m" #define BROWN "\033[0;33m" #define YELLOW "\033[1;33m" #define LIGHT_GRAY "\033[0;37m" #define WHITE "\033[1;37m" #define SHINE "\033[5m" //闪烁 #define DASH "\033[9m" // 中间一道横线 #define QUICKSHINE "\033[6m" //快闪 #define FANXIAN "\033[7m" //反显 #define XIAOYIN "\033[8m" // 消隐,消失隐藏 // 验证迭代器所属种类 void _display_category(random_access_iterator_tag mytag) { cout << "random_access_iterator_tag" << endl; } void _display_category(bidirectional_iterator_tag mytag) { cout << "bidirectional_iterator_tag" << endl; } void _display_category(forward_iterator_tag mytag) { cout << "forward_iterator_tag" << endl; } void _display_category(output_iterator_tag mytag) { cout << "output_iterator_tag" << endl; } void _display_category(input_iterator_tag mytag) { cout << "input_iterator_tag" << endl; } template void display_category(string &str, T ite) { cout << FANXIAN << YELLOW << "----------------------------------------begin--------------------------------------------" << NONE << endl; typename iterator_traits::iterator_category cagy; // 用萃取机获取T迭代器类型的种类 cout << RED << str << " : " << NONE; _display_category(cagy); //编译器挑选一个最适合的参数的重载_display_category cout << RED << str << " : " << NONE << "typeid(ite).name() = " << typeid(ite).name() << endl; //cout << "3[1;35m---------------end-------------------3[m" << endl; cout << XIAOYIN << YELLOW << "----------------------------------------end-----------------------------------------------" << NONE << endl << endl << endl; }; int main(void) { string arrayName = "array"; string vectorName = "vector"; string listName = "list"; string forward_listName = "forward_list"; string dequeName = "deque"; string setName = "set"; string mapName = "map"; string multimapName = "multimap"; string multisetName = "multiset"; string unordered_setName = "unordered_set"; string unordered_mapName = "unordered_map"; string unordered_multisetName = "unordered_multiset"; string unordered_multimapName = "unordered_multimap"; display_category(arrayName, array::iterator()); //这种类型():代表产生 一个临时对象 display_category(vectorName, vector::iterator()); display_category(listName, list::iterator()); display_category(forward_listName, forward_list::iterator()); display_category(dequeName, deque::iterator()); display_category(setName, set::iterator()); display_category(mapName, map::iterator()); display_category(multimapName, multimap::iterator()); display_category(multisetName, multiset::iterator()); display_category(unordered_setName, unordered_set::iterator()); display_category(unordered_mapName, unordered_map::iterator()); display_category(unordered_multisetName, unordered_multiset::iterator()); display_category(unordered_multimapName, unordered_multimap::iterator()); return 0; } 欢迎分享,转载请注明来源:内存溢出原文地址: http://outofmemory.cn/langs/674949.html 赞 (0) 打赏 微信扫一扫 支付宝扫一扫 code 管理员组 0 0 生成海报 插入、冒泡、希尔、选择、递归快速、堆排序 上一篇 2022-04-19 【C++】C++ Primer-第五版-10.5 泛型算法结构 下一篇 2022-04-19 发表评论 请登录后评论... 登录后才能评论 提交 评论列表(0条)
评论列表(0条)