Joel
Joel

Reputation: 2966

Basing template type of STL container on std::map<>::value_type when using std::set_symmetric_difference

Given two instances of std::map I'm trying to use the std::set_set_symmetric_difference() algorithm to store all differences. I have the following working code:

#include <iostream>
#include <map>
#include <string>
#include <algorithm>
#include <iterator>
#include <vector>

typedef std::map<std::string,bool> MyMap;
typedef std::vector< std::pair<MyMap::key_type,MyMap::mapped_type> > MyPairs;

//typedef std::vector< MyMap::value_type > MyPairs; 

using namespace std;
int main(int argc, char *argv[]) {
    MyMap previous;
    MyMap current;

    //Modified value
    previous["diff"] = true;
    current["diff"] = false;

    //Missing key in current
    previous["notInCurrent"] = true;

    //Missing key in previous
    current["notInPrevious"] = true;

    //Same value
    previous["same"] = true;
    current["same"] = true;

    cout << "All differences " << endl;
    MyPairs differences;
    std::back_insert_iterator<MyPairs> back_it(differences);
std::set_symmetric_difference(previous.begin(),previous.end(),current.begin(),current.end(),back_it);

    for(MyPairs::iterator it = differences.begin(); it != differences.end(); it++){
        cout << "(" << it->first << ":" << it->second << ") ";
    }
    cout << endl;

    return 0;
}

This prints what I expect:

All differences 
(diff:0) (diff:1) (notInCurrent:1) (notInPrevious:1)

What bugs me is that the typedef for MyPairs, the vector of differences from the maps.

Initially I tried to typedef the vector like as typedef std::vector< MyMap::value_type > MyPairs I land up with the following error which is described in the accepted answer of Non-static const member, can't use default assignment operator

SetDifferenceMapVectorType.cpp:36:   instantiated from here
/usr/include/c++/4.2.1/bits/stl_pair.h:69: error: non-static const member 'const std::basic_string<char, std::char_traits<char>, std::allocator<char> > std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool>::first', can't use default assignment operator

This is because the key for a value in a map is const to avoid changing the key and invalidating the map which makes sense. Because std::map<Key,Value>::value_type is std::pair<const Key, Value> meaning operator=() can't be used to add elements to the vector which is why not specifying const works in my working example.

Is there a better way to define the template parameter for the MyPairs vector that isn't redundant? The best I've been able to come up with so far is std::vector< std::pair<MyMap::key_type, MyMap::mapped_type> >

Upvotes: 3

Views: 1049

Answers (1)

Zac
Zac

Reputation: 3285

I'm not sure if this is what you are looking for - its a meta-function that removes the const from the first type of the pair and returns the new pair type. Boost is required unless you want to dive into how remove_const works - someone else will have to help on that one.

#include <boost/type_traits/remove_const.hpp>

template< typename PairType >
struct remove_const_from_pair
{
  typedef std::pair
    <
      typename boost::remove_const< typename PairType::first_type>::type,
      typename PairType::second_type
    > type;
};

typedef std::map<std::string,bool> MyMap;
//typedef std::vector< std::pair<MyMap::key_type,MyMap::mapped_type> > MyPairs;

typedef std::vector< remove_const_from_pair<MyMap::value_type>::type > MyPairs; 

Upvotes: 2

Related Questions