【发布时间】:2017-01-17 00:59:41
【问题描述】:
我需要返回 const 字符数组中最后一次出现的字符。因此,如果我有一个 const 字符数组,即 ["helloe"] 并且我需要返回的 char 索引是 "e",它将返回 5。
//s is a const array of chars that equals ["helloe"]
// c is the char "e"
// I need to return the index of the last occurrence of e which is 5
int reverse_find_character(const char s[], char c){
std::vector<int> no;
size_t bob = strlen(s);
size_t i;
for (i=bob;i>bob;i++){
if (s[i]==c){
no.push_back((int)i);
}
return *max_element(no.begin(),no.end());
}
【问题讨论】:
-
一个更简单的计划是返回第一次出现,但使用反向迭代器
-
您的问题/疑问是什么?
-
size会比bob更好。 -
如果只想找到一个值,为什么还需要向量?
标签: c++ arrays for-loop indexing char