我尝试对元组的向量进行排序,但出现一个奇怪的错误:
typedef std::tuple<const std::string, const std::string, const unsigned int> nodesfound;
std::vector<nodesfound> nf;
fil_vector(nf);
std::sort(nf.begin(), nf.end(), [](nodesfound const &n1, nodesfound const &n2) {
return std::get<2>(n1) < std::get<2>(n2);
});
错误是:
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/tuple:299:17: error: no viable overloaded '='_M_head(*this) = std::forward<_Head>(_M_head(__in));
如果我删除排序行,我的程序就很好
最佳答案
为了使sort
工作,必须必须分配元素;但是,您的元组元素都是const
,因此显然不可分配。删除const
:
using nodesfound = std::tuple<std::string, std::string, unsigned>;
std::vector<nodesfound> nf;
fill_vector(nf);
std::sort(
nf.begin(), nf.end(),
[](nodesfound const& n1, nodesfound const& n2) {
return std::get<2>(n1) < std::get<2>(n2);
}
);
Online Demo
关于c++ - 自定义对元组的 vector 排序时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38410034/