我的头文件中有一个自定义地图
class Tramway
{
private:
using stations = std::vector <std::string>;
using Tramlines = std::map <std::string, stations>;
...../
我正在尝试对唯一值进行排序,但是到目前为止,我的方法是给我编译错误。 这是我的代码。
void Tramway::print_stations(const Tramway::Tramlines &tramlines)
{
for(auto map_iter = tramlines.cbegin(); map_iter != tramlines.cend(); ++map_iter)
{
std::unique(map_iter->second.cbegin(), map_iter->second.cend());
std::sort(map_iter->second.begin(), map_iter->second.end());
for( auto vec_iter = map_iter->second.cbegin() ; vec_iter != map_iter->second.cend() ; ++vec_iter )
std::cout << *vec_iter << std::endl;
}
}
有没有办法获取唯一值并同时对其进行排序?我尝试了std::sort(std::unique(..,..))
,但是std::sort()
需要两个参数才能工作。
下面是我不断得到的错误
x86_64-w64-mingw32\7.3.0\include\c++\bits\stl_algobase.h:148: error: no matching function for call to 'swap(const std::__cxx11::basic_string<char>&, const std::__cxx11::basic_string<char>&)'
swap(*__a, *__b);
~~~~^~~~~~~~~~~~
x86_64-w64-mingw32\7.3.0\include\c++\bits\stl_algo.h:975: error: passing 'const std::__cxx11::basic_string<char>' as 'this' argument discards qualifiers [-fpermissive]
*++__dest = _GLIBCXX_MOVE(*__first);
^
x86_64-w64-mingw32\7.3.0\include\c++\bits\stl_algo.h:1852: error: passing 'const std::__cxx11::basic_string<char>' as 'this' argument discards qualifiers [-fpermissive]
*__first = _GLIBCXX_MOVE(__val);
^
x86_64-w64-mingw32\7.3.0\include\c++\bits\stl_heap.h:252: error: passing 'const std::__cxx11::basic_string<char>' as 'this' argument discards qualifiers [-fpermissive]
*__result = _GLIBCXX_MOVE(*__first);
^
x86_64-w64-mingw32\7.3.0\include\c++\bits\move.h:187: error: no type named 'type' in 'struct std::enable_if<false, void>'
86_64-w64-mingw32\7.3.0\include\c++\bits\stl_heap.h:225: error: passing 'const std::__cxx11::basic_string<char>' as 'this' argument discards qualifiers [-fpermissive]
*(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __secondChild));
^
86_64-w64-mingw32\7.3.0\include\c++\bits\stl_heap.h:231: error: passing 'const std::__cxx11::basic_string<char>' as 'this' argument discards qualifiers [-fpermissive]
*(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first
^
x86_64-w64-mingw32\7.3.0\include\c++\bits\stl_algobase.h:548: error: passing 'const std::__cxx11::basic_string<char>' as 'this' argument discards qualifiers [-fpermissive]
*--__result = std::move(*--__last);
~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
答案 0 :(得分:2)
三个问题:
std::unique
需要可修改的迭代器。您不应该将cbegin/cend
传递给他们。该功能还应采用可修改的参考。
std::sort
首先,之后std::unique
。 std::unique
仅删除相邻的等效元素。
std::unique
无权更改向量的大小。它将结果序列仅放在向量的开头。使用remove-erase idiom删除删除的元素。
固定代码:
void Tramway::print_stations(Tramway::Tramlines& tramlines)
{
for (auto map_iter = tramlines.cbegin(); map_iter != tramlines.cend(); ++map_iter)
{
std::sort(map_iter->second.begin(), map_iter->second.end());
map_iter->second.erase(std::unique(map_iter->second.begin(), map_iter->second.end()), map_iter->second.end());
for (auto vec_iter = map_iter->second.cbegin(); vec_iter != map_iter->second.cend(); ++vec_iter)
std::cout << *vec_iter << '\n';
}
}
另外,考虑使用基于范围的for循环和引用以简化代码:
void Tramway::print_stations(Tramway::Tramlines& tramlines)
{
for (auto& tramline : tramlines) {
auto& station = tramline.second;
std::sort(station.begin(), station.end());
station.erase(std::unique(station.begin(), station.end()), station.end());
std::copy(station.begin(), station.end(),
std::ostream_iterator<stations>{std::cout, '\n'});
}
}