试图对字符串执行索引搜索
trying to perform an index search for string
我试图写一个代码,应该允许我搜索数组的字符串,我输入时提示。该程序要求用户输入一定数量或数据,然后要求用户执行搜索。我有一个艰难的时间搜索输入。我可以做到这一点与整数,但现在字符串请帮助。你会怎么做呢?
#include <iostream>
using namespace std;
void contactArray(string a[], int size);
string search(const string a[], int size, string find);
int main( )
{
cout << "This program searches a list .n";
const int arraySize = 3;
string a[arraySize];
contactArray(a, arraySize);
string find;
cout << "Enter a value to search for: ";
cin >> find;
string lookup = search(a, arraySize, find);
if (lookup == " ")
cout << find << " is not in the array.n";
else
cout << find << " is element " << lookup << " in the array.n";
return 0;
}
void contactArray(string a[], int size)
{
cout << "Enter " << size << " list.n";
for (int index = 0; index < size; index++)
cin >> a[index];
}
int search(const string a[], int size, string find)
{
string index = "";
while ((a[index[3]] != find) && (index < size))
cout<<"try again"<<endl;
if (index == find)
index = "";
return index;
cout<<"hgi";
}
你可以用整数做吗?用整数来做,用字符串来调用这个函数,当你得到一个cannot convert string to int
编译错误时,把int
这个词改成string
。
代替
int search(const string a[], int size, string find)
{
string index = "";
while ((a[index[3]] != find) && (index < size))
cout<<"try again"<<endl;
if (index == find)
index = "";
return index;
cout<<"hgi";
}
试试
int search(string a[], int size, string find)
{
int index = -1;
for(int i=0;i<size;i++)
{
if(a[i] == find)
{
index = i;
break;
}
}
return index;
}
如果函数返回-1,表示没有找到字符串。任何其他返回值是'find'字符串在数组中所在的位置。
使用for循环遍历数组并检查值是否在数组中。可能有一个c++函数来做这个cstdlib,我知道。net有这个内置的
相关文章:
- 如何二进制搜索结构向量并在适当的索引处插入
- 从数组中搜索元素的索引
- 如何使用二叉搜索返回姓氏(给定字符串)的第一个索引/出现次数?
- 如何使该程序在数组中打印搜索字符的索引,否则将找不到它
- 如何使用二叉搜索树的索引生成器解决此问题
- 二叉搜索功能始终返回 -1 值而不是索引
- 使用二叉搜索查找数字的第 N 次出现的索引
- 在Boost多索引容器中搜索位字段数据
- 通过索引快速搜索,并将插入顺序保持在C 中
- 在排序数组 X 中搜索第一个索引 i,使得 X[i] >= a
- O(log n) 索引更新和搜索
- 二进制搜索返回其所属位置的索引
- 在数组 C++ 中搜索字符串索引
- 使用分而治之搜索字符串中'xy'的第一个索引
- C++用于搜索和索引的框架,如 clucene
- 基于partial_index_search的结果来增强multi_index_container部分索引搜索
- 如何从数组执行索引搜索
- 如何执行索引搜索方法
- 试图对字符串执行索引搜索
- 用std::set替换std::map并按索引搜索