c++ - 将两个 map 相加

标签 c++ gcc ubuntu gnu

我想添加两个 map 以及以下行为。

如果键存在 -> 将两个键值相加。

如果键不存在 -> 插入对映射。

我看过一些标准库算法。即转换,但似乎没有做我想要的。

取自此LINK

template < class InputIterator, class OutputIterator, class UnaryOperator >
  OutputIterator transform ( InputIterator first1, InputIterator last1,
                             OutputIterator result, UnaryOperator op )
{
  while (first1 != last1)
    *result++ = op(*first1++);  // or: *result++=binary_op(*first1++,*first2++);
  return result;
}

我的想法是,在使用和适当的仿函数时,我的第二个映射中只有一个迭代器

 *result++=binary_op(*first1++,*first2++);

因此我将无法遍历我的第二张 map 来找到键值。

一个想法是对我自己的算法做一个细微的改变。

template < class InputIterator, class ContainerType, class BinaryOperator >
  void myTransform ( InputIterator first1, InputIterator last1,
                               ContainerType cont2,  
                               BinaryOperator binary_op )
{
  while (first1 != last1)
    binary_op(first1++, cont2); //cont2 passed by reference 
}

然后我就可以使用:

cont2.find() 在我的仿函数中搜索整个 map 。

这将是一个更完整的例子来说明我的想法,但我似乎遇到了一个我无法解决的编译错误(我有点猜测 BinaryOperator 类型......见下文)?

#include <map>
#include <string>
#include <iostream>

template < class InputIterator, class ContainerType, class BinaryOperator >
void myTransform ( InputIterator first1, InputIterator last1,
                 ContainerType &cont2, 
                 BinaryOperator binary_op )
{
  while (first1 != last1)
    binary_op(first1++, cont2); //cont2 passed by reference
}

template<class IteratorType, class ContainerType>
struct AddMapValues:
  std::binary_function<IteratorType, ContainerType, void>
{
  void operator()(IteratorType itr, ContainerType& cont)
  {
    if( cont.find(itr->first) != cont.end() ) cont[itr->first] = cont.find(itr->first).second + itr->second;
    else cont.insert( (*itr) );
  }
};


int main()
{
  typedef std::map<std::string, double> stringDoubleMap;
  typedef std::map<std::string, double>::iterator stringDoubleMapItr;
  typedef void (*ptrfnt)(stringDoubleMapItr, stringDoubleMap& );

  stringDoubleMap map1;
  stringDoubleMap map2;

  map1.insert( stringDoubleMap::value_type("Test1",1.0) );
  map1.insert( stringDoubleMap::value_type("Test2",2.0) );
  map1.insert( stringDoubleMap::value_type("Test3",3.0) );

  map2.insert( stringDoubleMap::value_type("Test1",1.0) );
  map2.insert( stringDoubleMap::value_type("Test2",2.0) );
  map2.insert( stringDoubleMap::value_type("Test3",3.0) );

  myTransform( map1.begin(), map1.end(),
               map2, 
               AddMapValues< stringDoubleMapItr, stringDoubleMap >() );

  return 0;

}

这是我的编译器错误:

testingMapTransforms.cxx: In function ‘int main()’:

testingMapTransforms.cxx:52:85: error: no matching function for call to     ‘myTransform(std::map<std::basic_string<char>, double>::iterator, std::map<std::basic_string<char>, double>::iterator, stringDoubleMap&, std::map<std::basic_string<char>, double>::iterator, AddMapValues<std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, double> >, std::map<std::basic_string<char>, double> >)’

testingMapTransforms.cxx:52:85: note: candidate is:

testingMapTransforms.cxx:12:20: note: template<class InputIterator, class ContainerType, class   OutputIterator, class BinaryOperator> OutputIterator myTransform(InputIterator, InputIterator,  ContainerType, OutputIterator, BinaryOperator)

似乎有另一个迭代器来自某处,容器类型读取不正确?

有什么想法吗?

我正在使用

gcc - GNU 项目 C 和 C++ 编译器

Ubuntu/Linaro 4.6.3-1ubuntu5

谢谢

注意:

我已经在答案中更新了上面代码的工作版本。如果您认为我应该只更改问题代码而不是让我知道。不确定最佳做法

最佳答案

I want to add two maps together with the following behavior:
If key exists add two key values together.
If key does not exist. Insert pair to map.

我认为一个简单的 for 循环就可以满足您的需求:

for(auto it = map2.begin(); it != map2.end(); ++it) map1[it->first] += it->second;

如果键存在,值将被添加到现有的。如果键不存在,operator[] 将插入它并且它的值将被默认初始化(0.0 for double)。

我不认为在这里使用适用于任何容器的通用函数是明智的。 vector 和 map 的 insert() 和 operator[] 的语义差别太大了。

关于c++ - 将两个 map 相加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11013591/

相关文章:

linux - 编译 gcc 4.8.5

使用现代编译器编译的 C++ 项目,但链接到过时的 libstdc++

php - Google V8 Javascript 引擎、Ubuntu 和 PHP - 如何构建和运行它?

python - 在 Ubuntu 系统中安装 Jupyter

mysql - 为 ruby​​ 安装 mysql gem

c++ - 无法使用指针访问类函数

c++ - 关于 C++ 标准库容器中变量的作用域

c++ - 这个 Objective-C Cocos2D 和 CCNode 的 C++ 等价物是什么

c - gcc: -DVAR=-linux 在 Linux 上未按预期工作

c++ - 类 - 用户定义的具有动态大小的智能阵列