【问题标题】:Converting a char array into an int array [duplicate]将char数组转换为int数组[重复]
【发布时间】:2014-06-11 09:35:41
【问题描述】:

我有一个要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 一起使用

【问题讨论】:

  • classStudents的类型是什么?
  • 接受您昨天发布的the related question 的答案之一怎么样?
  • 排序时比较应该如何工作?是'a' &lt; '4' 还是'a' &gt; '4'
  • 您的问题不是很清楚,为什么要将整数数组与字符数组进行比较?字符序列中感兴趣的信息是什么?也许如果你添加一个例子,那会更清楚一点......就目前而言,投票结束......
  • @Maven 不,我看到你所有的 cmets 都回答了;实际上you didn't answers comments on clarifying the question further,你期待答案:)

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


【解决方案1】:

用途:

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

并传递自定义比较函数

【讨论】:

    猜你喜欢
    • 2011-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    • 2013-12-11
    • 1970-01-01
    • 2020-11-12
    • 1970-01-01
    相关资源
    最近更新 更多