Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 基于std::map的STL容器模板类型<>;::使用std::set_symmetric_difference时的值类型_C++_Map_Stl Algorithm - Fatal编程技术网

C++ 基于std::map的STL容器模板类型<>;::使用std::set_symmetric_difference时的值类型

C++ 基于std::map的STL容器模板类型<>;::使用std::set_symmetric_difference时的值类型,c++,map,stl-algorithm,C++,Map,Stl Algorithm,给定两个std::map实例,我试图使用std::set\u set\u symmetric\u difference()算法来存储所有差异。我有以下工作代码: #include <iostream> #include <map> #include <string> #include <algorithm> #include <iterator> #include <vector> typedef std::map<

给定两个std::map实例,我试图使用std::set\u set\u symmetric\u difference()算法来存储所有差异。我有以下工作代码:

#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;
}
让我头疼的是MyPairs的typedef,它是地图差异的向量

最初,我尝试将向量类型定义为
typedef std::vectorMyPairs
我遇到了以下错误,这在

SetDifferenceMapVectorType.cpp:36:从此处实例化
/usr/include/c++/4.2.1/bits/stl_pair.h:69:错误:非静态常量成员'const std::basic_string std::pair::first',无法使用默认赋值运算符
这是因为映射中值的键是const,以避免更改键并使映射无效,这是有意义的。因为
std::map::value_type
std::pair
意思是
operator=()
不能用于向向量添加元素,这就是为什么在我的工作示例中不指定const的原因


有没有更好的方法为MyPairs向量定义不冗余的模板参数?到目前为止,我能想到的最好的方法是
std::vector

我不确定这是否是您要寻找的-它是一个元函数,从对的第一个类型中删除常量并返回新的对类型。除非你想深入了解remove_const是如何工作的,否则就需要使用Boost,这需要其他人的帮助

#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; 
#包括
模板
结构从\u对中删除\u const\u
{
typedef std::pair
<
typename boost::remove_const::type,
typename PairType::第二种类型
>类型;
};
typedef std::map MyMap;
//typedef std::vectorMyPairs;
typedef std::vectorMyPairs;

这是更大问题的一部分吗?如果是这样的话,也许有一个解决方案可以帮助我们——但就目前情况而言,我认为没有任何方法可以在不添加大量代码的情况下更通用地实现这一点。Boost不仅可以接受,而且非常受鼓励。我将现有代码分条到上面的示例中,删除现有的boost,并尝试让boost::remove_const工作。我想你把我的失败钉死了。谢谢
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
#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;