C++ 容器类
2015-07-13 10:06
246 查看
顺序容器包含vector list deque
vector:用于查询操作较多的队列list:用户插入操作较多的队列
deque:双端队列,双端队列中的元素可以从两端弹出,其限定插入和删除操作在表的两端
进行,在队列中间做插入删除操作性能最低。
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <vector> #include <list> #include <deque> #include <string> using namespace std; /** * 初始化队列 */ void init_queue() { vector<int> ivec; list<int> ilist(ivec.begin(), ivec.end()); deque<int> ideque(ivec.begin(), ivec.begin() + ivec.size() / 2); char *words[] = { "o", "s", "o", "f", "o" }; size_t words_size = sizeof(words) / sizeof(char *); vector<string> svec(words, words + words_size); list<string> slist(64," sda"); cout << slist.size() << endl; } /** * 正向迭代 */ void iterator_queue(vector<string> svec) { /*for (vector<string>::iterator iter = svec.begin(); iter!=svec.end(); iter++) { cout << *iter << endl; }*/ vector<string>::iterator iter = svec.begin(); while (iter!=svec.end()) { cout << *iter << endl; iter++; } } /** * 反向迭代 */ void reverse_iterator_queue(vector<string> svec) { for (vector<string>::reverse_iterator iter = svec.rbegin(); iter! =svec.rend();iter++) { cout << *iter << endl; } } /** * 添加参数 * push_back 所有队列 * push_front 除vector以外 * insert 插入到指定迭代器前一个位置 */ vector<string> add_element_2_queue() { char *words[] = { "o", "s", "o", "f", "o" }; size_t words_size = sizeof(words) / sizeof(char *); vector<string> svec(words, words + words_size); svec.push_back("sdzf"); svec.insert(svec.end() - 1, "asda"); return svec; //list<string> slist(words, words + words_size); //slist.push_front("dfg"); //slist.insert(slist.begin + 1, svec.begin, svec.begin+svec.size() / 2); } /** * 另一种获取获取元素的方式 */ void get_element_from_queue() { vector<string> svec = add_element_2_queue(); ////获取第一个 //svec.front(); //*svec.begin(); ////获取最后一个 //svec.back(); //*svec.end(); ////取某个 //svec[3]; //svec.at(3); vector<string>::size_type svec_size = svec.size(); int index = 0; while (index!=svec_size) { cout << svec.at(index++) << "\n"; } } /** * 抹除元素 */ vector<string> remove_element_2_queue() { vector<string> svec = add_element_2_queue(); while (!svec.empty()) { svec.pop_back(); } //svec.erase(svec.begin()); //svec.erase(svec.begin(), svec.begin() + svec.size() / 2); //svec.pop_back(); //svec.clear(); return svec; } /** * 定义两个数值不等的队列 并对其交换 * 虽然做了交换 但交换前迭代器指向的是元素的地址 在交换后并不会改变 */ void swap_queue() { char *words[] = { "o", "s", "o", "f", "o" }; char *words2[] = { "k", "u", "y", "g", "r","t" }; size_t words_size = sizeof(words) / sizeof(char *); vector<string> svec(words, words + words_size); vector<string>::iterator iter = svec.begin(); vector<string> svec2(words2, words2 + words_size+1); //svec.assign(svec2.begin(), svec2.end()-2); svec.swap(svec2);//无论数量是否匹配 都是全部交互 //iterator_queue(svec); } /** * reserve()函数声明预留空间 * 队列个数超过容量值 并且队列所处的内存片段不够用 则重新分配内存地址 * 队列个数超过容量值 并且队列所处的内存片段够用 则扩容 */ void init_capacity() { char *words[] = { "k", "u", "y", "g", "r", "t" }; size_t words_size = sizeof(words) / sizeof(char *); vector<string> svec(words, words + words_size); //svec.reserve(20); cout << svec.size() << " "<< svec.capacity() << endl; cout << &svec << endl; svec.push_back("NEW DATA"); cout << &svec << endl; cout << svec.size() << " " << svec.capacity() << endl; svec.push_back("NEW DATA"); svec.push_back("NEW DATA"); svec.push_back("NEW DATA"); cout << &svec << endl; cout << svec.size() << " " << svec.capacity() << endl; }
队列容器的排序:
stack(FILO)队列最新进入的元素在再尾弹出queue(FIFO)队列是一种特殊的线性表,它只允许在表的前端(front)进行删除操作,
而在表的后端(rear)进行插入操作
priority_queue: 优先级队列顾名思义是根据元素的优先级被读取,接口和queues非常相
近。程序员可以通过template参数指定一个排序准则。缺省的排序准则是利用operator<
形成降序排列,那么所谓“下一个元素”就是“数值最大的元素”。
#include <iostream> #include <stack> #include <queue>//队列和优先级队列 #include <vector> using namespace std; /** * stack:all * queue:push_front()--->list deque; * priority_queue:随机访问 vector deque; */ void init_container() { deque<int> deq; stack<int> istk(deq); vector<int> ivec; stack< int, vector<int> > istk2(ivec); } /** * stack的用法 */ void stack_mth() { stack<int>::size_type stack_size = 10; stack<int> istk; size_t index = 0; while (index++ != stack_size) { istk.push(index); } while (!istk.empty()) { cout << istk.top() << endl; istk.pop(); } } /** * queue的用法,不支持top() */ void queue_mth() { queue<int>::size_type queue_size = 20; queue<int> iqueue; int index = 0; while (index++ != queue_size) { iqueue.push(index); } while (!iqueue.empty()) { cout << iqueue.front() << endl; iqueue.pop(); } } void pri_que_mth() { priority_queue<int>::size_type pri_queue_size = 15; priority_queue<int> ique; int index = 0; while (index++ != pri_queue_size) { ique.push(index); } while (!ique.empty()) { cout << ique.top() << endl; ique.pop(); } }
pair是一种模板类型,其中包含两个数据值,两个数据的类型可以不同,基本的定义如下:pair
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #include <utility>//pair的声明头文件 #include <map> #include <set> #include <vector> #include <stdio.h> using namespace std; typedef map<int, string> _StudMap; typedef _StudMap::iterator _StdMapIter; typedef _StudMap::value_type _SingleStdPair; typedef _StudMap::key_type _SingleStdKeyTpy; typedef _StudMap::mapped_type _SingleStdValTpy; /** * pair的操作 */ void pair_mth() { typedef pair <int, int> _IntPair; _IntPair p1(5, 6); pair <int, const char *> p2 = make_pair(6, "sadsa"); if (p2.first==6&&p2.second=="sadsa") { cout << "equals"; } } /** * 定义Map的三种形式 */ _StudMap init_mapContainer() { //_StudMap myMap; //_StudMap myMap2(myMap); size_t map_size = 10; vector< _SingleStdPair > ivec; int index = 0; while (index++ != map_size) { char res[20]; sprintf(res, "%d---", index); ivec.push_back(make_pair(index, res)); } _StudMap myMap3(ivec.begin(), ivec.begin() + ivec.size() / 2); return myMap3; } /** * value_type >>>>>>pair * key_type * mapped_type */ void map_type() { _StudMap map = init_mapContainer(); ; _StudMap::size_type map_size= map.size(); for (_StdMapIter iter = map.begin(); iter != map.end();iter++) { _SingleStdPair pair = *iter; _SingleStdKeyTpy key = pair.first; _SingleStdValTpy val = pair.second; cout << key << " " << val << endl; } } void map_add_find() { //通过下标访问元素 //如果没有对应的值就创建一个键值对并赋初始值 map<string, int> word_count; word_count["HELLO"] = 51; //cout << word_count["HELLO"]; //word_count["HELLO"]=55; //cout << word_count["HELLO"]; //insert() //word_count.insert(map<string, int>::value_type("ANNA",1)); //word_count.insert(make_pair("ANNAY",2)); //find() count() map<string, int>::iterator iter = word_count.find("HELLO"); if (iter != word_count.end()) { cout << iter->second << endl; } cout << word_count.count("HELLO"); } void map_delete() { _StudMap map = init_mapContainer(); //int remove_key=map.erase(2); //cout << "remove count :" << remove_key; _StdMapIter iter= map.erase(map.begin()); cout << "after iter_key:" <<(*iter).first; } void map_iter() { _StudMap map = init_mapContainer(); _StdMapIter iter = map.begin(); while (iter != map.end()) { cout << iter->second; iter++; } }
关联容器和顺序容器的本质差别在于:关联容器通过键(key)存储和读取元素,而顺序容器则通过元素在容器中的位置顺序存储和访问元素。关联容器(Associative containers)支持通过键来高效地查找和读取元素。两个基本的关联容器类型是 map 和 set。除了基本的关联容器 系统还提供了multimap支持同一个键多次出现的 map 类型multiset支持同一个键多次出现的 set 类型
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #include <utility> #include <set> #include <vector> using namespace std; set<int> init_set() { set<int> iset; for (size_t i = 0; i < 10; i++) { iset.insert(i); iset.insert(i); } return iset; } //map element is read-Only void read_set() { set<int> iset = init_set(); //cout << iset.size(); set<int>::iterator iter = iset.find(3); if (iter != iset.end()) { cout << *iter; } }
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #include <utility> #include <set> #include <map> #include <vector> using namespace std; typedef multimap<string, string> _MultiMap; typedef _MultiMap::size_type _MultiMapSize; typedef _MultiMap::iterator _MultiMapIter; typedef pair<_MultiMapIter, _MultiMapIter> _EntryPair; _MultiMap init_map() { _MultiMap mtp; mtp.insert(make_pair("KEY_A", "AA")); mtp.insert(make_pair("KEY_A", "BB")); mtp.insert(make_pair("KEY_A", "CC")); mtp.insert(make_pair("KEY_B", "CCK")); mtp.insert(make_pair("KEY_B", "CCD")); mtp.insert(make_pair("KEY_C", "jt")); mtp.insert(make_pair("KEY_C", "cfh")); mtp.insert(make_pair("KEY_C", "cfgd")); return mtp; } _MultiMapSize multimap_remove() { _MultiMap mtp = init_map(); return mtp.erase("KEY_A"); } void multimap_find(){ _MultiMap map = init_map(); string search_item("KEY_A"); _MultiMapSize size = map.count(search_item); _MultiMapIter iter = map.find(search_item); if (iter!=map.end()) { for (size_t index = 0; index != size;index++,iter++) { cout << iter->second << endl; } } } /** * upper_bound() 某个对象上限 * lower_bound() 某个对象下限 * equal_range() 定义对象范围 */ void multimap_find_new() { _MultiMap map = init_map(); string search_item("KEY_C"); //_MultiMapIter upIter = map.upper_bound(search_item), // lowerIter = map.lower_bound(search_item); //while (lowerIter != upIter ) //{ // cout << lowerIter++->second << endl; //} _EntryPair pair = map.equal_range(search_item); while (pair.first!=pair.second) { cout << (pair.first)++->second << endl; } }
相关文章推荐
- C++ 函数对象
- C++学习 可变参数的宏定义小技巧
- 用C++操作文件
- C语言格式控制符(一)
- c/c++混合编程
- 不可或缺 Windows Native (22) - C++: 多重继承, 虚基类
- 经典回忆Effective C++ 1
- 【C语言】05-printf和scanf函数
- 【C语言】07-基本语句和运算
- 【C语言】11-指针
- 【C语言】12-指向一维数组元素的指针
- 【C语言】17-预处理指令3-文件包含
- 【C语言】17-预处理指令3-文件包含
- 【C语言】21-结构体
- 【C语言】22-枚举
- [C语言]进阶|链表
- C语言之基本算法35—分解质因数(方法二)
- C语言之基本算法34—分解质因数(方法一)
- C语言之基本算法33—矩阵的基本运算
- C语言之基本算法32—鞍点