C++ 在字符串数组中搜索
所以我有一组字符串,它们存储在一个数组中,我想搜索数组,所以当找到字符串时,它应该说C++ 在字符串数组中搜索,c++,arrays,string,if-statement,for-loop,C++,Arrays,String,If Statement,For Loop,所以我有一组字符串,它们存储在一个数组中,我想搜索数组,所以当找到字符串时,它应该说found,当它没有找到时,它应该说invalid 这就是我目前所拥有的 cout << "Enter a Name to Search" <<endl; cin >>userInput; for (int i=0; i<size; i++) { if (first_name[i]==userInput) { cout <<"F
found
,当它没有找到时,它应该说invalid
这就是我目前所拥有的
cout << "Enter a Name to Search" <<endl;
cin >>userInput;
for (int i=0; i<size; i++)
{
if (first_name[i]==userInput)
{
cout <<"Found"<<endl;
}else{
cout << "InValid"<<endl;
break;
}
}
cout所以我找到了解决方案这就是我所做的
string result =""; //set a string named result
cout << "Enter a Name to Search" <<endl;
cin >>userInput;
for (int i=0; i<size; i++)
{
if (!(first_name[i]==userInput))
{
result = "Found";
break;
}else{
result ="InValid";
}
}
cout <<result<<endl; //outside of for loop
字符串结果=”//设置一个名为result的字符串
所以我找到了解决办法这就是我所做的
string result =""; //set a string named result
cout << "Enter a Name to Search" <<endl;
cin >>userInput;
for (int i=0; i<size; i++)
{
if (!(first_name[i]==userInput))
{
result = "Found";
break;
}else{
result ="InValid";
}
}
cout <<result<<endl; //outside of for loop
字符串结果=”//设置一个名为result的字符串
不能使用像std::set
和std::unordered\u set
这样的容器进行快速搜索
#include <iostream>
#include <unordered_set>
#include <string>
int main()
{
std::unordered_set<std::string> first_name;
first_name.insert("Joe");
first_name.insert("Anderson");
//....
std::string input;
std::cin >> input;
std::unordered_set<std::string>::iterator searchResult = first_name.find(input); // Search for the string. If nothing is found end iterator will be returned
if(searchResult != first_name.end())
std::cout << "Found!" << std::endl;
else
std::cout << "Not found!" << std::endl;
}
#包括
#包括
#包括
int main()
{
std::无序的\u集合第一个\u名称;
姓名。插入(“乔”);
姓名。插入(“安德森”);
//....
std::字符串输入;
std::cin>>输入;
std::unordered_set::iterator searchResult=first_name.find(input);//搜索字符串。如果未找到任何内容,则返回end iterator
if(searchResult!=first_name.end())
std::cout使用诸如std::set
和std::unordered_set
之类的容器进行快速搜索
#include <iostream>
#include <unordered_set>
#include <string>
int main()
{
std::unordered_set<std::string> first_name;
first_name.insert("Joe");
first_name.insert("Anderson");
//....
std::string input;
std::cin >> input;
std::unordered_set<std::string>::iterator searchResult = first_name.find(input); // Search for the string. If nothing is found end iterator will be returned
if(searchResult != first_name.end())
std::cout << "Found!" << std::endl;
else
std::cout << "Not found!" << std::endl;
}
#包括
#包括
#包括
int main()
{
std::无序的\u集合第一个\u名称;
姓名。插入(“乔”);
姓名。插入(“安德森”);
//....
std::字符串输入;
std::cin>>输入;
std::unordered_set::iterator searchResult=first_name.find(input);//搜索字符串。如果未找到任何内容,则返回end iterator
if(searchResult!=first_name.end())
std::cout我认为更优雅的解决方案应该使用布尔标志,如:
cout << "Enter a Name to Search" <<endl;
cin >>userInput;
bool found = false;
for (int i=0; i<size; i++)
{
if (first_name[i]==userInput)
{
found = true;
break;
}
}
cout << (found?"found":"invalid") << endl;
cout我认为更优雅的解决方案应该使用布尔标志,如:
cout << "Enter a Name to Search" <<endl;
cin >>userInput;
bool found = false;
for (int i=0; i<size; i++)
{
if (first_name[i]==userInput)
{
found = true;
break;
}
}
cout << (found?"found":"invalid") << endl;
cout您正在从else部分中断。因此,例如,如果数组大小为10,并且如果您在第5个数组元素中以字符串形式提供userinput,那么您的代码将在for循环的第一次迭代中中断。请尝试下面的代码。如果找到匹配项,它将打印“find”,或者如果userinput不在数组中,它将打印无效。希望有帮助。使用数组元素初始化“first_name”并更改大小
string userInput;
string first_name[10];
int i=0;
int size = 10;
first_name[0] = "Hi";
first_name[1] = "Hii";
first_name[2] = "Hiii";
cout << "Enter a Name to Search" <<endl;
cin >> userInput;
for (i = 0; i<size; i++)
{
if (first_name[i] == userInput)
{
cout <<"Found"<< endl;
break;
}
}
if(i == size)
cout << "Invalid" << endl;
stringuserinput;
字符串名[10];
int i=0;
int size=10;
名字[0]=“你好”;
名字[1]=“Hii”;
名字[2]=“Hiii”;
cout您正在从else部分中断。因此,例如,如果数组大小为10,并且如果您在第5个数组元素中以字符串形式提供userinput,那么您的代码将在for循环的第一次迭代时中断。请尝试以下代码。如果找到匹配项,它将打印“找到”,或者如果userinput不在数组中,它将打印无效。希望有帮助。使用数组元素初始化“first_name”并更改大小
string userInput;
string first_name[10];
int i=0;
int size = 10;
first_name[0] = "Hi";
first_name[1] = "Hii";
first_name[2] = "Hiii";
cout << "Enter a Name to Search" <<endl;
cin >> userInput;
for (i = 0; i<size; i++)
{
if (first_name[i] == userInput)
{
cout <<"Found"<< endl;
break;
}
}
if(i == size)
cout << "Invalid" << endl;
stringuserinput;
字符串名[10];
int i=0;
int size=10;
名字[0]=“你好”;
名字[1]=“Hii”;
名字[2]=“Hiii”;
请编辑您的问题以提供您可以在纸上(或在您的头脑中)浏览此代码,并查看如果userInput
是first\u name
数组中的第二个条目会发生什么情况。请提供最少的代码来重现您的问题。如何声明size
?请编辑您的问题以提供您可以在纸上浏览此代码(或在你的头脑中)并查看如果userInput
是first\u name
数组中的第二个条目,会发生什么情况。提供最少的代码来重现您的问题。如何声明size
?看起来您的中断在错误的块中。每当您发现字符串时,它应该中断看起来您的中断在错误的块中。每当您找到细绳它看起来有点复杂me@f2355116如果你编辑你的问题并添加最小的代码来重现问题,我可以为数组添加修复。如何声明size
me@f2355116如果您编辑您的问题并添加最小的代码来重现问题,我可以为数组添加修复。Howsize
I声明了什么?我想你是对的,但是请你解释一下这里的问号是什么,它是C/C++三元运算符(参见)我用它来避免一个if
语句。好吧,我知道它是一种写if-then-else
语句的捷径。是的,它在大多数情况下增加了可读性。那么,这就是你想要的吗?我想你是对的,但是你能解释一下这里的问号是什么吗这是C/C++三元运算符吗(请参阅)我用来避免使用if
语句。好吧,我知道这是一种写if-then-else
语句的捷径。是的,在大多数情况下,它增加了可读性。那么,这就是你想要的吗?