C++ 함수 std::algorithm::lexicographical_compare()는 한 범위가 다른 범위보다 사전순으로 작은지 여부를 테스트합니다. 사전순 비교는 사전에서 단어를 알파벳순으로 정렬하는 데 일반적으로 사용되는 비교 유형입니다.
선언
템플릿 <클래스 InputIterator1, 클래스 InputIterator2>
bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
알고리즘
Begin result = lexicographical_compare(v1.begin(), v1.end(), v2.begin(), v2.end()) if (result == true) Print v1 is less than v2 result = lexicographical_compare(v1.begin(), v1.end(), v2.begin(), v2.end()) if (result == false) Print v1 is not less than v2 End
예시
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
int main(void) {
//initialization of v1 and v2
vector<string> v1 = {"One", "Two", "Three"};
vector<string> v2 = {"one", "two", "three"};
bool result;
result = lexicographical_compare(v1.begin(), v1.end(), v2.begin(), v2.end());
if (result == true)
cout << "v1 is less than v2." << endl;
v1[0] = "two";
result = lexicographical_compare(v1.begin(), v1.end(), v2.begin(), v2.end());
if (result == false)
cout << "v1 is not less than v2." << endl;
return 0;
} 출력
v1 is less than v2. v1 is not less than v2.