一起添加两张地图

MWr*_*ght 4 c++ ubuntu gcc gnu

我想添加两个地图以及以下行为.

如果键存在 - >将两个键值一起添加.

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

我已经看了一些标准库算法.即改造,但似乎没有做我想要的.

取自此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;
}
Run Code Online (Sandbox Code Playgroud)

我的想法来自于我在第二张地图中使用时只有一个迭代器和相应的仿函数

 *result++=binary_op(*first1++,*first2++);
Run Code Online (Sandbox Code Playgroud)

因此,我将无法遍历我的第二个地图以找到键值.

一种想法只是让我自己的算法有一个微妙的变化.

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 
}
Run Code Online (Sandbox Code Playgroud)

然后我就可以使用:

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

这将是我正在思考的更完整的示例,但我似乎得到了一个我无法解决的编译错误(我有点猜测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;

}
Run Code Online (Sandbox Code Playgroud)

这是我的编译器错误:

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)
Run Code Online (Sandbox Code Playgroud)

似乎有来自某个地方的另一个迭代器并且容器类型未正确读取?

有任何想法吗?

我在用

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

Ubuntu/Linaro 4.6.3-1ubuntu5

谢谢

注意:

我在答案中更新了上面代码的工作版本.如果您认为我应该更改问题代码,请告诉我.不确定最佳做法

jro*_*rok 11

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.
Run Code Online (Sandbox Code Playgroud)

我认为一个简单的for循环可以做你想要的:

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

如果密钥存在,则该值将添加到现有密钥.如果该键不存在,operator[]将插入它,它的值将默认初始化(0.0 for double).

我不认为在这里使用适用于任何容器的通用功能是明智的.say vector和map的insert()和operator []的语义太不同了.


MWr*_*ght 0

这是上面的修订版本。问题代码中有很多错误,但这似乎有效并且是相同的想法:

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>
{
  static void Add(IteratorType itr, ContainerType& cont)
  {
    cont[itr->first] += itr->second;
  }
};

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("Test4",3.0) );

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

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

  for(stringDoubleMapItr itr = map2.begin(); itr != map2.end(); ++itr ){
    std::cout << "INFO: Key: " << itr->first << " | Value: " << itr->second  << std::endl;
  }

  return 0;

}
Run Code Online (Sandbox Code Playgroud)