4.2 set和multiset

使用必须包含头文件set

1)multiset

*:定义

如果不给第二个参数,默认less<key>,即用<来进行。

例如:

A是一个类的名字,则可以定义一个容器对象如下:

multiset<A>s;

由于multiset 的类型参数可以默认,所以上面的语句等价于:

multiset<int, less<A>,allocator<A>s>;

**:multiset的成员函数

find和count并不是通过==来进行比较值是否相等。它们的比较原则是x<y和y<x同时为假。

3)例

//program 19.4.2.1.cpp  multiset的用法
#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;  
};
class MyLess 
{
public:
    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
    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
    return 0;
}

2)set

 *:定义

template<class Key, class Pred=less<Key>,class A=allocator<Key>

class set {....}

set和multiset相似,差别在于set中不能有重复元素。multiset的成员函数set也有,但由于没有重复元素,所以set中插入单个元素的insert成员函数和multiset不同,其原型如下,返回一个pair模板类的对象。

pair<iterator,bool>insert(const T & val);

假如set的insert成员函数的返回值是对象x,那么x.second为ture,则说明插入成功,此时x.first指向被插入元素的迭代器;x.second为false,说明已经有一个一样的值,插入失败,x.first指向原有那个元素的迭代器。

关联容器的另一个成员函数equal_range返回值也是pair模板类对象:

pair<iterator,iterator>equal_range(const T & val);

2)例

//program 19.4.3.1.cpp  set的用法:
#include <iostream>
#include <set>  //使用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;
}
原文地址:https://www.cnblogs.com/by-dxm/p/5473531.html