2012-04-19 4 views
1

I는 벡터의 벡터대로 interval [i] [0]에 대한 'intervals'. 따라서 각 객체의 [0] 요소로 벡터 객체를 정렬하십시오.정렬 벡터 [I] [0] 다음

어떻게하면됩니까? 미리 감사드립니다.

+0

'map >'키가'vector [0] '인 곳에서 사용할 수 있습니까? – EdChum

답변

7

std::sort은 object의 비교 함수를 세 번째 인수로 취하므로 두 벡터를 취하여 첫 번째 요소를 비교하는보다 작음 연산자를 정의 할 수 있습니다.

bool foo(const std::vector<int>& a, const std::vector<int>& b) { 
    // in real life you may want to check vectors aren't empty. 
    // in real life you wouldn't call this foo either. 
    return a[0]<b[0]; 
} 

int main() { 
    std::vector<std::vector<int>> v = ...; 
    std::sort(v.begin(), v.end(), foo); 
} 
+1

필자는 비교 함수의 이름으로'foo'를 사용하지 않을 것입니다. 그러나 그렇지 않으면 이것을 수행하는 방법입니다. –

+0

@MarkRansom도 마찬가지입니다! 유명한. – juanchopanza