c++ 中的集合类
来源:互联网 发布:网络布控中心 编辑:程序博客网 时间:2024/06/11 05:15
概述
list
/* * array.cpp * * Created on: 2014-3-21 * Author: Administrator */#include <iostream>#include <vector>#include <deque>#include <list>#include <cstdlib>using namespace std;// compare only integral part:bool mycomparison(double first, double second) {return (int(first) > int(second));}void displayList(std::list<double> first) {std::cout << "displayList:";for (std::list<double>::iterator it = first.begin(); it != first.end();++it)std::cout << ' ' << *it;std::cout << '\n';}int merge() {std::list<double> first, second;first.push_back(3.1);first.push_back(2.2);first.push_back(2.9);second.push_back(3.7);second.push_back(7.1);second.push_back(1.4);displayList(first);displayList(second);first.sort();second.sort();displayList(first);displayList(second);first.merge(second);displayList(first);displayList(second);// (second is now empty)second.push_back(2.1);first.merge(second,mycomparison); //2.1 1.4 2.2 2.9 3.1 3.7 7.1displayList(first);//参数要求明明是个模板类为什么传方法也可以呢first.sort(mycomparison);displayList(first);return 0;//displayList: 3.1 2.2 2.9//displayList: 3.7 7.1 1.4//displayList: 2.2 2.9 3.1//displayList: 1.4 3.7 7.1//displayList: 1.4 2.2 2.9 3.1 3.7 7.1 结果是排序的//displayList://displayList: 1.4 2.2 2.9 2.1 3.1 3.7 7.1 结果是没排序的}void push() {list<int> list1(2, 1);cout << list1.size() << endl;list1.insert(list1.begin(), 10);list1.push_back(2);list1.push_back(3);list1.push_back(4);for (list<int>::iterator iter = list1.begin(); iter != list1.end();iter++) {cout << *iter << endl;}cout << list1.size() << endl;cout << list1.max_size() << endl;}int assign() //分配新的内容到列表容器,取代它的当前内容,并相应地修改其大小。{std::list<int> first;std::list<int> second;first.assign(7, 100); // 7 ints with value 100second.assign(first.begin(), first.end()); // a copy of firstint myints[] = { 1776, 7, 4 };first.assign(myints, myints + 3); // assigning from arraystd::cout << "Size of first: " << int(first.size()) << '\n';std::cout << "Size of second: " << int(second.size()) << '\n';return 0;}int splice ()// 拼接;{ std::list<int> mylist1, mylist2; std::list<int>::iterator it; // set some initial values: for (int i=1; i<=4; ++i) mylist1.push_back(i); // mylist1: 1 2 3 4 for (int i=1; i<=3; ++i) mylist2.push_back(i*10); // mylist2: 10 20 30 it = mylist1.begin(); ++it; // points to 2 mylist1.splice (it, mylist2); // mylist1: 1 10 20 30 2 3 4 // mylist2 (empty) // "it" still points to 2 (the 5th element) mylist2.splice (mylist2.begin(),mylist1, it); // mylist1: 1 10 20 30 3 4 // mylist2: 2 // "it" is now invalid. it = mylist1.begin(); std::advance(it,3); // "it" points now to 30 mylist1.splice ( mylist1.begin(), mylist1, it, mylist1.end()); // mylist1: 30 3 4 1 10 20 std::cout << "mylist1 contains:"; for (it=mylist1.begin(); it!=mylist1.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; std::cout << "mylist2 contains:"; for (it=mylist2.begin(); it!=mylist2.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0;}int main_list() {//push();//merge();//assign();splice();return 0;}
set
/* * set.cpp * * Created on: 2014-3-22 * Author: Administrator */// set::insert (C++98)#include <iostream>#include <set>void displaySet(std::set<int> myset) {std::cout << "myset contains:";for (std::set<int>::iterator it = myset.begin(); it != myset.end(); ++it)std::cout << ' ' << *it;std::cout << '\n';}int get_allocator() {std::set<int> myset;int * p;unsigned int i;// allocate an array of 5 elements using myset's allocator://get_allocator 实际返回的是 class allocatorp = myset.get_allocator().allocate(5);// assign some values to arrayfor (i = 0; i < 5; i++)p[i] = (i + 1) * 10;std::cout << "The allocated array contains:";for (i = 0; i < 5; i++)std::cout << ' ' << p[i];std::cout << '\n';displaySet(myset);myset.get_allocator().deallocate(p, 5);displaySet(myset);return 0;}void insert() {std::set<int> myset;std::set<int>::iterator it;std::pair<std::set<int>::iterator, bool> ret;// set some initial values:for (int i = 1; i <= 5; ++i)myset.insert(i * 10); // set: 10 20 30 40 50ret = myset.insert(20); // no new element insertedif (ret.second == false)it = ret.first; // "it" now points to element 20myset.insert(it, 25); // max efficiency insertingmyset.insert(it, 24); // max efficiency insertingmyset.insert(it, 26); // no max efficiency insertingint myints[] = { 5, 10, 15 }; // 10 already in set, not insertedmyset.insert(myints, myints + 3);displaySet(myset);}int key_comp() {std::set<int> myset;int highest;std::set<int>::key_compare mycomp = myset.key_comp();for (int i = 0; i <= 5; i++)myset.insert(i * 10);std::cout << "myset contains:";highest = *myset.rbegin();std::cout << highest << std::endl;std::set<int>::iterator it = myset.begin();do {std::cout << ' ' << *it;} while (mycomp(*(++it), highest));//myset.key_comp() 通过的是_Compare,_Compare是 struct less 的别名,less重载了()操作符//方法内部因没有访问内部的私有成员,所有不必用友元的方式,//总之就是比较大小,参数1<参数2返回真// typedef _Compare key_compare;// typename _Compare = std::less<_Key>,// struct less : public binary_function<_Tp, _Tp, bool>// {// bool// operator()(const _Tp& __x, const _Tp& __y) const// { return __x < __y; }// };std::cout << '\n';return 0;}int main_set() {//insert();//key_comp();get_allocator();return 0;}
map
/* * map.cpp * * Created on: 2014-3-22 * Author: Administrator */#include <iostream>#include <map>bool fncomp (char lhs, char rhs) {return lhs<rhs;}struct classcomp { bool operator() (const char& lhs, const char& rhs) const {return lhs<rhs;}};void map1() {std::map<char, int> first;first['a'] = 10;first['b'] = 30;first['c'] = 50;first['d'] = 70;std::map<char, int> second(first.begin(), first.end());std::map<char, int> third(second);std::map<char, int, classcomp> fourth; // class as Comparebool (*fn_pt)(char, char) = fncomp;std::map<char, int, bool (*)(char, char)> fifth(fn_pt); // function pointer as Compare}int main () {map1(); return 0;}
0 0
- Object-C中的集合类
- Objective-C中的集合类
- 5.21 Objective-C中的集合类
- C#OOP中的集合
- object-c中的集合
- 集合之--集合中的类
- <c:forEach 遍历集合中的集合的对象属性参数
- c语言中的时间处理函数集合
- Objective C中的字符串及集合
- JAVA中的集合类
- JAVA中的集合类
- JAVA中的集合类
- JAVA中的集合类
- JAVA中的集合类
- JAVA中的集合类 - -
- JAVA中的集合类
- java中的集合类
- Java中的集合类
- 不夜之夜
- 仙人路 - 创业大佬之马云北大演讲:阿里为何不设班车
- 二叉排序树_Java简单实现
- hibernate 多对一关系映射
- linux中__iomem的意义
- c++ 中的集合类
- C#使用POI把DataGridView中数据导出到excel文档中
- CKEditor图片上传实现详细步骤(使用Struts 2)
- 解决MongoDB导入csv格式含中文的文档编码报错
- linux设置关闭省电模式
- 计算机网络最主要的两个性能指标
- java 操作list集合
- 马云tango陌陌来往仍撼不动微信
- Latex: vsd转eps的详解