C++ 检查字符串是否在字符串中(字符串列表)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14515274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if string is in string (list of strings)
提问by Sumyjkl
I'm new here and to C++. I've had some experience in python, and found "if a in b" really easy, and I'm wondering if there's an equivalent in C++.
我是 C++ 新手。我在 python 方面有一些经验,发现“if a in b”真的很容易,我想知道 C++ 中是否有等价物。
Background
背景
I've been trying to make a list of strings and check if an input is in that list. The reason I want to do this is because I want to only use a function if the input will actually do something in that function. (Change int x and y coordinates in this case)
我一直在尝试制作一个字符串列表并检查输入是否在该列表中。我想这样做的原因是因为我只想在输入实际上在该函数中执行某些操作时才使用该函数。(在这种情况下更改 int x 和 y 坐标)
Question
题
string myinput;
string mylist[]={"a", "b", "c"};
cin>>myinput;
//if myinput is included in mylist
//do other stuff here
How do I check using an if
whether the input myinput
is included in string mylist
?
如何使用 a 检查if
输入myinput
是否包含在 string 中mylist
?
回答by Jerry Coffin
You could use std::find
:
你可以使用std::find
:
std::string myinput;
std::vector<std::string> mylist{"a", "b", "c"};
std::cin >> myinput;
if (std::find(std::begin(mylist), std::end(mylist), myinput) != std::end(mylist))
// myinput is included in mylist.
This works fine with only three strings, but if you're going to have many more, you'd probably be better off with an std::set
or std::unordered_set
instead.
这仅适用于三个字符串,但如果您要使用更多字符串,则最好使用std::set
orstd::unordered_set
代替。
std::set<std::string> myset;
// put "a", "b", and "c" into the set here
std::cin >> myinput;
if (myset.find(myinput) != myset.end())
// myinput is included in myset.
回答by chris
Use std::find
:
使用std::find
:
std::size_t listsize = sizeof mylist / sizeof mylist[0];
if (std::find(mylist, mylist + listsize, myinput) != mylist + listsize) {
//found
}
If you know the size of the list beforehand, I suggest std::array
which exposes iterators and a size()
function, as well as a few other benefits over built-in arrays. Note that this is C++11 only (the C++03 near equivalent is std::vector
), and also with C++11 comes std::begin
and std::end
, which reduce it to this:
如果您事先知道列表的大小,我建议std::array
哪个公开迭代器和size()
函数,以及与内置数组相比的其他一些好处。请注意,这仅适用于 C++11(C++03 几乎等效于std::vector
),并且 C++11 还带有std::begin
and std::end
,将其简化为:
if (std::find(std::begin(mylist), std::end(mylist), myinput) != std::end(mylist))
It's fairly easy to make your own for built-in arrays in C++03 as well, but with a standard container that exposes begin()
and end()
members, this shouldn't be too necessary, though it is more versatile.
在 C++03 中为内置数组创建自己的数组也相当容易,但是使用公开begin()
和end()
成员的标准容器,这应该不是太必要,尽管它更通用。
回答by billz
Use std::find, std::find_ifalgorithms
string myinput;
string mylist[]={"a", "b", "c"};
std::string *begin = mylist;
std::string *end = mylist + 3;
if (std::find(begin, end, "b") != end)
{
std::cout << "find" << std::endl;
}
Or use C++11 std::array
with std::begin()
, std::end()
或者将 C++11std::array
与std::begin()
,std::end()
std::array<std::string, 3> mylist = { "a", "b", "c" };
if (std::find(std::begin(mylist), std::end(mylist), "b") != std::end(mylist))
{
cout << "find" << endl;
}
Or Lambda:
或 Lambda:
if (std::find_if(std::begin(mylist), std::end(mylist),
[](const std::string& s){ return s == "b";}) != std::end(mylist))
回答by Hyman
Since you are working with C++ don't hesitate in using the STL library:
由于您正在使用 C++,请不要犹豫使用 STL 库:
string mylist[]={"a", "b", "c"};
vector<string> myvector(mylist, mylist + sizeof(mylist)/sizeof(mylist[0]));
if (find(myvector.begin(), myvector.end(), mystring) != myvector.end()) {
..
}
回答by user1610015
if (find(mylist, &mylist[3], myinput) != &mylist[3])
...
回答by Sajjad Heydari
You can use find as others suggested, or you can use a for loop(easierfor beginners):
您可以按照其他人的建议使用 find ,也可以使用 for 循环(初学者更容易):
for (int i = 0; i < mylist.size() ; i ++){
if (myinput == mylist[i]){
//Do Stuff
}
回答by Hu Xixi
You can also use std::count
你也可以使用 std::count
#include <iostream>
#include <algorithm> // std::count
#include <vector>
//using namespace std;
int main() {
std::vector<std::string> ans = {"a", "b", "c", "a"};
std::cout << count(ans.begin(), ans.end(), "a") << std::endl;
return 0;
}
if the number > 0
, it means the string is in strings.
如果是数字> 0
,则表示字符串在字符串中。
回答by Denis
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string myinput;
string mylist[]={"a", "b", "c"};
if (cin >> myinput &&
std::find(std::begin(mylist), std::end(mylist), myinput) == std::end(mylist))
{
// do other stuff
}
}