STL 容器概念(联合容器)

联合容器
//////////////////////////////////////////////
#include <set> set   multiset   #include <map> map  multimap
值与关键字联合在一起 提供了队元素的快速访问 允许插入新元素 但不允许指定插入位子

************************************************************
1>set
值与关键字相同 关键字唯一 可反转 可排序
set<string> A;
set<string,less<string> > A;  less<string>指定队关键字排序的比较函数或对象

通用函数(显示A与B的并集)
set_union(A.begin(),A.end(),B.begin(),b.end(),ostream_iterator<string,char>out (cout," "));

(将A与B的并集放入C)(insert_iterator可以将复制转为插入 并 模拟输入迭代器概念)
set_union(A.begin(),A.end(),B.begin(),b.end(),insert_itrtator<set<string> >(C,C.begin()));

set_intersection()查找交集 //(接口上同)

set_difference()获得两个集合的差 //(接口上同)

lower_bound() 将关键字代入获得第一个不小于关键字的迭代

upper_bound() 将关键字代入获得第一个大于关键字的迭代

// setops.cpp -- some set operations
#include <iostream>
#include <string>
#include <set>
#include <algorithm>
#include <iterator>

int main()
{
    using namespace std;
    const int N = 6;
    string s1[N] = {"buffoon", "thinkers", "for", "heavy", "can", "for"};
    string s2[N] = {"metal", "any", "food", "elegant", "deliver","for"};

    set<string> A(s1, s1 + N);
    set<string> B(s2, s2 + N);

    ostream_iterator<string, char> out(cout, " ");
    cout << "Set A: ";
    copy(A.begin(), A.end(), out);
    cout << endl;
    cout << "Set B: ";
    copy(B.begin(), B.end(), out);
    cout << endl;

    cout << "Union of A and B:\n";
    set_union(A.begin(), A.end(), B.begin(), B.end(), out);
    cout << endl;

    cout << "Intersection of A and B:\n";
    set_intersection(A.begin(), A.end(), B.begin(), B.end(), out);
    cout << endl;

    cout << "Difference of A and B:\n";
    set_difference(A.begin(), A.end(), B.begin(), B.end(), out);
    cout << endl;

    set<string> C;
    cout << "Set C:\n";
    set_union(A.begin(), A.end(), B.begin(), B.end(),
        insert_iterator<set<string> >(C, C.begin()));
    copy(C.begin(), C.end(), out);
    cout << endl;

    string s3("grungy");
    C.insert(s3);
    cout << "Set C after insertion:\n";
    copy(C.begin(), C.end(),out);
    cout << endl;

    cout << "Showing a range:\n";
    copy(C.lower_bound("ghost"),C.upper_bound("spook"), out);
    cout << endl;

    return 0;
}

******************************************************************************
2>multiset
类似set  关键字可以重复

******************************************************************************

3>map
类似multimap 关键字对应唯一值

******************************************************************************

4>multimap
关键字对应多个值 可以反转  经过排序的容器 关键字与值的类型不同
multimap<int,string> codes;  //int关键字类型  string值类型
multimap<int,string,less<int>()> //less<int>() 指定关键字排序的方式

为将值与关键字集合 使用 pair<class T,class U>
ep:
pair<const int,string> item(213,"los Angeles");
cout<<item.first<<" "<<item.second<<endl;


// multmap.cpp -- use a multimap
#include <iostream>
#include <string>
#include <map>
#include <algorithm>

typedef int KeyType;
typedef std::pair<const KeyType, std::string> Pair;
typedef std::multimap<KeyType, std::string> MapCode;

int main()
{
    using namespace std;
    MapCode codes;

    codes.insert(Pair(415, "San Francisco"));
    codes.insert(Pair(510, "Oakland"));
    codes.insert(Pair(718, "Brooklyn"));
    codes.insert(Pair(718, "Staten Island"));
    codes.insert(Pair(415, "San Rafael"));
    codes.insert(Pair(510, "Berkeley"));

    cout << "Number of cities with area code 415: "
         << codes.count(415) << endl;
    cout << "Number of cities with area code 718: "
         << codes.count(718) << endl;
    cout << "Number of cities with area code 510: "
         << codes.count(510) << endl;
    cout << "Area Code   City\n";
    MapCode::iterator it;
    for (it = codes.begin(); it != codes.end(); ++it)
        cout << "    " << (*it).first << "     "
            << (*it).second    << endl;

    pair<MapCode::iterator, MapCode::iterator> range
         = codes.equal_range(718);
    cout << "Cities with area code 718:\n";
    for (it = range.first; it != range.second; ++it)
        cout <<  (*it).second    << endl;
   
    cin.get();
    return 0;
}

posted @ 2007-03-18 15:41  Edward Xie  阅读(529)  评论(0)    收藏  举报