如何在 C++ 中对 STL 映射应用变换
在 C++ 中,我使用转换将映射的所有值更改为大写。
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);
});
这给了我以下编译错误:
/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> >&)
我认为我的 lambda 表达式中的参数类型有问题。这可能很简单,但我似乎无法弄清楚预期的是什么。
In C++, I'm using transform to change all the values of a map to uppercase.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您缺少该对的第一种类型中的 const。
但这不是您的问题:您不能使用
map
作为 OutputIterator,因为它们不支持分配。但是,您可以使用 std::for_each 改变第二个参数。好旧的
map_to_foobar
:概念性的东西:使用与输入和输出相同的范围调用
transform
是非常合法的,并且如果所有函子都按值返回并且不这样做,则很有意义不要改变他们的论点。然而,就地改变某些东西可能会更快(或者至少在代码中看起来更快,更不用说优化编译器了),并且对于成员函数来说很有意义。You are missing the const in the first type of the pair.
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 usingstd::for_each
.Good old
map_to_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.如果您打算坚持使用
std::transform
,那么您需要std::inserter()
:C++03 MCVE
C++11(您可以在
中执行所有操作main()
真的)C++14(您可以在 lambda 参数中使用
auto
)C++17(只是因为我喜欢结构化绑定)
If you plan on sticking to
std::transform
, then you needstd::inserter()
:C++03 MCVE
C++11 (you can do everything in
main()
really)C++14 (you can use
auto
in lambda parameters)C++17 (just because i love structured bindings)