Map,Multimap,Set,MultiSet,Hash_Map,Hash_Set,Share_ptr的区分

#include <map>
#include <hash_map>
#include  <set>
#include  <hash_set>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;


class Point
{
private:
    float x,y;
public:
    Point(float x=0,float y=0)
    {
        this->x =x;
        this->y=y;
    }
    void printf()
    {
        cout << this->x  <<" "<< this->y <<endl;
    }
};

 int main( )
{
    /*Map的底层是用平衡二叉树实现的,迭代器输出的时候是已经有序的*/
    map <int, string> m1;
    map <int, string>::iterator m1_Iter;
    m1.insert ( pair <int, string> ( 11, "OK" ) );
    m1.insert ( pair <int, string> ( 12, "I think" ) );
    m1.insert ( pair <int, string> ( 13, "just do it" ) );
    m1.insert ( pair <int, string> ( 4, "oh no") );
    m1.insert ( pair <int, string> ( 25, "work hard" ) );
    m1.insert ( pair <int, string> ( 6, "funny" ) );
    cout << "The original map m1 is:"<<endl;
    for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
        cout << m1_Iter->first<<" "<<m1_Iter->second<<endl;

    m1_Iter=m1.find(13);
    cout << m1_Iter->first<<" "<<m1_Iter->second<<endl;
    m1_Iter=m1.find(6);
    cout << m1_Iter->first<<" "<<m1_Iter->second<<endl;

    /*Hash_map 是用哈希表来实现的,所以用迭代器输出的时候是无序的*/
    hash_map<int, string>  hash_map1;
    hash_map1.insert(pair<int,string>(67,"Proud"));
    hash_map1.insert(pair<int,string>(16,"understanding"));
    hash_map1.insert(pair<int,string>(26,"tree"));
    hash_map1.insert(pair<int,string>(12,"you are a boy"));
    hash_map<int,string>::iterator hashmap1_Iter;
    for(hashmap1_Iter=hash_map1.begin();hashmap1_Iter!=hash_map1.end();hashmap1_Iter++)
        cout << hashmap1_Iter->first <<" "<<hashmap1_Iter->second<<endl;

    hashmap1_Iter=hash_map1.find(67);
    cout << hashmap1_Iter->first << " " <<hashmap1_Iter->second<<endl;

    /*map与multimap不同之处在于map不允许相同的KEY,而multimap允许有相同的KEY,并且实现的数据结构都是平衡二叉树,所以是有序的
    multimap <int, string> mu1;
    multimap <int, string>::iterator mu1_Iter;
    mu1.insert ( pair <int, string> ( 11, "OK" ) );
    mu1.insert ( pair <int, string> ( 12, "I think" ) );
    mu1.insert ( pair <int, string> ( 12, "Good Example" ) );
    mu1.insert ( pair <int, string> ( 13, "just do it" ) );
    mu1.insert ( pair <int, string> ( 4, "oh no") );
    mu1.insert ( pair <int, string> ( 25, "work hard" ) );
    mu1.insert ( pair <int, string> ( 25, "persistance" ) );
    mu1.insert ( pair <int, string> ( 6, "funny" ) );
    cout << "The original map m1 is:"<<endl;
    for ( mu1_Iter = mu1.begin( ); mu1_Iter != mu1.end( ); mu1_Iter++ )
        cout << mu1_Iter->first<<" "<<mu1_Iter->second<<endl;

    mu1_Iter=mu1.find(13);
    cout << mu1_Iter->first<<" "<<mu1_Iter->second<<endl;
    mu1_Iter=mu1.find(6);
    cout << mu1_Iter->first<<" "<<mu1_Iter->second<<endl;


    /*Set使用的数据结构也是平衡二叉树,只有Value*/
    set<string>   s1;
    set<string>::iterator s1_Iter;
    s1.insert(string("orange"));
    s1.insert(string("apple"));
    s1.insert(string("banana"));
    s1.insert(string("fruit"));
    for(s1_Iter=s1.begin();s1_Iter!=s1.end();s1_Iter++)
        cout << *s1_Iter<<endl;

    set<string>   s2;
    set<string>::iterator s2_Iter;
    s2.insert(string("orange"));
    s2.insert(string("potato"));
    s2.insert(string("grass"));
    s2.insert(string("fruit"));
    for(s2_Iter=s2.begin();s2_Iter!=s2.end();s2_Iter++)
        cout << *s2_Iter<<endl;
    //支持集合的操作
    string str[10];
    string  *first=str;
    string *end=std::set_intersection(s1.begin(),s1.end(),s2.begin(),s2.end(),str);
    cout<<endl;
    while(first<end)
        cout<<*first++<<endl;

    string str2[10];
    first=str2;
    end=std::set_union(s1.begin(),s1.end(),s2.begin(),s2.end(),str2);
    cout<<endl;
    while(first<end)
        cout<<*first++<<endl;

    multiset<string>   ms1;
    multiset<string>::iterator ms1_Iter;
    ms1.insert(string("orange"));
    ms1.insert(string("apple"));
    ms1.insert(string("banana"));
    ms1.insert(string("fruit"));
    ms1.insert(string("orange"));
    ms1.insert(string("apple"));
    ms1.insert(string("banana"));
    ms1.insert(string("fruit"));
    for(ms1_Iter=ms1.begin();ms1_Iter!=ms1.end();ms1_Iter++)
        cout << *ms1_Iter<<endl;
/*实现的还是哈希表,输出状态无序*/ hash_set
<string> hs2; hash_set<string>::iterator hs2_Iter; hs2.insert(string("orange")); hs2.insert(string("potato")); hs2.insert(string("grass")); hs2.insert(string("fruit")); cout <<" Hash Set:"<<endl; for(hs2_Iter=hs2.begin();hs2_Iter!=hs2.end();hs2_Iter++) cout << *hs2_Iter<<endl;
//共享指针管理对象,防范内存泄漏 std::shared_ptr
<Point> point1(new Point(1.0,1.0)); point1->printf(); }
posted @ 2012-05-23 22:58  苍术厚朴  阅读(774)  评论(0编辑  收藏  举报