#include <iostream>
#include <array>
#include <algorithm>
#include <cstring>
using namespace std;
int main() {
array<char, 20> test{"HelloWorld"}, tmp;
// method 1
tmp = test;
sort(tmp.begin(), tmp.begin() + strlen(tmp.begin()));
cout << "result: " << tmp.data() << endl;
// method 2
tmp = test;
sort(tmp.begin(), tmp.end());
cout << "result: " << tmp.data() << endl;
return 0;
}
std::array
可以用方法1进行排序。但它很难看。如何使用std :: sort对std :: array进行排序?
我更喜欢方法2,但什么都不能返回。它出什么问题了?
这两个'sort'调用做不同的事情。使用你需要的那个。 –
这里的根本问题是你试图让'std :: array'成为'std :: string'。这不是,而且试图用它作为一个永远不会很好的工作。 –
为什么不使用'std :: string'?然后方法2将正常工作。 –