vector<int> as key of map

From: Aries (jesuspit63_at_hotmail.com)
Date: 04/23/04

  • Next message: Chris Val: "Re: Friends and templates"
    Date: Fri, 23 Apr 2004 11:41:30 +0800
    
    

    May I know if it is possible to use vector<int> as the key for a map?
    i.e.
    map<vector<int>, int, lt_fn> my_map;

    I have tried in the following code, but it seems some of the mapped key
    cannot be found. What's more weird is that the 'not found' is dependent
    on the order of insert into the map.
    Can anyone please kindly help?

    Thanks a lot.

    //======================
    //the problematic code with inserted object not found
    //=======================
    #include <iostream>
    #include <vector>
    #include <map>

    using namespace std;

    //define less than function for map using vector<int> as key
    struct ltvector{
       bool operator()(vector<int> set1, vector<int> set2) const{
         if(set1.size() < set2.size()){
           return true;
         }else if(set1.size() > set2.size()){
           return false;
         }else{
           for(unsigned int i = 0; i < set1.size(); i++){
            if(set1[i] < set2[i]){
              return true;
            }
           }
         }
         return false;
       }
    };

    typedef map<vector<int>, int, ltvector> mapItemSet;

    bool is_exist(mapItemSet& input, vector<int> vec){
       return (input.find(vec) != input.end());
    }

    int main(int argc, char *argv[]){
        //the special map
        mapItemSet testMap;
       //keys be inserted into the map
        vector<int> a;
        a.push_back(1);
        a.push_back(2);
        a.push_back(3);
        vector<int> b;
        b.push_back(1);
        b.push_back(2);
        b.push_back(7);
        vector<int> c;
        c.push_back(1);
        c.push_back(2);
        c.push_back(9);
        vector<int> d;
        d.push_back(1);
        d.push_back(2);
        d.push_back(8);
        vector<int> e;
        e.push_back(1);
        e.push_back(3);
        e.push_back(6);
        vector<int> f;
        f.push_back(1);
        f.push_back(3);
        f.push_back(10);
        vector<int> g;
        g.push_back(1);
        g.push_back(3);
        g.push_back(8);

        //insert into the map
        testMap.insert(mapItemSet::value_type(a, 12));
        testMap.insert(mapItemSet::value_type(b, 13));
        testMap.insert(mapItemSet::value_type(c, 14));
        testMap.insert(mapItemSet::value_type(d, 11));
        testMap.insert(mapItemSet::value_type(e, 12));
        testMap.insert(mapItemSet::value_type(f, 12));
        testMap.insert(mapItemSet::value_type(g, 13));

        //check by key(vector<int>) in such an element exists
        if(is_exist(testMap, a))
          cout << "a is found!" << endl;
        if(is_exist(testMap, b))
          cout << "b is found!" << endl;
        if(is_exist(testMap, c))
          cout << "c is found!" << endl;
        if(is_exist(testMap, d))
          cout << "d is found!" << endl;
        if(is_exist(testMap, e))
          cout << "e is found!" << endl;
        if(is_exist(testMap, f))
          cout << "f is found!" << endl;
        if(is_exist(testMap, g))
          cout << "g is found!" << endl;

        return 0;
    }


  • Next message: Chris Val: "Re: Friends and templates"