C++ 正在尝试对字符串执行索引搜索
我正在尝试编写一个代码,允许我在提示时搜索我输入的数组字符串。程序要求用户输入一定数量或数据,然后要求用户执行搜索。我很难找到输入。我可以用整数来做这件事,但现在字符串请帮忙。你会怎么做C++ 正在尝试对字符串执行索引搜索,c++,visual-c++,C++,Visual C++,我正在尝试编写一个代码,允许我在提示时搜索我输入的数组字符串。程序要求用户输入一定数量或数据,然后要求用户执行搜索。我很难找到输入。我可以用整数来做这件事,但现在字符串请帮忙。你会怎么做 #include <iostream> using namespace std; void contactArray(string a[], int size); string search(const string a[], int size, string find); int main
#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";
}
#包括
使用名称空间std;
void contactArray(字符串a[],整数大小);
字符串搜索(常量字符串a[],整数大小,字符串查找);
int main()
{
找不到;
字符串查找=搜索(a,arraySize,find);
如果(查找==“”)
CUT< P>使用循环for循环数组,检查是否有数组中的值。可能有一个C++函数来完成这个CSTDLB,我知道.NET有这个内置的。< /P> < P>可以用整数来做。用整数做,用字符串调用那个函数,并且在哪里得到一个<代码>不能把字符串转换成int < /COD>编译器。错误,请将单词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搜索(常量字符串a[],int大小,字符串查找)
{
字符串索引=”;
while((a[index[3]!=find)和&(index 这是作业吗?如果是,请添加作业
标记
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;
}