将char数组转换为int数组

时间:2014-06-11 09:35:41

标签: c++ arrays visual-c++

我有一个我想要sort的字符数组。问题是我是否将数组原样传递给sort函数,而不是实际转换数组字符,而这些字符实际上是ASCII等价数字。对于例如4成为52。

std::vector<int> classStudents;
....
char* cend = cAllowedStudents+maxAllowedStudents;
std::sort(cAllowedStudents, cend);
std::set_difference(classStudents.begin(), classStudents.end(),cAllowedStudents, cend,std::back_inserter(diff));

我也尝试通过这个将整个数组转换为单独的int数组(但理想情况下我不想使用另一个数组,而只是作为最后一个选择):

iAllowedStudents[i]=(int)cAllowedStudents[i];

但它也是一样的,所以如何将此cAllowedStudents转换为与std::set_difference一起使用

1 个答案:

答案 0 :(得分:1)

使用:

template< class RandomIt, class Compare >
void sort( RandomIt first, RandomIt last, Compare comp );

并传递自定义比较函数