在数组 C++ 中搜索字符串索引
Search for string index in array C++
我试图在数组中找到元素的索引....我设法使用以下函数使其与整数一起使用:
int *getIndexOfInt(int *arr, int size, int check) {
int *result;
int *k;
int count = 0;
for (int i = 0; i <= size - 1; i++) {
if (arr[i] == check) {
k[count] = i;
count++;
}
}
if (count > 0) {
*result = *k;
return result;
} else
cout << "Not Found";
}
但是,当我对字符串尝试此操作时,它只会给我错误(程序以状态 11 退出)或无限循环:
int *getIndexOfString(string *arr, int size, string check) {
int *result;
int *k;
int count = 0;
for (int i = 0; i <= size - 1; i++) {
if (arr[i] == check) {
k[count] = i;
count++;
}
}
if (count > 0) {
*result = *k;
return result;
}
else cout << "Not Found";
}
你能告诉我为什么,也许可以帮助我修复错误吗?
编辑:结果变量是然后在主函数中使用的数组,它包含在给定数组中找到字符串的索引。k 变量只是一个数组,其中的值在添加到结果中之前被存储。arr 是给定的字符串数组,大小是给定的大小,检查是代码将搜索的字符串。
首先,您正在访问未初始化的内存。奇怪的是,你的第一个代码有效。但是,它可能是特定于编译器的(这些事情经常发生C++)。
局部变量通常在堆栈上分配,C++不能保证您有任何默认值。因此,一种可能的解释是(在保存指针的同一内存地址上)存在另一个有效的指针。现在,当你创建这个局部变量时,它只是得到了这个"旧"地址,所以它正在访问一些以前分配的内存。只是目前不在乎它,即使它有效,相信我们 - 你不应该依赖这个。:-)
另一个问题是该返回值。当您不知道该数组的大小时,您将如何使用它?你应该返回类似 std::vector<>、一些结构或类似的东西。不仅仅是指向未知长度的数组的指针!
结果:你的代码太复杂了。查看更好的解决方案:
#include <iostream>
#include <string>
#include <vector>
std::vector<int> getIndexes(std::vector<std::string> &input, std::string searched) {
std::vector<int> result;
for (int i = 0; i < input.size(); i++) {
if (input[i] == searched) {
result.push_back(i);
}
}
return result;
}
int main(int argc, char *argv[]) {
std::vector<std::string> greetings;
greetings.push_back("hello");
greetings.push_back("hi");
greetings.push_back("bye");
greetings.push_back("hi");
greetings.push_back("hello");
greetings.push_back("bye");
std::vector<int> indexes = getIndexes(greetings, "hi");
for (int i = 0; i < indexes.size(); i++) {
std::cout << indexes[i] << std::endl;
}
return 0;
}
其他人提出了它,我把它放在这里以供参考。
您可以使用标准库。特别是算法std::find
:
#include<vector>
#include<string>
#include<iostream>
#include<algorithm>
int main() {
std::vector<std::string> words = {"one", "two", "three", "four", "five"};
size_t index = std::distance(words.begin(),
std::find(words.begin(), words.end(), "three"));
std::cout<<"Index: "<<index<<std::endl;
}
编译为 (GCC 4.8.1 OS X 10.7.4):
g++ indices-in-array.cpp -std=c++11
输出:
Index: 2
您的主要问题是您没有初始化指向有效数组的结果指针。实际上,虽然您应该返回索引向量而不是指针,但这样调用方就知道大小,而不必手动管理内存。像这样:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;
vector<int> getIndicesOfString(const vector<string>& in, const string& check) {
vector<int> ret;
for (auto it = cbegin(in); it != cend(in); ++it) {
if (*it == check) ret.push_back(distance(cbegin(in), it));
}
return ret;
}
int main() {
auto v = vector<string>{"red", "orange", "yellow", "green", "blue", "indigo", "violet", "red"};
auto indices = getIndicesOfString(v, "red");
copy(cbegin(indices), cend(indices), ostream_iterator<int>(cout, ", "));
}
编辑用于矢量:
std::vector<int> getIndexOfString(string *arr, int size, string check)
{
std::vector<int> iVect;
for (int i = 0; i <= size - 1; i++)
{
if (arr[i] == check)
{
iVect.push_back (i);
cout << "Found at " << i << endl;
}
}
if (iVect.empty)
cout << "not found" << endl;
return iVect;
}
您的函数有太多未初始化的指针。是否确实要返回索引或指针?您还缺少失败案例的返回值。
- 按字母顺序对C++问题中的子字符串索引进行分区
- C++ 中字符串向量的索引
- C++ - 成功打印超出范围的字符串索引
- 对于循环增量器不能用作字符串向量的索引
- 如何检索由带通配符的字符串索引的对象
- 访问包含P的有效索引时返回空格的C++字符串
- 在我的 for 循环中,如何将索引更改为字符串?
- 有效地从文本文件中读取带有字符串索引的大型二维数组(矩阵)
- 如何使用调整后的字符串索引第二次通过循环运行字符串
- C++中的字符串索引问题
- Qt5.5 Q奇数结果的字符串索引
- 是否有理由使用字符串 => 索引到向量的映射,而不是字符串 => 对象?
- 在数组 C++ 中搜索字符串索引
- C++:字符串索引和保留标点符号时出现问题
- C++中类似python的字符串索引
- 如何在c++中高效地实现PHP字符串索引数组
- 如果访问了错误的字符串索引,c++不会抛出错误
- C++中的 Unicode 字符串索引
- 如何按索引为 c++ 字符串索引赋值
- 在 C++ 中使用字符串索引的整数