the type defined for the container

The STL defines the following types for all containers :
   container:: value_type , such as vector<int>::value_type for int type
   (Note: For ordinary, it is its type, for map, it is pair<int, string> type )
    container:: reference , such as list<int>::reference for int &
    container:: const_reference , such as set<int>::const_reference means const int &
    container:: iterator , such as list<int>::iterator means iterator to int
    container:: const_iterator , const iterator
    Container:: different_type , which represents the distance between two iterators, similar to the difference between pointers.
    Container:: size_type , an unsigned integer representing the number of elements of the container object.
The types defined by the STL specifically for associative containers are:
   Container:: key_type , indicating the type of the keyword, such as map<int, string>::key_type=int
    container:: mapped_type , the type of the value in the map/multimap, string in the above example
    container:: key_compare , the object class of the keyword comparison function, defaults to less<Tkey>
    Container:: value_compare , for set/multiset, same as key_compare; for map/multimap, provides sorting function for value_type
#include<iostream>
#include<map>
using namespace std;
template<class container>
void print_container(container& v)
{
        for(auto& elem:v)
                cout<<elem.first<<","<<elem.second<<endl;
}
intmain()
{
        //STL defines types for all containers
        //The value_type here is equivalent to pair<int,string>
        map<int,string>::value_type arr[4] =
        {
                map<int,string>::value_type (1,"kunming"),
                map<int,string>::value_type(3,"nanning"),
                map<int,string>::value_type(2,"guiyang"),
                map<int,string>::value_type(3,"haikou")
        };
        map<int,string> map1(arr,arr+4);
        print_container(map1);
        map<int,string>::size_type size = map1.size();
        cout<<"map1.size="<<size<<endl;
        map<int,string>::key_type cnt = map1.erase(3);
        cout<<"map1 after erase key 3"<<endl;
        print_container(map1);
        cout<<"erase count="<<cnt<<endl;
        cout<<"----华丽的分割线----"<<endl;
        multimap<int,string> mmap1(arr,arr+4);
        print_container(mmap1);
        multimap<int,string>::size_type msize = mmap1.size();
        cout<<"mmap1.size="<<msize<<endl;
        multimap<int,string>::key_type mcnt = mmap1.erase(3);
        cout<<"mmap1 after erase key 3"<<endl;
        print_container(mmap1);
        cout<<"erase count="<<mcnt<<endl;
        return 0;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325894452&siteId=291194637