사용자 정의 클래스에서 Compare연산을 위한 연산자 작성에 대해..
글쓴이: vuscon / 작성시간: 금, 2007/04/27 - 9:46오전
안녕하세요 매번 눈팅만 하다가 이번에 질문을 올려봅니다. :)
STL에서 제공하는 Priority Queue를 비슷하게 구현해보려고 하는데 문제가 생기는군요;;
컨테이너 클래스 입니다.
class Node {
public:
// Ctor
Node() : _freq(0), _char('\0'), _left(0), _right(0) {}
Node(const int& f, const char& c) : _freq(f), _char(c) {}
// Copy Ctor
Node(const Node& x) : _freq(x._freq), _char(x._char) {}
// Dtor
virtual ~Node() {}
// Get methods
int getFreq() { return _freq; }
char getChar() { return _char; }
// Set methods
void setFreq(const int& f) { _freq = f; }
void setChar(const char& c){ _char = c; }
void setLeft(Node* l){ _left = l; }
void setRight(Node* r){ _right = r; }
// Operator override
bool operator==(const Node& x) {
return (this->_freq == x._freq && this->_char == x._char);
}
bool operator<(const Node& x) {
if(this->_freq < x._freq) return true;
else return false;
}
bool operator>(const Node& x) {
if(this->_freq > x._freq) return true;
else return false;
}
private:
int _freq;
char _char;
Node* _left;
Node* _right;
};Priority Queue구현부분입니다.
// Container adaptor of priority queue
template <typename T, typename Container = vector<T>,
typename Compare = less<typename Container::value_type> >
class pQueue {
blahblah...
blahblah...
private:
// internal values
int _heapsize;
T _buf;
Container _container;
// Compare (*_compare)(const T& x, const T& y);
}Compare 타입네임을 도대체 어떻게 써야될지 모르겠습니다. Functor로 받자니 계속 오류가 생기구요.
pQueue<Node> list1;
pQueue<Node, vector<Node>, greater<Node> > list;
근데 greater에 대한 함수를 찾을수 없다며 오류가 계속 뜨는군요 :(
현재 페도라6에 g++ 4.1.11 버젼을 사용하고 있습니다.
고수님들의 친절하신 답변 부탁드립니다.
Forums:


자세한 건 안봤지만,
자세한 건 안봤지만, 일단 멤버함수에 const 붙일 수 있는 건 다 붙이세요. 크기비교 연산 같은 건 객체의 상태를 바꾸는 연산이 아니니 전부다 const ㄱㄱㅅ
[예진아씨 피카사 웹앨범] 임예진 팬클럽 ♡예진아씨♡ http://cafe.daum.net/imyejin
include functional?
과 함께 std::greater?
댓글 달기