使用“排序”用字符数组(C ++) [英] Use 'sort' with char array(C++)
问题描述
是否有可能使用'排序'算法'中定义为根据自己的ASCII值排序字符数组?如果有,请提供一个例子。
Is it possible to use 'sort' defined inside 'algorithm' for sorting char arrays according to their ASCII value? If yes, please provide an example.
推荐答案
是的。这是绝对有可能。你可能知道,只是写了一些样本code,像这样的:
Yes. That is definitely possible. You could know that just by writing some sample code, such as this:
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风格的数组,并应使用preFER 的std ::阵列
或的std ::矢量
。
By the way, you should avoid using c-style arrays, and should prefer using std::array
or std::vector
.
当你知道在编译时间本身大小的std ::阵列
被使用,而的std ::矢量
是当你需要动态数组的大小在运行时被称为使用。
std::array
is used when you know the size at compile-time itself, while std::vector
is used when you need dynamic array whose size will be known at runtime.
这篇关于使用“排序”用字符数组(C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!