Map

来源:互联网 发布:snh48赵嘉敏退团 知乎 编辑:程序博客网 时间:2024/05/17 06:44

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

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. map<string,int> mymap = {  
  2.     {"alpha", 0},  
  3.     {"beta", 0},  
  4.     {"gamma", 0}};  
  5.   
  6.   
  7. mymap.at("alpha") = 10;  
  8. mymap.at("beta") = 20;  
  9. mymap.at("gamma") = 30;  
  10.   
  11. for(auto &x: mymap){  
  12.     cout << x.first << ": " << x.second << endl;  
  13. }  

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

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. map<charint> mymap;  
  2. map<charint>::iterator it;  
  3.   
  4. mymap['b'] = 100;  
  5. mymap['a'] = 200;  
  6. mymap['c'] = 300;  
  7.   
  8. for(map<charint>::iterator it = mymap.begin(); it != mymap.end(); it ++)  
  9. {  
  10.     cout << it->first << " => " << it->second << endl;  
  11. }  

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

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. map<charint> mymap;  
  2.   
  3. mymap['b'] = 100;  
  4. mymap['a'] = 200;  
  5. mymap['c'] = 300;  
  6.   
  7. cout << "mymap contains:\n";  
  8. for(auto it = mymap.cbegin(); it != mymap.cend(); it ++)  
  9. {  
  10.     cout << "[" << (*it).first << ": " << (*it).second << "]\n";  
  11. }  

     (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都是唯一的。

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. map<charint> mymap;  
  2. char c;  
  3.   
  4. mymap['a'] = 101;  
  5. mymap['b'] = 202;  
  6. mymap['f'] = 303;  
  7.   
  8. for(c = 'a'; c < 'h'; c ++)  
  9. {  
  10.     cout << c;  
  11.     if(mymap.count(c) > 0)  
  12.         cout << " is an element of mymap.\n";  
  13.     else cout << " is not an element of mymap.\n";  
  14. }  
     (6)map::crbegin  --> A 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中

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. map<charint> mymap;  
  2. mymap.emplace('x', 100);  
  3. mymap.emplace('y', 200);  
  4. mymap.emplace('z', 300);  
  5.   
  6. cout << "mymap contains:\n";  
  7. for(auto &x: mymap){  
  8.     cout << "[" << x.first << ", " << x.second << "]\n";  
  9. }  

         (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).

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. map<charint> mymap;  
  2. auto it = mymap.end();  
  3.   
  4. it = mymap.emplace_hint(it, 'b', 10);  
  5. mymap.emplace_hint(it, 'a', 12);  
  6. mymap.emplace_hint(mymap.end(), 'c', 14);  
  7.   
  8. cout << "mymap contains: ";  
  9. for(auto &x: mymap)  
  10.     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.

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. map<charint> mymap;  
  2. mymap['a'] = 10;  
  3. mymap['b'] = 20;  
  4. mymap['c'] = 30;  
  5.   
  6. pair<map<charint>::iterator, map<charint>::iterator> ret;  
  7. ret = mymap.equal_range('a');  
  8.   
  9. cout << "lower bound points to: ";  
  10. cout << ret.first->first << " => " << ret.first->second << endl;  
  11. cout << "upper bound points to: ";  
  12. 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)).

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. map<charint> mymap;  
  2. map<charint>::iterator it;  
  3.   
  4. mymap['a'] = 10;  
  5. mymap['b'] = 20;  
  6. mymap['c'] = 30;  
  7. mymap['d'] = 40;  
  8. mymap['e'] = 50;  
  9. mymap['f'] = 60;  
  10.   
  11. it = mymap.find('b');  
  12. mymap.erase(it);        //erasing by iterator, erase b  
  13.   
  14. mymap.erase('c') ;       //erasing by key, erase c  
  15.   
  16. it = mymap.find('e');  
  17. mymap.erase(it, mymap.end()) ;  //erasing by range, erase e, f  
  18.   
  19. //输出a d  
  20. for(it = mymap.begin(); it != mymap.end(); ++ it)  
  21.     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.

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. std::map<char,int> mymap;  
  2. std::map<char,int>::iterator it;  
  3.   
  4. mymap['a']=50;  
  5. mymap['b']=100;  
  6. mymap['c']=150;  
  7. mymap['d']=200;  
  8.   
  9. it=mymap.find('b');  
  10. mymap.erase (it);  
  11. mymap.erase (mymap.find('d'));  
  12.   
  13. // print content:  
  14. std::cout << "elements in mymap:" << '\n';  
  15. std::cout << "a => " << mymap.find('a')->second << '\n';  
  16. std::cout << "c => " << mymap.find('c')->second << '\n';  

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

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. int psize;  
  2. map<charint> mymap;  
  3. pair<const charint> *p;  
  4.   
  5. //allocate an array of 5 elements using mymap's allocater.  
  6. p = mymap.get_allocator().allocate(5);  
  7.   
  8. //assign some values to array  
  9. psize = sizeof(map <charint>::value_type) * 5;  
  10. cout << "the allocated array has a size of " << psize << " bytes\n";  
  11. 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.  插入时会检查是否已经存在和当前键值一样的元素。

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. map<charint> mymap;  
  2.   
  3. //first insert function version(single parameter)  
  4. mymap.insert(pair<charint>('a', 200));  
  5. mymap.insert(pair<charint>('z', 200));  
  6.   
  7. pair<map<charint>::iterator, bool> ret;  
  8.   
  9. ret = mymap.insert(pair<charint>('z', 500));  
  10. if(ret.second == false){  
  11.     cout << "element '" << ret.first->first << "' already exists";  
  12.     cout << " with a value of " << ret.first->second << endl;  
  13. }  
  14.   
  15. //second insert function version(with hint position)  
  16. map<charint>::iterator it = mymap.begin();  
  17. mymap.insert(it, pair<charint>('b', 300));  
  18. mymap.insert(it, pair<charint>('c', 400));  
  19.   
  20. //third insert function version(range insertion);  
  21. map<charint> anothermap;  
  22. anothermap.insert(mymap.begin(), mymap.find('c'));  
  23.   
  24. //showing contents  
  25. cout << "mymap contains:\n";  
  26. for(it = mymap.begin(); it != mymap.end(); ++it)  
  27.     cout << it->first << " => " << it->second << endl;  
  28.   
  29. cout << "anothermap contains:\n";  
  30. for(it = anothermap.begin(); it != anothermap.end(); ++ it)  
  31.     cout << it->first << " => " << it->second << endl;  

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

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. map<charint> mymap;  
  2.   
  3. map<charint>::key_compare mycomp = mymap.key_comp();  
  4. mymap['b'] = 400;  
  5. mymap['e'] = 500;  
  6. mymap['f'] = 300;  
  7. mymap['a'] = 100;  
  8.   
  9. cout << "mymap contains:\n";  
  10. //key value of last element  
  11. char highest = mymap.rbegin()->first;  
  12.   
  13. map<charint>::iterator it = mymap.begin();  
  14. do{  
  15.     cout << it->first << " => " << it->second << endl;  
  16. }while(mycomp((*it ++).first, highest));  
  17.   
  18. 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.

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. map<charint> mymap;  
  2. map<charint>::iterator itlow, itup;  
  3.   
  4. mymap['a'] = 20;  
  5. mymap['b'] = 40;  
  6. mymap['c'] = 60;  
  7. mymap['d'] = 80;  
  8. mymap['e'] = 100;  
  9.   
  10. //itlow points to b  
  11. itlow = mymap.lower_bound('a');  
  12. //itup points to e(not 'd')  
  13. itup = mymap.upper_bound('d');  
  14.   
  15. //erase [itlow, itup)  
  16. mymap.erase(itlow, itup);  
  17.   
  18. for(map<charint>::iterator it = mymap.begin(); it != mymap.end(); ++ it)  
  19. {  
  20.     cout << it->first << " => " << it->second << endl;  
  21. }  
     (17)map::max_size  --> Returns the maximum number of elements that the map container can hold.
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. map<intint> mymap;  
  2.   
  3. cout << mymap.max_size() << endl;  
  4. if(mymap.max_size() > 1000){  
  5.     for(int i = 0; i < 1000; i ++)  
  6.         mymap[i] = 0;  
  7.     cout << "The map contains 1000 elements" << endl;;  
  8. }  
  9. else cout << "The map could not hold 1000 elements." << endl;  
   

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

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. map<charint> first;  
  2. map<charint> second;  
  3.   
  4. first['x'] = 8;  
  5. first['y'] = 16;  
  6. first['z'] = 32;  
  7.   
  8. second = first;  
  9. second.insert(pair<charint>('a', 100));  
  10. first = second;  
  11.   
  12. cout << "Size of first: " << first.size() << endl;  
  13. cout << "Size of second: " << second.size() << endl;  
  14.   
  15. first = map<charint>();  
  16. cout << "Size of first: " << first.size() << endl;  
  17. 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.

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. map<char, string> mymap;  
  2.   
  3. mymap['a'] = "an element";  
  4. mymap['b'] = "another elment";  
  5. mymap['c'] = mymap['b'];  
  6.   
  7. cout << "mymap['a'] is " << mymap['a'] << endl;  
  8. cout << "mymap['b'] is " << mymap['b'] << endl;  
  9. cout << "mymap['c'] is " << mymap['c'] << endl;  
  10.   
  11. 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).

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. map<charint> mymap;  
  2.   
  3. mymap['x'] = 100;  
  4. mymap['y'] = 200;  
  5. mymap['z'] = 300;  
  6.   
  7. map<charint>::reverse_iterator rit;  
  8. for(rit=mymap.rbegin(); rit != mymap.rend(); ++rit){  
  9.     cout << rit->first << " => " << rit->second << endl;  
  10. }  


    (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.  需要是同种类型的

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. map<charint> foo, bar;  
  2.   
  3. foo['x'] = 100;  
  4. foo['y'] = 200;  
  5.   
  6. bar['a'] = 11;  
  7. bar['b'] = 22;  
  8. bar['c'] = 33;  
  9.   
  10. foo.swap(bar);  
  11.   
  12. cout << "foo contains: \n";  
  13. for(map<charint>::iterator it = foo.begin(); it != foo.end(); ++it)  
  14.     cout << it->first << " => " << it->second << endl;  
  15.   
  16. cout << "bar contains: \n";  
  17. for(map<charint>::iterator it = bar.begin(); it != bar.end(); ++it)  
  18.     cout << it->first << " => " << it->second << endl;  
0 0
原创粉丝点击