我有一个要按字母顺序排序的向量。我已经成功地能够按字母顺序按一个索引值对其进行排序,但是当我这样做时,它只会更改该索引的顺序,而不是整个向量。我怎样才能让它将订单更改应用于整个向量?这是我正在运行的当前代码:
std::sort (myvector[2].begin(), myvector[2].end(), compare);
bool icompare_char(char c1, char c2)
{
return std::toupper(c1) < std::toupper(c2);
}
bool compare(std::string const& s1, std::string const& s2)
{
if (s1.length() > s2.length())
return true;
if (s1.length() < s2.length())
return false;
return std::lexicographical_compare(s1.begin(), s1.end(),
s2.begin(), s2.end(),
icompare_char);
}
我对这个向量的一般结构是 vector[row][column] 其中:
| One | Two | Three |
| 1 | 2 | 3 |
| b | a | c |
例如,如果我有一个向量:
myvector[0][0] = 'One' AND myvector[2][0]='b'
myvector[0][1] = 'Two' AND myvector[2][1]='a'
myvector[0][2] = 'Three' AND myvector[2][2]='c'
| One | Two | Three |
| 1 | 2 | 3 |
| b | a | c |
我对它进行排序,我得到:
myvector[0][0] = 'One' AND myvector[2][0]='a'
myvector[0][1] = 'Two' AND myvector[2][1]='b'
myvector[0][2] = 'Three' AND myvector[2][2]='c'
| One | Two | Three |
| 1 | 2 | 3 |
| a | b | c |
而不是我想要的:
myvector[0][0] = 'Two' AND myvector[2][0]='a'
myvector[0][1] = 'One' AND myvector[2][1]='b'
myvector[0][2] = 'Three' AND myvector[2][2]='c'
| Two | One | Three |
| 2 | 1 | 3 |
| a | b | c |
我环顾四周寻找一个好的方法,但找不到任何有效的方法......我在想类似的事情:
std::sort (myvector.begin(), myvector.end(), compare);
然后在我的比较函数中处理第三个索引的排序,以便编辑整个向量......但是当我传递我的数据时,我要么只更改了函数中的顺序,但仍然没有更改顶层或出错。任何建议或帮助将不胜感激。先感谢您。