2016-10-25 7 views
2

나는 사전 식 순서로 서명되지 않은 int의 벡터를 정렬하려고 해요.빠른 방법 lexicographical 두 숫자를 비교 할

std :: lexicographical_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()); 
}); 
+0

그래서 당신이 원하는 숫자를 십진수로 비교합니까? – wilx

+3

숫자가 사전 적으로 작지 만 숫자가 숫자보다 크거나 같을 때의 예를 들려 줄 수 있습니까? – kfsone

+0

코드가 작동하는지 확인 하시겠습니까? – Ebrahimi

답변

2

C++ 11 std::to_string

을 소개하면 아래와 같은 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()); 
});