"如何使用C++将字符串的第一个和最后一个索引返回到向量中?
"How do I return the first and last index of a string, into a vector, with C++?
这是来自Edabit的编码问题。我试图专注于我解决问题的能力,特别是C++。
问题是从字符串返回字符的第一个和最后一个索引。 对于 std::string word(第一个参数(中 char c(第二个参数(的每个实例,我认为我需要将 (char c( 与 push_back 推送到一个空字符串,并从该空字符串返回第一个和最后一个索引。或者我可以简单地使用 (例如:std::string word.being()
(?
到目前为止,我已经在下面包含了一些代码。我只是坚持如何用语法实现我上面的想法。我想我的想法是正确的,但不知道该怎么做。我不想要确切的答案,只是下一步该怎么做的指南。
std::vector<int> charIndex(std::string word, char c) {
std::string newWord = "";
for(int i = 0; i < word.size(); ++i){
std::string size.push_back[i] = newWord;
//for every instance of char c in std::string word, I need to
//push that (char c) to empty string and return first and last index
if(newWord[i] == )
}
}
以下是预期结果的示例:
charIndex("circumlocution", "c") ➞ [0, 8]
The first "c" has index 0, the last "c" has index 8.
charIndex("happy", "h") ➞ [0, 0]
Only one "h" exists, so the first and last index is 0.
知道了!!
vector<int> arr;
arr[0] = word.find_first_of(c);
arr[1] = word.find_last_of(c);
return arr;
谢谢大家!!
相关文章:
- 在 C++ 中访问数组负索引处的内存不会返回垃圾
- 保持排序的数据结构,允许log N插入时间,并且可以返回我在log N中查找的元素的索引
- 为什么 min_element() 返回最小元素的索引,而不是迭代器?
- 如何在C++中返回向量的从零开始的索引
- 返回向量的单个索引值
- C++函数返回两个 char 数组的相同索引元素
- 访问包含P的有效索引时返回空格的C++字符串
- 当我在网格/面中实现索引时,为什么它会返回OpenGl错误
- 为什么函数的任何索引处的下标运算符在C++中总是返回1
- 如何使用递归函数返回数组中整数的索引?
- "如何使用C++将字符串的第一个和最后一个索引返回到向量中?
- 当索引为 1 时,_BitScanReverse返回 0,这意味着根据 MS "no set bits were found"
- 从给定索引返回子字符字符串的函数
- 从字符串返回 i(索引)字
- 如何从函数返回索引?
- 如何使minmax_element在平局的情况下返回索引最少的元素
- QmetaObject :: IndexOfMethod返回到底返回索引
- boost ::几何查询返回索引
- 如何返回索引位置而不是值
- 遍历查找节点中的顶点并返回索引