현재 문자열 인해 설계 제약 조건에 문자 또는 숫자를 (포함 할 수 있습니다오버로딩을 비교 한 연산자 "잘못된 연산자는 <"
++ C에서, 문자열을 포함하는 각 객체와 객체의 벡터를 정렬 시도 이것은 비교기가 변경 될 수 있기 때문에 필요합니다).
현재 개체의 클래스가 오버로드되어 두 개체가 비교 될 때 포함 된 문자열이 비교됩니다. 이 점은 한 가지로 작용합니다. 그러나 STL 정렬과 같은 정렬 연산을 사용하여 객체를 순서대로 정렬하면 "1", "4", "12"와 같은 세 개의 문자열을 순서대로 정렬합니다 "1", "12", "4". 4는 12보다 크지 만 가장 왼쪽 숫자와 비교하기 시작하기 때문에이 '잘못된'정렬이 발생합니다.
내 초기 응답은 비교 작업을 오버로드하는 방법을 변경하는 것이 었습니다. 먼저 비교할 문자열의 길이를 확인합니다. 문자열의 내용이 크거나 작 으면 표시가됩니다. A의
// overloaded comparision operators
friend bool operator<(const nodeRecord & record1, const nodeRecord & record2){
// we need to deal with strings of different lengths...
if(record1.comparator.length() < record2.comparator.length())
return true;
else
return (record1.comparator < record2.comparator);
}
이 작업의 결과 "표현 : 잘못된 연산자 <"런타임 동안 메시지.
내가 실수하고있는 부분에 대한 아이디어가 있습니까? 정렬 작업을 원하는 방식으로 작업을 정확하게 지시 할 수 있어야합니다. 현재 개체를 포함하는 벡터를 사용하고 있기 때문에 유효하지 않은 경우에도 정렬 작업을 수행해야합니다. nodeRecord 객체의 초기화 중
비교기 :
단일 비교기를 사용하고 그 기능을 좀 더 똑똑하지 않는 이유는nodeRecord(int fromNode, int toNode, int connectionCost, bool compareByCost = false){
// take the provided stock information and insert it into the object
stringstream fromNodeSS;
fromNodeSS << fromNode;
this->fromNode = fromNodeSS.str();
stringstream toNodeSS;
toNodeSS << toNode;
this->toNode = toNodeSS.str();
this->connectionCost = connectionCost;
// set the comparator to our chosen comparision term
if (!compareByCost){
this->comparator = this->fromNode; // we use from node in this case, since we build the tree outwards
}
else{
stringstream ss;
ss << this->connectionCost;
this->comparator = ss.str(); // we use the connection cost in this case, to allow us to sort new connections
}
// set this as a non-null (active) record
this->nullRecord = false;
}
비교기는 무엇인가? 그 코드를 게시하십시오. –
비교기의 정의를 보여 주실 수 있습니까? –
@Mike 및 @Mario - 비교기는 nodeRecord 객체를 초기화하는 동안 초기화됩니다. 위의 내용을 볼 수 있습니다. – BSchlinker