我想使用插入排序來排序字符串向量。用字符串向量插入排序
這是我的代碼:
void insertionsort(std::vector<std::string> &strings)
{
typedef std::vector<std::string>::size_type size_type;
for(size_type i = 0;i < strings.size(); i++)
{
std::string const tmp = strings[i];
size_type j = i - 1;
while(j >= 0 && tmp < strings[j]) //this is the problem
{
strings[j + 1]= strings[j];
j--;
}
strings[j + 1]=tmp;
}
}
它給我的錯誤:如果我使用J>時0
comparison of unsigned expression >= 0 is always true
功能工作正常,但它完全忽略該字符串的第一行。
舉例來說,如果我有:
2 line1
3 line2
4 line3
5 line4
1 line5
然後它給了我:
2 line1
1 line5
3 line2
4 line3
5 line4
使用簽名類型。 (這不是無可爭議的,但標準委員會的幾位知名成員都與我一起參與)。 –