迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > C++ >

如何检查 C++ 向量中元素是否存在

作者:迹忆客 最近更新:2023/03/24 浏览次数:

本文演示了多种方法来检查一个 C++ 向量中是否存在元素。


C++ std::find() 检查向量中是否存在元素

find 方法是 STL 算法库的一部分,它可以检查给定的元素是否存在于特定的范围内。该函数搜索一个等于用户传递的第三个参数的因子。相应的返回值是遍历到找到的第一个元素,如果没有找到,则返回范围的 end

注意,我们使用*运算符来访问返回的字符串值,并在 if 语句中做一个比较条件,如下例所示。

#include <iostream>
#include <vector>
#include <algorithm>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector; using std::find;

int main() {
    string element_to_check1 = "nibble";
    string element_to_check2 = "nimble";

    vector<string> data_types = {"bit", "nibble",
                                 "byte", "char",
                                 "int", "long",
                                 "long long", "float",
                                 "double", "long double"};

    if (*find(data_types.begin(), data_types.end(), element_to_check1) == element_to_check1) {
        printf("%s is present in the vector\n", element_to_check1.c_str());
    } else {
        printf("%s is not present in the vector\n", element_to_check1.c_str());
    }

    if (*find(data_types.begin(), data_types.end(), element_to_check2) == element_to_check2) {
        printf("%s is present in the vector\n", element_to_check2.c_str());
    } else {
        printf("%s is not present in the vector\n", element_to_check2.c_str());
    }

    return EXIT_SUCCESS;
}

输出:

nibble is present in the vector
nimble is not present in the vector

C++ 基于范围的 for 循环来检查元素是否存在于向量中

基于范围的 for 循环可以作为另一种解决方案来检查给定元素是否存在于向量中。这种方法比较直接,因为它遍历向量;每次遍历都检查是否与给定的字符串相等。如果一个元素匹配,就会打印一个确认字符串,然后使用 break 语句停止循环。

#include <iostream>
#include <vector>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector;

int main() {
    string element_to_check = "nibble";

    vector<string> data_types = {"bit", "nibble",
                                 "byte", "char",
                                 "int", "long",
                                 "long long", "float",
                                 "double", "long double"};

    for (const auto &item : data_types) {
        if (item == element_to_check) {
            printf("%s is present in the vector\n", element_to_check.c_str());
            break;
        }
    }
    return EXIT_SUCCESS;
}

输出:

nibble is present in the vector

C++ any_of() 检查向量中是否存在元素

<algorithm> 头文件的另一个有用的方法是 any_of 算法,它与 find 方法类似。any_of 方法检查作为第三个参数指定的单元谓词是否对给定范围内的至少一个元素返回 true。在这个例子中,我们使用一个 lambda 表达式来构建一个用于比较向量元素的单元谓词。

#include <iostream>
#include <vector>
#include <algorithm>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector; using std::any_of;

int main() {
    string element_to_check = "nibble";

    vector<string> data_types = {"bit", "nibble",
                                 "byte", "char",
                                 "int", "long",
                                 "long long", "float",
                                 "double", "long double"};

    if (any_of(data_types.begin(), data_types.end(), [&](const string& elem) { return elem == element_to_check; })) {
        printf("%s is present in the vector\n", element_to_check.c_str());
    }

    return EXIT_SUCCESS;
}

输出:

nibble is present in the vector

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Arduino 中停止循环

发布时间:2024/03/13 浏览次数:444 分类:C++

可以使用 exit(0),无限循环和 Sleep_n0m1 库在 Arduino 中停止循环。

Arduino 复位

发布时间:2024/03/13 浏览次数:315 分类:C++

可以通过使用复位按钮,Softwarereset 库和 Adafruit SleepyDog 库来复位 Arduino。

Arduino 的字符转换为整型

发布时间:2024/03/13 浏览次数:181 分类:C++

可以使用简单的方法 toInt()函数和 Serial.parseInt()函数将 char 转换为 int。

Arduino 串口打印多个变量

发布时间:2024/03/13 浏览次数:381 分类:C++

可以使用 Serial.print()和 Serial.println()函数在串口监视器上显示变量值。

Arduino if 语句

发布时间:2024/03/13 浏览次数:123 分类:C++

可以使用 if 语句检查 Arduino 中的不同条件。

Arduino ICSP

发布时间:2024/03/13 浏览次数:214 分类:C++

ICSP 引脚用于两个 Arduino 之间的通信以及对 Arduino 引导加载程序进行编程。

使用 C++ 编程 Arduino

发布时间:2024/03/13 浏览次数:127 分类:C++

本教程将讨论使用 Arduino IDE 在 C++ 中对 Arduino 进行编程。

Arduino 中的子程序

发布时间:2024/03/13 浏览次数:168 分类:C++

可以通过在 Arduino 中声明函数来处理子程序。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便