【STL】 map

来源:互联网 发布:初中题库软件 编辑:程序博客网 时间:2024/05/16 00:58

1、说明:系统根据C++ Reference学习下STL--> Map

2、Map:Maps are associative containers that store elements formed by a combination of a key value and a mapped value, following a specific order. 就是说一个key(关键)值映射一个mapped(映射)值,并且按照欧一定的顺序排列。

(1) key value标记一个一个元素,mapped value通常用来key value对应的内容

(2) Key value和Mapped Value的类型可以不同,一般由

typedef pair<const Key, T> value_type来绑定。

3、Map的基本成员函数

(1) map::at  --> Returns a reference to the mapped value of the element identified with key k

    map<string,int> mymap = {        {"alpha", 0},        {"beta", 0},        {"gamma", 0}};    mymap.at("alpha") = 10;    mymap.at("beta") = 20;    mymap.at("gamma") = 30;    for(auto &x: mymap){        cout << x.first << ": " << x.second << endl;    }

(2)map::begin  --> Returns an iterator referring to the first element in the map container.

    map<char, int> mymap;    map<char, int>::iterator it;    mymap['b'] = 100;    mymap['a'] = 200;    mymap['c'] = 300;    for(map<char, int>::iterator it = mymap.begin(); it != mymap.end(); it ++)    {        cout << it->first << " => " << it->second << endl;    }

(3)map::cbegin  --> Returns a const_iterator pointing to the first element in the container.

    map<char, int> mymap;    mymap['b'] = 100;    mymap['a'] = 200;    mymap['c'] = 300;    cout << "mymap contains:\n";    for(auto it = mymap.cbegin(); it != mymap.cend(); it ++)    {        cout << "[" << (*it).first << ": " << (*it).second << "]\n";    }

     (4)map::clear  --> Removes all elements from the map container (which are destroyed), leaving the container with a size of 0.

       (5)map::count  --> Searches the container for elements with a key equivalent to k and returns the number of matches.  Map中的所有element都是唯一的。

    map<char, int> mymap;    char c;    mymap['a'] = 101;    mymap['b'] = 202;    mymap['f'] = 303;    for(c = 'a'; c < 'h'; c ++)    {        cout << c;        if(mymap.count(c) > 0)            cout << " is an element of mymap.\n";        else cout << " is not an element of mymap.\n";    }
     (6)map::crbegin  --> const_reverse_iterator to the reverse beginning of the sequence.

