c++ - "in"在 C++ 中等效列表

标签 c++ algorithm vector

<分区>

假设我在 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:如果不存在与此等效的内容,也请告诉我。

最佳答案

有用的链接:

find Algorithm

例子是

#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/

相关文章:

c++ - 如何保留多维Vector?

c++ - 如何在 C 中公开 C++ 函数指针?

c++ - 为什么我的编辑控件 "add variable"弹出框没有 CString 作为选项

algorithm - 如何找到连接的子图?

java - 如何创建简单的扑克手牌算法?

C++将局部变量存储到 vector 中可防止它在超出范围时被破坏

c++ - 获取除第一个元素以外的所有元素作为单独的 vector

c++ - 如何从 lua 脚本访问 C++ 类成员函数?

c++ - BOOST_LOG_TRIVIAL 与 logrotate(重新打开日志)

算法:使用关联键重叠 1D 段