set , mutiset , map , mutimap
- 内部元素有序排列,新元素插入的位置取决于它的值,查找速度快
- 除了各容器都有的函数以外,还支持一下函数
find:查找等于某个值的元素(x 小于 y 和 y 小于x 同时不成立即为相等)
low_bound: 查找某个下界
upper_bound: 查找某个上界
equal_range: 同时查找上界和下界
count: 计算等于某个值的元素的个数(x小于y和y小于x同时不成立即为相等)
insert: 用以插入一个元素或者一个区间
预备知识: pair模板
代码示例:
#include <iostream>
#include <set> //使用mutiset包含此头文件
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 mutiset<A> MSET1; //MSET1用<比较大小
typedef mutiset<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 << "m1.count(22)" << " " << m1.count( 22 ) << endl; // 2
cout << "print m1" << print( m1.begin() , m1.end() ) << endl;
// 4 8 19 22 22 33 40
MSET1::iterator pp = m1.find(19);
if( pp != m1.end() )
{
cout << "find" << *pp << endl;
}
cout << *m1.lower_bound( 22 ) << "," << *m1.upper_bound( 22 ) << endl;
// 22 33
//pp = m1.erase( m1.lower_bound( 22 ) , m1.upper_bound( 22 ) );
//这行代码没有测试出来
print( m1.begin() , m1.end() );// 4 8 19 33 40
cout << *pp << endl; // 33
MSET2 m2 ;// m2里面的元素按照n的个位数从小到大排列
m2.insert( a , a + SIZE );
print( m2.begin() , m2.end() ); // 40 20 33 4 8 19
return 0;
}
结果图片如下:
set
插入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 ); //变成1 2 3 4 5 6
if( result.second ) // 插入成功则输入被插入元素
{
cout << *result.first << "inserted" << endl;
}
if( ( result = st.insert( 5 ) ).second )
cout << *result.first << endl;
else
{
cout << *result.first << "already exist" << endl;// 5 alreadyexist
}
pair<IT , IT> bounds = st.equal_range( 4 );
cout << *bounds.first << " , " << *bounds.second << endl;
return 0 ;
}
结果图片如下:
mutimap
#include <cstdlib>
#include <iostream>
#include <map>
using namespace std;
int main(int argc, char *argv[])
{
typedef multimap< int , double , less<int> > mmid;
mmid pairs;
cout << "pairs.count( 15 )" << 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 << "pairs.count( 15)" << pairs.count( 15 ) << endl; //求关键字等于某值的元素个数
pairs.insert( mmid::value_type( 30 , 111.11) );
pairs.insert( mmid::value_type( 10 , 22.22 ));
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 << " ) " << endl;
cout << " " ;
}
system("PAUSE");
return EXIT_SUCCESS;
}
#include <cstdlib>
#include <iostream>
#include <map> //用 mutimap需要包含这个头文件
#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(int argc, char *argv[])
{
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 maxld = p->second.id;
for( ; p != mp.begin() && p->first == score ; p-- )
{ //遍历所有成绩和 score 相等的学生
if( p->second.id> maxld)
{
maxp = p;
maxld = p->second.id;
}
}
if( p->first == score )
{
//如果上面的循环是因为 p == mp.begin() 而终止 ,则 p 指向的元素还要处理
if( p->second.id > maxld )
{
maxp = p;
maxld = p->second.id;
}
}
cout << maxp->second.name << " " << maxp->second.id << " " << maxp->first << endl;
}
else
{
//查询结果就是 begin ,说明没人分数比 lower_bound低
cout << "nobody" << endl;
}
}
}
system("PAUSE");
return EXIT_SUCCESS;
}
实测结果如下
Map
Map的 [ ]成员函数
#include <cstdlib>
#include <iostream>
#include <map> //用 mutimap需要包含这个头文件
#include <string>
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(int argc, char *argv[])
{
typedef map< int , double , less<int> > mmid;
mmid pairs;
cout << "pairs.count(15)" << pairs.count( 15 ) << endl;
pairs.insert( mmid::value_type( 15 , 2.7 ) );
pairs.insert( make_pair( 15 , 99.3 ) ); // make_pair生成一个 pair 对象
cout << "pairs.count(15)" << pairs.count( 15 ) << endl;
pairs.insert( mmid::value_type( 20 , 9.3 ));
mmid::iterator i ;
cout << "print pairs" << endl;
for( i = pairs.begin() ; i != pairs.end() ; i++ )
{
cout << *i << " , ";
}
cout << endl;
int n = pairs[40]; //如果没有关键字为40的元素 ,则插入一个
for( i = pairs.begin() ; i != pairs.end() ; i++ )
{
cout << *i << " , ";
}
cout << endl;
pairs[15] = 6.28; //把关键字为15的元素改为6.28
for( i = pairs.begin() ; i != pairs.end() ; i++ )
{
cout << *i << " , ";
}
cout << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
结果如下: