Populate a vector with all multimap values with a given key

前端 未结 6 1671
伪装坚强ぢ
伪装坚强ぢ 2021-02-08 13:10

Given a multimap M what\'s a neat way to create a vector of all values in M with a specific key.

e.g given a multimap how c

6条回答
  •  名媛妹妹
    2021-02-08 13:31

    Here's the way to do it STL style :

    // The following define is needed for select2nd with DinkumWare STL under VC++
    #define _HAS_TRADITIONAL_STL 1
    
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    using namespace std;
    
    void main()
    {
        typedef multimap MapType;
        MapType m;
        vector v;
    
        // Test data
        for(int i = 0; i < 10; ++i)
        {
            m.insert(make_pair("123", i * 2));
            m.insert(make_pair("12", i));
        }
    
        MapType::iterator i = m.lower_bound("123");
        MapType::iterator j = m.upper_bound("123");
    
        transform(i, j, back_inserter(v), select2nd());
    
        copy(v.begin(), v.end(),  ostream_iterator(cout, ","));
    
    }
    

提交回复
热议问题