我正在尝试按字典顺序对 unsigned int vector 进行排序。
std::lexicographyal_compare 函数仅支持迭代器,因此我不确定如何比较两个数字。
这是我尝试使用的代码:
std::sort(myVector->begin(),myVector->end(), [](const unsigned int& x, const unsigned int& y){
std::vector<unsigned int> tmp1(x);
std::vector<unsigned int> tmp2(y);
return lexicographical_compare(tmp1.begin(),tmp1.end(),tmp2.begin(),tmp2.end());
} );
最佳答案
C++11 引入 std::to_string
您可以使用 from to_string
如下:
std::sort(myVector->begin(),myVector->end(), [](const unsigned int& x, const unsigned int& y){
std::string tmp1 = std::to_string(x);
std::string tmp2 = std::to_string(y);
return lexicographical_compare(tmp1.begin(),tmp1.end(),tmp2.begin(),tmp2.end());
} );
关于c++ - 按字典顺序比较 2 个数字的快速方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40232471/