나는 정렬해야하는 앞 부분에 숫자가 들어있는 std :: strings를 가지고있다. 숫자는 정수 또는 실수 일 수 있습니다.소수 정렬 자연 정렬 프로그램을 향상시키는 방법?
정렬이 최적이 아니므로 다음과 같은 자연 정렬 프로그램이 훨씬 좋았습니다. 나는 여전히 숫자가 0보다 작은 작은 문제가 있는데, 바로 정렬하지는 않습니다. 누구든지 개선 할 제안이 있습니까? 우리는 Visual Studio 2003을 사용하고 있습니다.
전체 프로그램이 이어집니다.
TIA, 버트
#include <list>
#include <string>
#include <iostream>
using namespace std;
class MyData
{
public:
string m_str;
MyData(string str) {
m_str = str;
}
long field1() const
{
int second = m_str.find_last_of("-");
int first = m_str.find_last_of("-", second-1);
return atol(m_str.substr(first+1, second-first-1).c_str());
}
long field2() const
{
return atol(m_str.substr(m_str.find_last_of("-")+1).c_str());
}
bool operator < (const MyData& rhs)
{
if (field1() < rhs.field1()) {
return true;
} else if (field1() > rhs.field1()) {
return false;
} else {
return field2() < rhs.field2();
}
}
};
int main()
{
// Create list
list<MyData> mylist;
mylist.push_front(MyData("93.33"));
mylist.push_front(MyData("0.18"));
mylist.push_front(MyData("485"));
mylist.push_front(MyData("7601"));
mylist.push_front(MyData("1001"));
mylist.push_front(MyData("0.26"));
mylist.push_front(MyData("0.26"));
// Sort the list
mylist.sort();
// Dump the list to check the result
for (list<MyData>::const_iterator elem = mylist.begin(); elem != mylist.end(); ++elem)
{
cout << (*elem).m_str << endl;
}
return 1;
}
GOT : 예상 된
0.26
0.26
0.18
93.33
485
1001
7601
: 그냥 문자열을 떠 있다면
0.18
0.26
0.26
93.33
485
1001
7601
훌륭합니다. – Quadmore