STL set,map
2016-03-31 22:49
281 查看
[b]一.set和multiset[/b]
set, multiset, map, multimap
内部元素有序排列,新元素插入的位置取决于它的值,查找速度快。
除了各容器都有的函数外,还支持以下成员函数:
find: 查找等于某个值 的元素(x小于y和y小于x同时不成立即为相等)
lower_bound : 查找某个下界
upper_bound : 查找某个上界
equal_range : 同时查找上界和下界
count :计算等于某个值的元素个数(x小于y和y小于x同时不成立即为相等)
insert: 用以插入一个元素或一个区间
预备知识: pair 模板
map/multimap容器里放着的都是pair模版类的对象,且按first从小到大排序
第三个构造函数用法示例:
pair<int,int>
p(pair<double,double>(5.5,4.6));
// p.first = 5, p.second = 4
[b]1.multiset[/b]
template<class Key, class Pred = less<Key>, class A = allocator<Key> > class multiset { …… };
Pred类型的变量决定了multiset 中的元素,“一个比另一个小”是怎么定义的。
multiset运行过程中,比较两个元素x,y的大小的做法,就是生成一个 Pred类型的变量,假定为 op,若表达式op(x,y) 返回值为true,则 x比y小。Pred的缺省类型是 less<Key>。
less 模板的定义:
template<class T>
struct less : public binary_function<T, T, bool>
{ bool operator()(const T& x, const T& y) { return x < y ; } const; };//less模板是靠 < 来比较大小的
multiset的成员函数
iterator find(const T & val);在容器中查找值为val的元素,返回其迭代器。如果找不到,返回end()。
iterator insert(const T & val); 将val插入到容器中并返回其迭代器。
void insert( iterator first,iterator last); 将区间[first,last)插入容器。
int count(const T & val); 统计有多少个元素的值和val相等。
iterator lower_bound(const T & val);查找一个最大的位置 it,使得[begin(),it) 中所有的元素都比 val 小。
iterator upper_bound(const T & val);查找一个最小的位置 it,使得[it,end()) 中所有的元素都比 val 大。
pair<iterator,iterator> equal_range(const T & val);同时求得lower_bound和upper_bound。
iterator erase(iterator it);删除it指向的元素,返回其后面的元素的迭代器(Visual studio 2010上如此,但是在C++标准和Dev C++中,返回值不是这样)。
multiset <A> a;就等价于multiset<A, less<A>> a;
插入元素时, multiset会将被插入元素和已有元素进行比较。由于less模板是用 < 进行比较的,所以,这都要求 A 的对象能用 < 比较,即适当重载了 <。
[b]2.set[/b]
template<class Key, class Pred = less<Key>,class A = allocator<Key> >class set { … }
插入set中已有的元素时,忽略插入。
[b]3.multimap[/b]
template<class Key, class T, class Pred = less<Key>,class A = allocator<T> >
class multimap {
….
typedef pair<const Key, T> value_type;
…….
}; //Key 代表关键字的类型
multimap中的元素由 <关键字,值>组成,每个元素是一个pair对象,关键字就是first成员变量,其类型是Key
multimap 中允许多个元素的关键字相同。元素按照first成员变量从小到大排列,缺省情况下用 less<Key> 定义关键字的“小于”关系。
一个学生成绩录入和查询系统,接受以下两种输入:
Add name id score
Query score
name是个字符串,中间没有空格,代表学生姓名。 id是个整数,代表学号。 score是个整数,表示分数。学号不会重复,分数和姓名都可能重复。两种输入交替出现。第一种输入表示要添加一个学生的信息,碰到这种输入,就记下学生的姓名、 id和分数。第二种输入表示要查询,碰到这种输入,就输出已有记录中分数比score低的最高分获得者的姓名、学号和分数。如果有多个学生都满足条件,就输出学号最大的那个学生的信息。如果找不到满足条件的学生,则输出“ Nobody”。
输入样例:
Add Jack 12 78
Query 78
Query 81
Add Percy 9 81
Add Marry 8 81
Query 82
Add Tom 11 79
Query 80
Query 81
输出果样例:
Nobody
Jack 12 78
Percy 9 81
Tom 11 79
Tom 11 79
[b]4.map[/b]
template<class Key, class T, class Pred = less<Key>,class A = allocator<T> >
class map {
….
typedef pair<const Key, T> value_type;
…….
};
map 中的元素都是pair模板类对象。 关键字(first成员变量)各不相同。元素按照关键字从小到大排列,缺省情况下用 less<Key>,即“ <” 定义“小于”。
若pairs为map模版类的对象,pairs[key]返回对关键字等于key的元素的值(second成员变量)的引用。若没有关键字为key的元素,则会往pairs里插入一个关键字为key的元素,其值用无参构造函数初始化,并返回其值的引用。
如:
map<int,double> pairs;
则
pairs[50] = 5; 会修改pairs中关键字为50的元素,使其值变成5。
若不存在关键字等于50的元素,则插入此元素,并使其值变为5。
set, multiset, map, multimap
内部元素有序排列,新元素插入的位置取决于它的值,查找速度快。
除了各容器都有的函数外,还支持以下成员函数:
find: 查找等于某个值 的元素(x小于y和y小于x同时不成立即为相等)
lower_bound : 查找某个下界
upper_bound : 查找某个上界
equal_range : 同时查找上界和下界
count :计算等于某个值的元素个数(x小于y和y小于x同时不成立即为相等)
insert: 用以插入一个元素或一个区间
预备知识: pair 模板
template<class _T1, class _T2> struct pair { typedef _T1 first_type; typedef _T2 second_type; _T1 first; _T2 second; pair() : first(), second() { } pair(const _T1& __a, const _T2& __b) : first(__a), second(__b) { } template<class _U1, class _U2> pair(const pair<_U1, _U2>& __p) : first(__p.first), second(__p.second) { } };
map/multimap容器里放着的都是pair模版类的对象,且按first从小到大排序
第三个构造函数用法示例:
pair<int,int>
p(pair<double,double>(5.5,4.6));
// p.first = 5, p.second = 4
[b]1.multiset[/b]
template<class Key, class Pred = less<Key>, class A = allocator<Key> > class multiset { …… };
Pred类型的变量决定了multiset 中的元素,“一个比另一个小”是怎么定义的。
multiset运行过程中,比较两个元素x,y的大小的做法,就是生成一个 Pred类型的变量,假定为 op,若表达式op(x,y) 返回值为true,则 x比y小。Pred的缺省类型是 less<Key>。
less 模板的定义:
template<class T>
struct less : public binary_function<T, T, bool>
{ bool operator()(const T& x, const T& y) { return x < y ; } const; };//less模板是靠 < 来比较大小的
multiset的成员函数
iterator find(const T & val);在容器中查找值为val的元素,返回其迭代器。如果找不到,返回end()。
iterator insert(const T & val); 将val插入到容器中并返回其迭代器。
void insert( iterator first,iterator last); 将区间[first,last)插入容器。
int count(const T & val); 统计有多少个元素的值和val相等。
iterator lower_bound(const T & val);查找一个最大的位置 it,使得[begin(),it) 中所有的元素都比 val 小。
iterator upper_bound(const T & val);查找一个最小的位置 it,使得[it,end()) 中所有的元素都比 val 大。
pair<iterator,iterator> equal_range(const T & val);同时求得lower_bound和upper_bound。
iterator erase(iterator it);删除it指向的元素,返回其后面的元素的迭代器(Visual studio 2010上如此,但是在C++标准和Dev C++中,返回值不是这样)。
#include <set> using namespace std; class A { }; int main() { multiset<A> a; a.insert( A()); //error }
multiset <A> a;就等价于multiset<A, less<A>> a;
插入元素时, multiset会将被插入元素和已有元素进行比较。由于less模板是用 < 进行比较的,所以,这都要求 A 的对象能用 < 比较,即适当重载了 <。
#include <iostream> #include <set> //使用multiset须包含此文件 using namespace std; template <class T> void Print(T first, T last) { for(;first != last ; ++first) cout << * first << " "; cout << endl; } class A { private: int n; public: A(int n_ ) { n = n_; } friend bool operator< ( const A & a1, const A & a2 ) { return a1.n < a2.n; } friend ostream & operator<< ( ostream & o, const A & a2 ) { o << a2.n; return o; } friend class MyLess; }; struct MyLess { bool operator()( const A & a1, const A & a2) //按个位数比大小 { return ( a1.n % 10 ) < (a2.n % 10); } }; typedef multiset<A> MSET1; //MSET1用 "<"比较大小 typedef multiset<A,MyLess> MSET2; //MSET2用 MyLess::operator()比较大小 int main() { const int SIZE = 6; A a[SIZE] = { 4,22,19,8,33,40 }; MSET1 m1; m1.insert(a,a+SIZE); m1.insert(22); cout << "1) " << m1.count(22) << endl; //输出 1) 2 cout << "2) "; Print(m1.begin(),m1.end()); //输出 2) 4 8 19 22 22 33 40 //m1元素: 4 8 19 22 22 33 40 MSET1::iterator pp = m1.find(19); if( pp != m1.end() ) //条件为真说明找到 cout << "found" << endl; //本行会被执行,输出 found cout << "3) "; cout << * m1.lower_bound(22) << "," <<* m1.upper_bound(22)<< endl; //输出 3) 22,33 pp = m1.erase(m1.lower_bound(22),m1.upper_bound(22)); //pp指向被删元素的下一个元素 cout << "4) "; Print(m1.begin(),m1.end()); //输出 4) 4 8 19 33 40 cout << "5) "; cout << * pp << endl; //输出 5) 33 MSET2 m2; // m2里的元素按n的个位数从小到大排 m2.insert(a,a+SIZE); cout << "6) "; Print(m2.begin(),m2.end()); //输出 6) 40 22 33 4 8 19 //m1元素: 4 8 19 22 22 33 40 pp = m1.find(19); if( pp != m1.end() ) //条件为真说明找到 cout << "found" << endl; //本行会被执行,输出 found cout << "3) "; cout << * m1.lower_bound(22) << "," <<* m1.upper_bound(22)<< endl; //输出 3) 22,33 pp = m1.erase(m1.lower_bound(22),m1.upper_bound(22)); //pp指向被删元素的下一个元素 cout << "4) "; Print(m1.begin(),m1.end()); //输出 4) 4 8 19 33 40 cout << "5) "; cout << * pp << endl; //输出 5) 33 MSET2 m3; // m2里的元素按n的个位数从小到大排 m3.insert(a,a+SIZE); cout << "6) "; Print(m3.begin(),m3.end()); //输出 6) 40 22 33 4 8 19 return 0; } //iterator lower_bound(const T & val); //查找一个最大的位置 it,使得[begin(),it) 中所有的元素都比 val 小。 //输出: //1) 2 //2) 4 8 19 22 22 33 40 //3) 22,33 //4) 4 8 19 33 40 //5) 33 //6) 40 22 33 4 8 19
[b]2.set[/b]
template<class Key, class Pred = less<Key>,class A = allocator<Key> >class set { … }
插入set中已有的元素时,忽略插入。
#include <iostream> #include <set> using namespace std; int main() { typedef set<int>::iterator IT; int a[5] = { 3,4,6,1,2 }; set<int> st(a,a+5); // st里是 1 2 3 4 6 pair< IT,bool> result; result = st.insert(5); // st变成 1 2 3 4 5 6 if( result.second ) //插入成功则输出被插入元素 cout << * result.first << " inserted" << endl; //输出: 5 inserted if( st.insert(5).second ) cout << * result.first << endl; else cout << * result.first << " already exists" << endl; //输出 5 already exists pair<IT,IT> bounds = st.equal_range(4); cout << * bounds.first << "," << * bounds.second ; //输出: 4,5 return 0; } //set用法示例 //输出结果: //5 inserted //5 already exists //4,5
[b]3.multimap[/b]
template<class Key, class T, class Pred = less<Key>,class A = allocator<T> >
class multimap {
….
typedef pair<const Key, T> value_type;
…….
}; //Key 代表关键字的类型
multimap中的元素由 <关键字,值>组成,每个元素是一个pair对象,关键字就是first成员变量,其类型是Key
multimap 中允许多个元素的关键字相同。元素按照first成员变量从小到大排列,缺省情况下用 less<Key> 定义关键字的“小于”关系。
#include <iostream> #include <map> using namespace std; int main() { typedef multimap<int,double,less<int> > mmid; mmid pairs; cout << "1) " << pairs.count(15) << endl; pairs.insert(mmid::value_type(15,2.7));//typedef pair<const Key, T> value_type; pairs.insert(mmid::value_type(15,99.3)); cout << "2) " << pairs.count(15) << endl; //求关键字等于某值的元素个数 pairs.insert(mmid::value_type(30,111.11)); pairs.insert(mmid::value_type(10,22.22)); //输出: //1) 0 //2) 2 pairs.insert(mmid::value_type(25,33.333)); pairs.insert(mmid::value_type(20,9.3)); for( mmid::const_iterator i = pairs.begin();i != pairs.end() ;i ++ ) cout << "(" << i->first << "," << i->second << ")" << ","; } //输出: //1) 0 //2) 2 //(10,22.22),(15,2.7),(15,99.3),(20,9.3),(25,33.333),(30,111.11)
一个学生成绩录入和查询系统,接受以下两种输入:
Add name id score
Query score
name是个字符串,中间没有空格,代表学生姓名。 id是个整数,代表学号。 score是个整数,表示分数。学号不会重复,分数和姓名都可能重复。两种输入交替出现。第一种输入表示要添加一个学生的信息,碰到这种输入,就记下学生的姓名、 id和分数。第二种输入表示要查询,碰到这种输入,就输出已有记录中分数比score低的最高分获得者的姓名、学号和分数。如果有多个学生都满足条件,就输出学号最大的那个学生的信息。如果找不到满足条件的学生,则输出“ Nobody”。
输入样例:
Add Jack 12 78
Query 78
Query 81
Add Percy 9 81
Add Marry 8 81
Query 82
Add Tom 11 79
Query 80
Query 81
输出果样例:
Nobody
Jack 12 78
Percy 9 81
Tom 11 79
Tom 11 79
#include <iostream> #include <map> //使用multimap需要包含此头文件 #include <string> using namespace std; class CStudent { public: struct CInfo //类的内部还可以定义类 { int id; string name; }; int score; CInfo info; //学生的其他信息 }; typedef multimap<int, CStudent::CInfo> MAP_STD; int main() { MAP_STD mp; CStudent st; string cmd; while( cin >> cmd ) { if( cmd == "Add") { cin >> st.info.name >> st.info.id >> st.score ; mp.insert(MAP_STD::value_type(st.score,st.info ));//mp.insert(make_pair(st.score,st.info )); 也可以 } else if( cmd == "Query" ){ int score; cin >> score; MAP_STD::iterator p = mp.lower_bound (score); if( p!= mp.begin()) { --p; score = p->first; //比要查询分数低的最高分 MAP_STD::iterator maxp = p; int maxId = p->second.id; //iterator lower_bound(const T & val); //查找一个最大的位置 it,使得[begin(),it) 中所有元素的first都比 val 小。 for( ; p != mp.begin() && p->first ==score; --p) { //遍历所有成绩和score相等的学生 if( p->second.id > maxId ) { maxp = p; maxId = p->second.id ; } } if( p->first == score) { //如果上面循环是因为 p == mp.begin() // 而终止,则p指向的元素还要处理 if( p->second.id > maxId ) { maxp = p; maxId = p->second.id ; } } cout << maxp->second.name << " " << maxp->second.id << " " << maxp->first << endl; } else //lower_bound的结果就是 begin,说明没人分数比查询分数低 cout << "Nobody" << endl; } } return 0; }
[b]4.map[/b]
template<class Key, class T, class Pred = less<Key>,class A = allocator<T> >
class map {
….
typedef pair<const Key, T> value_type;
…….
};
map 中的元素都是pair模板类对象。 关键字(first成员变量)各不相同。元素按照关键字从小到大排列,缺省情况下用 less<Key>,即“ <” 定义“小于”。
若pairs为map模版类的对象,pairs[key]返回对关键字等于key的元素的值(second成员变量)的引用。若没有关键字为key的元素,则会往pairs里插入一个关键字为key的元素,其值用无参构造函数初始化,并返回其值的引用。
如:
map<int,double> pairs;
则
pairs[50] = 5; 会修改pairs中关键字为50的元素,使其值变成5。
若不存在关键字等于50的元素,则插入此元素,并使其值变为5。
#include <iostream> #include <map> using namespace std; template <class Key,class Value> ostream & operator <<( ostream & o, const pair<Key,Value> & p) { o << "(" << p.first << "," << p.second << ")"; return o; } int main() { typedef map<int, double,less<int> > mmid; mmid pairs; cout << "1) " << pairs.count(15) << endl; pairs.insert(mmid::value_type(15,2.7)); pairs.insert(make_pair(15,99.3)); //make_pair生成一个pair对象 cout << "2) " << pairs.count(15) << endl; pairs.insert(mmid::value_type(20,9.3)); mmid::iterator i; cout << "3) "; for( i = pairs.begin(); i != pairs.end();i ++ ) cout << * i << ","; cout << endl; //输出: // 1) 0 // 2) 1 // 3) (15,2.7),(20,9.3), // 20 cout << "4) "; int n = pairs[40];//如果没有关键字为40的元素,则插入一个 for( i = pairs.begin(); i != pairs.end();i ++ ) cout << * i << ","; cout << endl; cout << "5) "; pairs[15] = 6.28; //把关键字为15的元素值改成6.28 for( i = pairs.begin(); i != pairs.end();i ++ ) cout << * i << ","; } //输出: //1) 0 //2) 1 //3) (15,2.7),(20,9.3), //4) (15,2.7),(20,9.3),(40,0), //5) (15,6.28),(20,9.3),(40,0),
相关文章推荐
- 20145313张雪纯 《Java程序设计》第5周学习总结
- 使用iptables作为网络防火墙构建安全的网络环境
- 【Codeforces659G】Fence Divercity【数学】【计数DP?】
- tornado 模板总结
- 技术记录----开机向导的问题解决以及流程整理
- SPOJ GSS5 Can you answer these queries V(区间合并)
- UIView的alpha、hidden和opaque属性之间的关系和区别
- 跟我一起学习MySQL技术内幕(第五版):(第一章学习日记5)
- 【蓝桥杯】历届试题 打印十字图
- AFNetworking 3.0.4 的使用
- UVALive 7511 Multiplication Table (数学模拟题)
- V4L2摄像头驱动移植
- 有关JSTL核心core标签库的学习
- 从环信拉取消息记录,写入本地数据库
- POJ-3274 Gold Balanced Lineup
- 【NOIP 2004】虫食算
- 下载youtube视频的网站
- #测试两种不同的SVM,rbf的核真是太棒了(一种会拐弯的边界)
- 在hw的这一年
- 织梦增加广告不显示.无法预览原因