How to find a specific element in a vector?

To search for a specific element in a vector, you can use the std::find function. Here is an example code:

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

int main() {
    std::vector<int> vec = {1, 2, 3, 4, 5};

    // 查找元素3
    int target = 3;
    auto it = std::find(vec.begin(), vec.end(), target);

    if (it != vec.end()) {
        std::cout << "找到了元素" << target << std::endl;
    } else {
        std::cout << "未找到元素" << target << std::endl;
    }

    return 0;
}

In the above code, the std::find function is used to search for element 3 in a vector. If the element is found, “Found element 3” is printed; if it is not found, “Element 3 not found” is printed.

bannerAds