使用 std::set_symmetric_difference 时,STL 容器的模板类型基于 std::map<>::value_type
Basing template type of STL container on std::map<>::value_type when using std::set_symmetric_difference
给定std::map的两个实例,我尝试使用std::set_set_symmetric_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;
}
这打印出我所期望的:
All differences
(diff:0) (diff:1) (notInCurrent:1) (notInPrevious:1)
让我头疼的是MyPairs的typedef,地图差异的向量。
最初,我试图将向量类型定义为typedef std::vector< MyMap::value_type > MyPairs
,但出现了以下错误,该错误在非静态常量成员的可接受答案中有描述,can';t使用默认分配运算符
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
这是因为映射中值的键是const,以避免更改键和使映射无效,这是有意义的。因为std::map<Key,Value>::value_type
是std::pair<const Key, Value>
,意味着operator=()
不能用于向向量添加元素,这就是为什么在我的工作示例中不指定const的原因。
有没有更好的方法来定义MyPairs向量的模板参数,而不是冗余的?到目前为止,我能想到的最好的是std::vector< std::pair<MyMap::key_type, MyMap::mapped_type> >
我不确定这是否是您想要的——它是一个从对的第一个类型中删除常量并返回新对类型的元函数。Boost是必需的,除非你想深入了解remove_test是如何工作的——其他人必须在这方面提供帮助。
#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;
相关文章:
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 使用一个考虑到std::map中键值的滚动或换行的键
- 有没有办法对std::unordered_set、std::unrdered_map、std::set、std::map
- 将重物插入std::map
- 使用通用值初始化 std::map,不重复
- 仅包含可移动 std::map 的类的移动构造函数不起作用
- C++:当所有条目都保证是唯一时,替代 std::map
- 使用模板化的键类型定义 std::map,该键类型基于作为参数接收的函数
- 如果 KEY 是 std::list 或 std::vector 而不是值,那么 std::map 的默认行为是什么?
- C++如何创建 std::map
- 从其他容器中移动构造"std::map"
- 将 std::map::emplace 与返回 shared_ptr 的函数一起使用是否正确?
- C++中 std::map 的运行时复杂度是多少?
- 为什么在 std::map 上移动无法将元素从一个映射移动到另一个映射
- 使用重载 [] 运算符返回 std::map() 的可赋值
- std::map, std::unordered_map - 缩小初始值设定项列表中的转换范围
- C++ 使用枚举类对象分配 std::map 值
- 静态 std::map instatiation 在类的方法中调用构造函数吗?
- std::map:当元素不可默认构造时创建/替换元素
- Arduino编译器和STL:使用std::vector和std::map