나는 boost :: bind, boost :: lambda 라이브러리와 STL 알고리즘을 어떻게 사용할 수 있는지에 관심이있다. int key로 정렬 된 int-string 쌍의 벡터가 있다고 가정합니다.C++ : std :: less <int>을 boost :: bind 및 boost :: lambda와 함께 사용하는 방법?
std::less<int> comparator;
:
std::vector<std::pair<int, string> > entries;
...
int k = ...;
// Let's ignore std::lower_bound return value for now
std::lower_bound (entries.begin(), entries.end(), k,
boost::bind (&std::pair<int, string>::first, _1) < k)
지금 나는 (이 예에서는 유형 std::less<int>
의) 함수 객체로 operator<
을 대체하려는 : 정렬 된 벡터를 유지하는 것은 찾을 수 있습니다 동안 다음과 같이 장소는 새로운 쌍을 삽입
위 코드를 어떻게 변경하여 작동합니까? 난 그냥 boost::bind
의 반환 형식이 무엇이든 허용하지 않습니다
std::lower_bound (entries.begin(), entries.end(), k,
comparator (boost::bind (&std::pair<int, string>::first, _1), k))
std::less<int>::operator()
때문에 할 수 없습니다. 내가 여기서 무엇을 놓치고 있니? TIA
외부 boost :: bind는 comparator.operator()가 내부 boost :: bind가 생성하는 것을 받아들이는 마법을 만듭니다. 감사! (BTW 누락 된 쌍 템플릿 매개 변수는 내 편집 오류 일뿐입니다.) –
이렇게 여러 번 해보려는 유혹을받습니다. 그러나 한 걸음 물러서면 코드를 읽는 것이 매우 어렵다는 것을 깨닫게됩니다. 이제 바인딩은 C++ 표준의 일부이므로 부스트 구현 만 관계형 및 논리 연산자를 오버로드한다는 점에 유의해야합니다. –