<分区>
假设我在 C++ 中有一个 vector ,如 v = ('a','e','i','o','u')
。我想通过简单地检查字符是否在 vector v 中来检查字符串是否为元音。正如我自己所知,我不需要任何代码,我正在寻找 c++ 中的函数或关键字,它等效于 python 中的以下内容:
list = ['a', 'e', 'i', 'o', 'u']
if str in list:
#do stuff
PS:如果不存在与此等效的内容,也请告诉我。
<分区>
假设我在 C++ 中有一个 vector ,如 v = ('a','e','i','o','u')
。我想通过简单地检查字符是否在 vector v 中来检查字符串是否为元音。正如我自己所知,我不需要任何代码,我正在寻找 c++ 中的函数或关键字,它等效于 python 中的以下内容:
list = ['a', 'e', 'i', 'o', 'u']
if str in list:
#do stuff
PS:如果不存在与此等效的内容,也请告诉我。
最佳答案
有用的链接:
例子是
#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
int main()
{
int n1 = 3;
int n2 = 5;
std::vector<int> v{0, 1, 2, 3, 4};
auto result1 = std::find(std::begin(v), std::end(v), n1);
auto result2 = std::find(std::begin(v), std::end(v), n2);
if (result1 != std::end(v)) {
std::cout << "v contains: " << n1 << '\n';
} else {
std::cout << "v does not contain: " << n1 << '\n';
}
if (result2 != std::end(v)) {
std::cout << "v contains: " << n2 << '\n';
} else {
std::cout << "v does not contain: " << n2 << '\n';
}
}
关于c++ - "in"在 C++ 中等效列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36886732/