是否可以使用 std::sort
内部定义 <algorithm>
用于排序 char
数组根据他们的 ASCII 值?如果是,请举例说明。
最佳答案
是的。那绝对有可能。您只需编写一些示例代码就可以知道,例如:
char charArray[] = {'A','Z', 'K', 'L' };
size_t arraySize = sizeof(charArray)/sizeof(*charArray);
std::sort(charArray, charArray+arraySize);
//print charArray : it will print all chars in ascending order.
顺便说一下,你应该避免使用c风格的数组,而应该更喜欢使用std::array
或std::vector
.
std::array
用于在编译时本身知道大小的情况,而 std::vector
用于需要大小为的动态数组时在运行时已知。
关于c++ - 将 'sort' 与字符数组一起使用 (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14111841/