迹忆客 专注技术分享

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

在 C++ 中的向量中查找元素索引

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

本文将介绍几种在 C++ 中如何在向量中查找元素索引的方法。

在 C++ 中使用自定义函数查找向量中的元素索引

我们可以使用自定义线性搜索功能在 vector 中找到给定元素的位置。请注意,这是解决此问题的一种非常无效的方法。在下面的示例代码中,我们声明一个整数的向量,并寻找一个任意的元素位置,如果调用成功,则将其输出。

#include <iostream>
#include <string>
#include <vector>

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

int findIndex(const vector<int> &arr, int item) {

    for (auto i = 0; i < arr.size(); ++i) {
        if (arr[i] == item)
            return i;
    }

    return -1;
}

int main(int argc, char *argv[]) {
    vector<int> arr = {1,2,3,4,5,6,7,8,9,10};

    auto pos = findIndex(arr, 32);
    pos != -1 ?
    cout << "Found the element " << 32 << " at position " << pos << endl :
    cout << "Could not found the element " << 32 << " in vector" << endl;

    exit(EXIT_SUCCESS);
}

输出:

Could not found the element 32 in vector

在 C++ 中使用 std::find 算法查找向量中的元素索引

另外,我们可以使用 STL 库中的 std::find 算法。此函数将迭代器返回到满足条件的第一个元素。另一方面,如果未找到任何元素,则算法将返回范围的最后一个元素。请注意,返回的迭代器应按 begin 迭代器递减以计算位置。

#include <iostream>
#include <string>
#include <vector>

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

int findIndex2(const vector<int> &arr, int item) {
    auto ret = std::find(arr.begin(), arr.end(), item);

    if (ret != arr.end())
        return ret - arr.begin();
    return -1;
}

int main(int argc, char *argv[]) {
    vector<int> arr = {1,2,3,4,5,6,7,8,9,10};

    auto pos2 = findIndex2(arr, 10);
    pos2 != -1 ?
    cout << "Found the element " << 10 << " at position " << pos2 << endl :
    cout << "Could not found the element " << 10 << " in vector" << endl;

    exit(EXIT_SUCCESS);
}

输出:

Found the element 10 at position 9

在 C++ 中使用 std::find_if 算法查找向量中的元素索引

查找元素索引的另一种方法是调用 std::find_if 算法。它与 std::find 相似,除了第三个参数可以是用于评估每个迭代元素的谓词表达式。如果表达式返回 true,则算法将返回。注意,我们将 lambda 表达式作为第三个参数传递,该表达式检查元素是否等于 10

#include <iostream>
#include <string>
#include <vector>

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

int main(int argc, char *argv[]) {
    vector<int> arr = {1,2,3,4,5,6,7,8,9,10};

    auto ret = std::find_if(arr.begin(), arr.end(),
                            [](int x) { return x == 10; });

    if (ret != arr.end())
        cout << "Found the element " << 10 << " at position "
             << ret - arr.begin() << endl;

    exit(EXIT_SUCCESS);
}

输出:

Found the element 10 at position 9

转载请发邮件至 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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便