我如何返回一个std :: map,其中包含c ++中两个映射的差异? [英] How i can return a std::map containing the differences of two maps in c++?
问题描述
我有两张地图,我需要找到差异并创建仅具有差异的新地图.不确定如何执行此操作.我尝试使用set_difference,但并不太了解它是如何工作的.任何帮助,将不胜感激.谢谢
i have two maps and i need to find the differences and create a new map that only has the differences. Not sure how to do this. I tried using set_difference but don't really understand how it works. Any help would be appreciated. thanks
// header file
typedef std::map<std::string, int> MapCol;
typedef std::map<std::string, MapCol> MapRow;
MapRow m_mapRows;
//.cpp fle
CheckForDifferences( const Table& rhs )
{
Table diffTable;
vector<string> v;
vector<string>::iterator it;
it=set_difference (m_mapRows.begin(), m_mapRows.end(), diffTable.m_mapRows.begin(), diffTable.m_mapRows.end, v.begin());
}
std::set_difference( m_mapRows.begin(), m_mapRows.end(),
rhs.m_mapRows.begin(), rhs.m_mapRows.end(), diffTable.m_mapRows.begin());
好的,这是我尝试过的,但是我遇到了错误,第一个是 错误C2678:二进制'=':找不到运算符,该运算符采用类型为'const std :: string'的左侧操作数(或者没有可接受的转换)
ok this is what i tried, but i get errors, the first one being error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const std::string' (or there is no acceptable conversion)
有什么想法吗?
推荐答案
如果您有两个已排序的容器a
和b
,并且想要将不在一个容器中的对象集复制到另一个容器中c
,您可以这样做:
If you have two sorted containers a
and b
and you want to copy the set of objects that aren't in either one into another container c
, you'd do this:
std::set_symmetric_difference(
a.begin(), a.end(),
b.begin(), b.end(),
std::back_inserter(c) );
如果只希望a
中的元素不在b
中,请使用set_difference
而不是set_symmetric_difference
.
If you just want the elements from a
that aren't in b
, use set_difference
instead of set_symmetric_difference
.
这篇关于我如何返回一个std :: map,其中包含c ++中两个映射的差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!