VC6 에서 std::list::sort() 사용

litnsio2의 이미지

VC 로 작업하다 생긴 문제점을 질문해도 될런지 모르겠군요.
list 를 소트하려고 하는데 VC6 으로는 문제가 이만저만이 아니군요.
알아보니 VC6에선 STL 표준이 확립되기 전에 지들맘대로 만들어놔서 그렇다던데..
암튼.. 문제점을 요약하자면, 두개의 클래스가 있습니다.

class person {
   string name;
   string email;
    ...
};
class student : public person {
   string addres;
   ...
};
typedef std::list<student> studentlist;

studentlist 에 저장된 student 객체를 정렬하려는데 부모클래스(person)의 name 멤버를 기준으로 정렬하려고 합니다. 상황에 따라 email 이나 address 와 같이 다른 멤버로 정렬을 해야하구요.

일단 algorithm 에 있는 sort 를 사용하니까 안되더군요. 당연한거였습니다.
list 에 따로 list 만의 sort가 있었으니까요.
(Effective STL 을 보면 std::sort() 함수만으로 리스트를 정렬시키는
편법이 나와있습니다만, 저는 std::list::sort() 함수를 사용하기로 했습니다.
오기가 나서 그런지.. 이걸로 꼭 해결해야겠다는 마음이..+_+ )

list::sort() 를 사용하는데 두가지 버전이 있더군요. list::sort() 와 list::sort(Compreobj) 였습니다.

전자는 리스트 속에 들어간 객체의 operator< 함수를 사용해서 앞뒤를 가려 정렬하는 것이고 후자는 비교객체를 이용하는 것이라는건 알고계시죠.?

일단 후자의 방법을 사용하기로 하고 다음과 같은 함수를 정의하였습니다.
(펑크터로 사용하기 위해서)

bool comparestudentname(student s1, student s2)
{
   return s1.getname() < s2.getname();
}

하지만 VC6 요녀석이 에러를 내더군요.

Quote:

e:\my documents\program source files\vc++ source files\잡다한거\term_2\datamanager.cpp(79) : error C2664: 'void __thiscall std::list<class Student,class std::allocator<class Student> >::sort(struct std::greater<class Student>)' : cannot convert parameter 1 from 'bool (class Student,class Student)' to 'struct std::greater<class Student>'
No constructor could take the source type, or constructor overload resolution was ambiguou

그말인즉슨, 펑크터를 사용할수 없다는 말인것 같았습니다. VC6 자신은 std::greater<Student> 를 사용하겠다 이거죠.. 글고 전달된 함수객체가
std::greater<Student> 로 변환이 불가능하다.. 그래서 못한다... 쳇....
왜이리 까탈스러운건지 -_-

그래서 이곳저곳을 뒤진끝에 std::greater<Student> 를 상속받는 구조체를 하나
만들기로 했습니다.

struct greater_stdnt_name : public std::greater<Student> {
   // s1, s2도 const 로 하면 부모클래스의 멤버함수인 getname() 을 호출할수 없더군요.. ^.^a
   bool operator()(Student s1, Student s2) const {
       return s1.getname() < s2.getname();
   }
};
studentlist.sort(greater_stdnt_name);

하지만 이도 역시 실패. 다음과 같은 불평을 내뱉더군요.

Quote:

E:\My Documents\Program Source Files\Vc++ Source Files\잡다한거\term_2\DataManager.cpp(81) : error C2275: 'greater_stdnt_name' : illegal use of this type as an expression
E:\My Documents\Program Source Files\Vc++ Source Files\잡다한거\term_2\DataManager.cpp(72) : see declaration of 'greater_stdnt_name'

expression 으로 사용하는건 잘못되었다는것 같았는데.. studentlist.sort() 를 호출하는 부분을

// studentlist.sort(greater_stdnt_name); 에서
studentlist.sort(greater_stdnt_name() ); // 로 바꾸어보니 

이번엔 수많은 에러가 저의 눈을 괴롭히더군요..+_+
list 내부에서 나는 에러들이었습니다.

아.. 이건 무언가 문제가 있다는걸 감지하고 처음에 있는 두 가지 방법 중에서
첫번째 방법을 사용하기로 하였습니다. operator< 함수를 이용하는 것이죠.
일단 operator< 함수는 두가지로 구현할수 있습니다. 전달인자가 하나인것과
전달인자가 두개인것..
전달인자가 하나인 경우는 클래스의 멤버함수이고 두개인 경우는 friend 함수 입니다.

두가지 각각 구현해 보았는데, 이렇게 하면 studentlist.sort() 를 호출하지 않아도
list 자체에서 에러가 나더군요. studentlist.sort() 가 아예 없는데 왜 에러가 나는건지..

stlport 를 설치하면 vc6 에서 제대로 사용이 가능하다는 말을 들었지만,
그걸 사용하지 않고 순수하게 vc6 으로만 std::list::sort(Comp) 를 제대로 사용할 수는 없을까요?

ㅠㅠ

긴글 읽어주셔서 감사합니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.