2014-02-28 2 views
0

이 클래스는 HPC_user라고하며 멤버 변수 중 하나를 사용하여 HPC_user 벡터를 내림차순으로 정렬하려고합니다. std :: sort는 세 번째 인수로 작업을 수행해야합니다. 그래서이 스레드를 따라 더 큰 새로운 구조체를 정의했습니다.std :: sort를 사용하여 내림차순으로 클래스의 벡터 정렬

In file included from db2class.cpp:3: 
    In file included from /usr/include/c++/4.2.1/iostream:44: 
    In file included from /usr/include/c++/4.2.1/ostream:44: 
    In file included from /usr/include/c++/4.2.1/ios:44: 
    In file included from /usr/include/c++/4.2.1/bits/char_traits.h:45: 
    In file included from /usr/include/c++/4.2.1/bits/stl_algobase.h:74: 
    /usr/include/c++/4.2.1/bits/stl_iterator_base_types.h:128:35: error: no type named 'iterator_category' in 'HPC_user' 
      typedef typename _Iterator::iterator_category iterator_category; 
        ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~ 
    /usr/include/c++/4.2.1/bits/stl_algo.h:2854:24: note: in instantiation of template class 'std::iterator_traits<HPC_user>' 
      requested here 
      typedef typename iterator_traits<_RandomAccessIterator>::value_type 
         ^
    db2class.cpp:119:3: note: in instantiation of function template specialization 'std::sort<HPC_user, bool (*)(const HPC_user &, 
      const HPC_user &)>' requested here 
     std::sort(users[0], users[len], cpuComp); 
    ^
In file included from db2class.cpp:3: 
In file included from /usr/include/c++/4.2.1/iostream:44: 
In file included from /usr/include/c++/4.2.1/ostream:44: 
In file included from /usr/include/c++/4.2.1/ios:47: 
In file included from /usr/include/c++/4.2.1/bits/ios_base.h:46: 
In file included from /usr/include/c++/4.2.1/bits/locale_classes.h:46: 
In file included from /usr/include/c++/4.2.1/string:56: 
In file included from /usr/include/c++/4.2.1/algorithm:67: 
/usr/include/c++/4.2.1/bits/stl_algo.h:2864:19: error: invalid operands to binary expression ('HPC_user' and 'HPC_user') 
     if (__first != __last) 
      ~~~~~~~^~~~~~~ 
db2class.cpp:122:3: note: in instantiation of function template specialization 'std::sort<HPC_user, cpuComp>' requested here 
    std::sort(users[0], users[len], cpuComp()); 
^
/usr/include/c++/4.2.1/bits/postypes.h:206:5: note: candidate template ignored: failed template argument deduction 
    operator!=(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs) 
    ^
/usr/include/c++/4.2.1/bits/stl_pair.h:109:5: note: candidate template ignored: failed template argument deduction 
    operator!=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) 
    ^
/usr/include/c++/4.2.1/bits/stl_iterator.h:294:5: note: candidate template ignored: failed template argument deduction 
    operator!=(const reverse_iterator<_Iterator>& __x, 
    ^
/usr/include/c++/4.2.1/bits/stl_iterator.h:344:5: note: candidate template ignored: failed template argument deduction 
    operator!=(const reverse_iterator<_IteratorL>& __x, 
    ^
/usr/include/c++/4.2.1/bits/allocator.h:120:5: note: candidate template ignored: failed template argument deduction 
    operator!=(const allocator<_T1>&, const allocator<_T2>&) 
    ^
/usr/include/c++/4.2.1/bits/basic_string.h:2188:5: note: candidate template ignored: failed template argument deduction 
    operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 

내가 무슨 일을 했는가 : 나는 코드를 컴파일 할 때

Sorting a vector in descending order

struct cpuComp 
{ 
    bool operator()(HPC_user const & a, HPC_user const & b) 
    { 
    return a.get_activity() > b.get_activity(); 
    } 
}; 

int main() 
{ 
    // do something; 
    std::vector<HPC_user> users = db2class(ins, days); 
    std::sort(users[0], users[len], cpuComp()); 
//..... 
} 

, 나는 약간의 오차가있어? 벡터의 클래스를 정렬하는 올바른 방법은 무엇입니까? 그래서 당신의 옵션은, 반복자 또는 포인터

std::sort(users.begin(), users.end(), cpuComp()); 

답변

6

이 원하는 요소 내에서 범위를 정하십시오.

반복자는 올바른 반복자를 사용하여 표준에서 std::vector이 필요로하는 방식으로 메모리에 데이터를 연속적으로 저장하지 않은 컨테이너를 정렬 할 수 있습니다. 반복기를 사용하여 시작한다면 나중에 컨테이너를 사용하도록 변경해야합니다. 나중에 정렬 선을 수정하지 않아도되며, 잊어 버리면 런타임에 오작동하지 않습니다. 따라서 표준 알고리즘으로 반복자를 사용하고 자신의 코드로 유사한 접근법을 사용하는 것이 가장 좋습니다.

+0

이것은 실제로 오류를 없앴습니다. 정말 벡터의 일부를 정렬하고 싶었습니다. 그래서 그것을 users.begin(), users.begin() + len으로 변경했습니다. 이제 작동합니다. – ddd

0

std::sort 작품 : 다음 & 사용자 [N] 형식으로

std::sort(users.begin(), users.end(), cpuComp()); 

std::sort(&users[0], &users[len], cpuComp()); // assuming len == users.size() 

, 그것은 하위를 지정 약간 쉽게

관련 문제