和cbegin的调用是一致的。

     (7)map::emplace  --> Inserts a new element in the map if its key is unique. This new element is constructed in place using args as the arguments for the construction of a value_type (which is an object of a pair type).  如果键值是唯一的,则插入成功。如果插入成功,则会依照原有的顺序插入到map中

    map<char, int> mymap;    mymap.emplace('x', 100);    mymap.emplace('y', 200);    mymap.emplace('z', 300);    cout << "mymap contains:\n";    for(auto &x: mymap){        cout << "[" << x.first << ", " << x.second << "]\n";    }

         (8)map::emplace_hint   --> Inserts a new element in the map if its key is unique, with a hint on the insertion position. This new element is constructed in place using args as the arguments for the construction of a value_type (which is an object of a pairtype).

    map<char, int> mymap;    auto it = mymap.end();    it = mymap.emplace_hint(it, 'b', 10);    mymap.emplace_hint(it, 'a', 12);    mymap.emplace_hint(mymap.end(), 'c', 14);    cout << "mymap contains: ";    for(auto &x: mymap)        cout << "[" << x.first << ", " << x.second << endl;
       (9)map::empty  --> Returns whether the map container is empty (i.e. whether its size is 0).


       (10)map::equal_range  --> Returns the bounds of a range that includes all the elements in the container which have a key equivalent to k.

    map<char, int> mymap;    mymap['a'] = 10;    mymap['b'] = 20;    mymap['c'] = 30;    pair<map<char, int>::iterator, map<char, int>::iterator> ret;    ret = mymap.equal_range('a');    cout << "lower bound points to: ";    cout << ret.first->first << " => " << ret.first->second << endl;    cout << "upper bound points to: ";    cout << ret.second->first << " => " << ret.second->second << endl;
       (11)map::erase  --> Removes from the map container either a single element or a range of elements ([first,last)).

    map<char, int> mymap;    map<char, int>::iterator it;    mymap['a'] = 10;    mymap['b'] = 20;    mymap['c'] = 30;    mymap['d'] = 40;    mymap['e'] = 50;    mymap['f'] = 60;    it = mymap.find('b');    mymap.erase(it);        //erasing by iterator, erase b    mymap.erase('c') ;       //erasing by key, erase c    it = mymap.find('e');    mymap.erase(it, mymap.end()) ;  //erasing by range, erase e, f    //输出a d    for(it = mymap.begin(); it != mymap.end(); ++ it)        cout << it->first << " => " << it->second << endl;
     (12)map::find  --> Searches the container for an element with a key equivalent to k and returns an iterator to it if found, otherwise it returns an iterator to map::end.

    std::map<char,int> mymap;    std::map<char,int>::iterator it;    mymap['a']=50;    mymap['b']=100;    mymap['c']=150;    mymap['d']=200;    it=mymap.find('b');    mymap.erase (it);    mymap.erase (mymap.find('d'));    // print content:    std::cout << "elements in mymap:" << '\n';    std::cout << "a => " << mymap.find('a')->second << '\n';    std::cout << "c => " << mymap.find('c')->second << '\n';

     (13)map::get_allocate  --> Returns a copy of the allocator object associated with the map.

    int psize;    map<char, int> mymap;    pair<const char, int> *p;    //allocate an array of 5 elements using mymap's allocater.    p = mymap.get_allocator().allocate(5);    //assign some values to array    psize = sizeof(map <char, int>::value_type) * 5;    cout << "the allocated array has a size of " << psize << " bytes\n";    mymap.get_allocator().deallocate(p, 5);

      (14)map::insert  --> Extends the container by inserting new elements, effectively increasing the container size by the number of elements inserted.  插入时会检查是否已经存在和当前键值一样的元素。

    map<char, int> mymap;    //first insert function version(single parameter)    mymap.insert(pair<char, int>('a', 200));    mymap.insert(pair<char, int>('z', 200));    pair<map<char, int>::iterator, bool> ret;    ret = mymap.insert(pair<char, int>('z', 500));    if(ret.second == false){        cout << "element '" << ret.first->first << "' already exists";        cout << " with a value of " << ret.first->second << endl;    }    //second insert function version(with hint position)    map<char, int>::iterator it = mymap.begin();    mymap.insert(it, pair<char, int>('b', 300));    mymap.insert(it, pair<char, int>('c', 400));    //third insert function version(range insertion);    map<char, int> anothermap;    anothermap.insert(mymap.begin(), mymap.find('c'));    //showing contents    cout << "mymap contains:\n";    for(it = mymap.begin(); it != mymap.end(); ++it)        cout << it->first << " => " << it->second << endl;    cout << "anothermap contains:\n";    for(it = anothermap.begin(); it != anothermap.end(); ++ it)        cout << it->first << " => " << it->second << endl;

    (15)map::key_comp  --> Returns a copy of the comparison object used by the container to compare keys.

    map<char, int> mymap;    map<char, int>::key_compare mycomp = mymap.key_comp();    mymap['b'] = 400;    mymap['e'] = 500;    mymap['f'] = 300;    mymap['a'] = 100;    cout << "mymap contains:\n";    //key value of last element    char highest = mymap.rbegin()->first;    map<char, int>::iterator it = mymap.begin();    do{        cout << it->first << " => " << it->second << endl;    }while(mycomp((*it ++).first, highest));    cout << endl;


     (16)map::lower_bound  -> Returns an iterator pointing to the first element in the container whose key is not considered to go before k (i.e., either it is equivalent or goes after).  he mapcontains an element with a key equivalent to k: In this case, lower_bound returns an iterator pointing to that element, whereas upper_bound returns an iterator pointing to the next element.

    map<char, int> mymap;    map<char, int>::iterator itlow, itup;    mymap['a'] = 20;    mymap['b'] = 40;    mymap['c'] = 60;    mymap['d'] = 80;    mymap['e'] = 100;    //itlow points to b    itlow = mymap.lower_bound('a');    //itup points to e(not 'd')    itup = mymap.upper_bound('d');    //erase [itlow, itup)    mymap.erase(itlow, itup);    for(map<char, int>::iterator it = mymap.begin(); it != mymap.end(); ++ it)    {        cout << it->first << " => " << it->second << endl;    }
     (17)map::max_size  --> Returns the maximum number of elements that the map container can hold.
    map<int, int> mymap;    cout << mymap.max_size() << endl;    if(mymap.max_size() > 1000){        for(int i = 0; i < 1000; i ++)            mymap[i] = 0;        cout << "The map contains 1000 elements" << endl;;    }    else cout << "The map could not hold 1000 elements." << endl;
   

   (18)map::operator=  --> Assigns new contents to the container, replacing its current content.

    map<char, int> first;    map<char, int> second;    first['x'] = 8;    first['y'] = 16;    first['z'] = 32;    second = first;    second.insert(pair<char, int>('a', 100));    first = second;    cout << "Size of first: " << first.size() << endl;    cout << "Size of second: " << second.size() << endl;    first = map<char, int>();    cout << "Size of first: " << first.size() << endl;    cout << "Size of second: " << second.size() << endl;


    (19)map::operator[]  --> A reference to the mapped value of the element with a key value equivalent to k.

    map<char, string> mymap;    mymap['a'] = "an element";    mymap['b'] = "another elment";    mymap['c'] = mymap['b'];    cout << "mymap['a'] is " << mymap['a'] << endl;    cout << "mymap['b'] is " << mymap['b'] << endl;    cout << "mymap['c'] is " << mymap['c'] << endl;    cout << "mymap now contains " << mymap.size() << endl;

    (20)map::rbegin  --> Returns a reverse iterator pointing to the last element in the container (i.e., its reverse beginning).

    map<char, int> mymap;    mymap['x'] = 100;    mymap['y'] = 200;    mymap['z'] = 300;    map<char, int>::reverse_iterator rit;    for(rit=mymap.rbegin(); rit != mymap.rend(); ++rit){        cout << rit->first << " => " << rit->second << endl;    }


    (21)map::size  -> Returns the number of elements in the map container.

  

    (22)map::value_comp  --> Returns a comparison object that can be used to compare two elements to get whether the key of the first one goes before the second.


    (23)map::swap  --> Exchanges the content of the container by the content of x, which is another map of the same type. Sizes may differ.  需要是同种类型的

    map<char, int> foo, bar;    foo['x'] = 100;    foo['y'] = 200;    bar['a'] = 11;    bar['b'] = 22;    bar['c'] = 33;    foo.swap(bar);    cout << "foo contains: \n";    for(map<char, int>::iterator it = foo.begin(); it != foo.end(); ++it)        cout << it->first << " => " << it->second << endl;    cout << "bar contains: \n";    for(map<char, int>::iterator it = bar.begin(); it != bar.end(); ++it)        cout << it->first << " => " << it->second << endl;













0 0