通过 std::transform 将 std::map 展平为集合
flattening a std::map to a set via std::transform
我需要将容器从 std::map<int, std::set<std::string>>
扁平化到 std::set<std::string>
,我 运行 语法困难。当我说展平时,我基本上想制作一个 std::set<string>
并将所有集合缝合在一起 - (set
将防止重复条目,这正是我所需要的)我正在尝试基于中的答案这个 question 但我的值类型是一个容器,这使得问题更难解决。编译器的输出非常难懂,这里出处如下。
我有我的代码的 live coliru here
int main()
{
std::set<std::string> outputSet;
std::map<int, std::set<std::string>> inputMap = {
{1, {"a", "b", "c"}},
{2, {"b", "c", "d"}}
};
std::transform(inputMap.cbegin(), inputMap.cend(),
std::inserter(outputSet, outputSet.end()),
[](const std::map<int, std::set<std::string>>::value_type& rNext) {
return rNext.second;
});
}
报错如下:
In file included from /usr/local/include/c++/6.2.0/algorithm:62:0,
from main.cpp:4:
/usr/local/include/c++/6.2.0/bits/stl_algo.h: In instantiation of '_OIter std::transform(_IIter, _IIter, _OIter, _UnaryOperation) [with _IIter = std::_Rb_tree_const_iterator<std::pair<const int, std::set<std::__cxx11::basic_string<char> > > >; _OIter = std::insert_iterator<std::set<std::__cxx11::basic_string<char> > >; _UnaryOperation = main()::<lambda(const value_type&)>]':
main.cpp:19:10: required from here
/usr/local/include/c++/6.2.0/bits/stl_algo.h:4177:12: error: no match for 'operator=' (operand types are 'std::insert_iterator<std::set<std::__cxx11::basic_string<char> > >' and 'std::set<std::__cxx11::basic_string<char> >')
*__result = __unary_op(*__first);
~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/local/include/c++/6.2.0/bits/stl_algobase.h:67:0,
from /usr/local/include/c++/6.2.0/bits/stl_tree.h:63,
from /usr/local/include/c++/6.2.0/set:60,
from main.cpp:1:
/usr/local/include/c++/6.2.0/bits/stl_iterator.h:680:7: note: candidate: std::insert_iterator<_Container>& std::insert_iterator<_Container>::operator=(const typename _Container::value_type&) [with _Container = std::set<std::__cxx11::basic_string<char> >; typename _Container::value_type = std::__cxx11::basic_string<char>]
operator=(const typename _Container::value_type& __value)
^~~~~~~~
/usr/local/include/c++/6.2.0/bits/stl_iterator.h:680:7: note: no known conversion for argument 1 from 'std::set<std::__cxx11::basic_string<char> >' to 'const value_type& {aka const std::__cxx11::basic_string<char>&}'
/usr/local/include/c++/6.2.0/bits/stl_iterator.h:688:7: note: candidate: std::insert_iterator<_Container>& std::insert_iterator<_Container>::operator=(typename _Container::value_type&&) [with _Container = std::set<std::__cxx11::basic_string<char> >; typename _Container::value_type = std::__cxx11::basic_string<char>]
operator=(typename _Container::value_type&& __value)
^~~~~~~~
/usr/local/include/c++/6.2.0/bits/stl_iterator.h:688:7: note: no known conversion for argument 1 from 'std::set<std::__cxx11::basic_string<char> >' to 'std::set<std::__cxx11::basic_string<char> >::value_type&& {aka std::__cxx11::basic_string<char>&&}'
/usr/local/include/c++/6.2.0/bits/stl_iterator.h:629:11: note: candidate: constexpr std::insert_iterator<std::set<std::__cxx11::basic_string<char> > >& std::insert_iterator<std::set<std::__cxx11::basic_string<char> > >::operator=(const std::insert_iterator<std::set<std::__cxx11::basic_string<char> > >&)
class insert_iterator
^~~~~~~~~~~~~~~
/usr/local/include/c++/6.2.0/bits/stl_iterator.h:629:11: note: no known conversion for argument 1 from 'std::set<std::__cxx11::basic_string<char> >' to 'const std::insert_iterator<std::set<std::__cxx11::basic_string<char> > >&'
/usr/local/include/c++/6.2.0/bits/stl_iterator.h:629:11: note: candidate: constexpr std::insert_iterator<std::set<std::__cxx11::basic_string<char> > >& std::insert_iterator<std::set<std::__cxx11::basic_string<char> > >::operator=(std::insert_iterator<std::set<std::__cxx11::basic_string<char> > >&&)
/usr/local/include/c++/6.2.0/bits/stl_iterator.h:629:11: note: no known conversion for argument 1 from 'std::set<std::__cxx11::basic_string<char> >' to 'std::insert_iterator<std::set<std::__cxx11::basic_string<char> > >&&'
您使用 std::inserter
创建的 std::insert_iterator
期望分配一个元素,然后将其插入到容器中,但您试图分配整个 set
给它。
std::for_each
更适合您的目标
std::for_each(inputMap.cbegin(), inputMap.cend(),
[&](auto const& m) {
outputSet.insert(m.second.begin(), m.second.end());
});
使用范围 for
:
以直接的方式做事
for (const auto& v : inputMap)
for (const auto& e : v.second)
outputSet.insert(e);
这可能不是超级优雅;但它也不会产生任何编译器错误。通过该工作,您可以将 necessary/desired 调整为其他内容。例如
for (const auto& v : inputMap)
outputSet.insert(v.second.cbegin(), v.second.cend());
我需要将容器从 std::map<int, std::set<std::string>>
扁平化到 std::set<std::string>
,我 运行 语法困难。当我说展平时,我基本上想制作一个 std::set<string>
并将所有集合缝合在一起 - (set
将防止重复条目,这正是我所需要的)我正在尝试基于中的答案这个 question 但我的值类型是一个容器,这使得问题更难解决。编译器的输出非常难懂,这里出处如下。
我有我的代码的 live coliru here
int main()
{
std::set<std::string> outputSet;
std::map<int, std::set<std::string>> inputMap = {
{1, {"a", "b", "c"}},
{2, {"b", "c", "d"}}
};
std::transform(inputMap.cbegin(), inputMap.cend(),
std::inserter(outputSet, outputSet.end()),
[](const std::map<int, std::set<std::string>>::value_type& rNext) {
return rNext.second;
});
}
报错如下:
In file included from /usr/local/include/c++/6.2.0/algorithm:62:0,
from main.cpp:4:
/usr/local/include/c++/6.2.0/bits/stl_algo.h: In instantiation of '_OIter std::transform(_IIter, _IIter, _OIter, _UnaryOperation) [with _IIter = std::_Rb_tree_const_iterator<std::pair<const int, std::set<std::__cxx11::basic_string<char> > > >; _OIter = std::insert_iterator<std::set<std::__cxx11::basic_string<char> > >; _UnaryOperation = main()::<lambda(const value_type&)>]':
main.cpp:19:10: required from here
/usr/local/include/c++/6.2.0/bits/stl_algo.h:4177:12: error: no match for 'operator=' (operand types are 'std::insert_iterator<std::set<std::__cxx11::basic_string<char> > >' and 'std::set<std::__cxx11::basic_string<char> >')
*__result = __unary_op(*__first);
~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/local/include/c++/6.2.0/bits/stl_algobase.h:67:0,
from /usr/local/include/c++/6.2.0/bits/stl_tree.h:63,
from /usr/local/include/c++/6.2.0/set:60,
from main.cpp:1:
/usr/local/include/c++/6.2.0/bits/stl_iterator.h:680:7: note: candidate: std::insert_iterator<_Container>& std::insert_iterator<_Container>::operator=(const typename _Container::value_type&) [with _Container = std::set<std::__cxx11::basic_string<char> >; typename _Container::value_type = std::__cxx11::basic_string<char>]
operator=(const typename _Container::value_type& __value)
^~~~~~~~
/usr/local/include/c++/6.2.0/bits/stl_iterator.h:680:7: note: no known conversion for argument 1 from 'std::set<std::__cxx11::basic_string<char> >' to 'const value_type& {aka const std::__cxx11::basic_string<char>&}'
/usr/local/include/c++/6.2.0/bits/stl_iterator.h:688:7: note: candidate: std::insert_iterator<_Container>& std::insert_iterator<_Container>::operator=(typename _Container::value_type&&) [with _Container = std::set<std::__cxx11::basic_string<char> >; typename _Container::value_type = std::__cxx11::basic_string<char>]
operator=(typename _Container::value_type&& __value)
^~~~~~~~
/usr/local/include/c++/6.2.0/bits/stl_iterator.h:688:7: note: no known conversion for argument 1 from 'std::set<std::__cxx11::basic_string<char> >' to 'std::set<std::__cxx11::basic_string<char> >::value_type&& {aka std::__cxx11::basic_string<char>&&}'
/usr/local/include/c++/6.2.0/bits/stl_iterator.h:629:11: note: candidate: constexpr std::insert_iterator<std::set<std::__cxx11::basic_string<char> > >& std::insert_iterator<std::set<std::__cxx11::basic_string<char> > >::operator=(const std::insert_iterator<std::set<std::__cxx11::basic_string<char> > >&)
class insert_iterator
^~~~~~~~~~~~~~~
/usr/local/include/c++/6.2.0/bits/stl_iterator.h:629:11: note: no known conversion for argument 1 from 'std::set<std::__cxx11::basic_string<char> >' to 'const std::insert_iterator<std::set<std::__cxx11::basic_string<char> > >&'
/usr/local/include/c++/6.2.0/bits/stl_iterator.h:629:11: note: candidate: constexpr std::insert_iterator<std::set<std::__cxx11::basic_string<char> > >& std::insert_iterator<std::set<std::__cxx11::basic_string<char> > >::operator=(std::insert_iterator<std::set<std::__cxx11::basic_string<char> > >&&)
/usr/local/include/c++/6.2.0/bits/stl_iterator.h:629:11: note: no known conversion for argument 1 from 'std::set<std::__cxx11::basic_string<char> >' to 'std::insert_iterator<std::set<std::__cxx11::basic_string<char> > >&&'
您使用 std::inserter
创建的 std::insert_iterator
期望分配一个元素,然后将其插入到容器中,但您试图分配整个 set
给它。
std::for_each
更适合您的目标
std::for_each(inputMap.cbegin(), inputMap.cend(),
[&](auto const& m) {
outputSet.insert(m.second.begin(), m.second.end());
});
使用范围 for
:
for (const auto& v : inputMap)
for (const auto& e : v.second)
outputSet.insert(e);
这可能不是超级优雅;但它也不会产生任何编译器错误。通过该工作,您可以将 necessary/desired 调整为其他内容。例如
for (const auto& v : inputMap)
outputSet.insert(v.second.cbegin(), v.second.cend());