如何在C ++中将变换应用于STL映射

时间:2021-01-31 23:37:12

In C++, I'm using transform to change all the values of a map to uppercase.

在C ++中,我使用transform将地图的所有值更改为大写。

  std::map<std::string, std::string> data = getData();

  // make all values uppercase
  std::transform(data.begin(), data.end(), data.begin(),
         [](std::pair<std::string, std::string>& p) {
           boost::to_upper(p.second);
           return(p);
         });

This gives me the following compilation error:

这给了我以下编译错误:

/opt/local/include/gcc46/c++/bits/stl_algo.h:4805:2: error: no match for call to '(main(int, char**)::<lambda(std::pair<std::basic_string<char>, std::basic_string<char> >&)>) (std::pair<const std::basic_string<char>, std::basic_string<char> >&)

I think there's something wrong with the type of the argument in my lambda expression. It's probably something simple, but I can't seem to figure out what's expected.

我认为我的lambda表达式中的参数类型有问题。它可能很简单,但我似乎无法弄清楚预期的结果。

1 个解决方案

#1


19  

You are missing the const in the first type of the pair.

你缺少第一种类型的const。

[](std::pair<const std::string, std::string>& p) {

However this is not your problem: You cannot use a map as the OutputIterator, as they do not support assignment. You can, however mutate the second argument using std::for_each.

但是,这不是您的问题:您不能将地图用作OutputIterator,因为它们不支持赋值。但是,您可以使用std :: for_each来改变第二个参数。

Good old map_to_foobar:

好老map_to_foobar:

std::for_each(data.begin(), data.end(), 
              [](std::pair<const std::string, std::string>& p) {
                p.second = "foobar";
              });

Conceptual stuff: Calling transform with the same range as input and output is quite legit and makes a lot of sense if all your functors return by value and don't mutate their arguments. However, mutating something in place can be a faster (or at least look faster in code, nevermind the optimizing compiler) and makes a lot of sense with member functions.

概念性的东西:调用与输入和输出相同范围的变换是非常合理的,如果所有的仿函数都按值返回并且不改变它们的参数,那么这很有意义。但是,对某些内容进行变更可能会更快(或者至少在代码中看起来更快,而不是优化编译器)并且对成员函数有很大意义。

#1


19  

You are missing the const in the first type of the pair.

你缺少第一种类型的const。

[](std::pair<const std::string, std::string>& p) {

However this is not your problem: You cannot use a map as the OutputIterator, as they do not support assignment. You can, however mutate the second argument using std::for_each.

但是,这不是您的问题:您不能将地图用作OutputIterator,因为它们不支持赋值。但是,您可以使用std :: for_each来改变第二个参数。

Good old map_to_foobar:

好老map_to_foobar:

std::for_each(data.begin(), data.end(), 
              [](std::pair<const std::string, std::string>& p) {
                p.second = "foobar";
              });

Conceptual stuff: Calling transform with the same range as input and output is quite legit and makes a lot of sense if all your functors return by value and don't mutate their arguments. However, mutating something in place can be a faster (or at least look faster in code, nevermind the optimizing compiler) and makes a lot of sense with member functions.

概念性的东西:调用与输入和输出相同范围的变换是非常合理的,如果所有的仿函数都按值返回并且不改变它们的参数,那么这很有意义。但是,对某些内容进行变更可能会更快(或者至少在代码中看起来更快,而不是优化编译器)并且对成员函数有很大意义。