温馨提示×

c++ contains能否用于数组

c++
小樊
100
2024-07-16 00:10:50
栏目: 编程语言

是的,C++中的contains函数可以用于数组。contains函数用于检查一个数组是否包含某个特定的元素。例如:

#include <iostream>
#include <algorithm>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int target = 3;

    if (std::find(std::begin(arr), std::end(arr), target) != std::end(arr)) {
        std::cout << "Array contains the element " << target << std::endl;
    } else {
        std::cout << "Array does not contain the element " << target << std::endl;
    }

    return 0;
}

上述代码中,我们使用std::find算法来检查数组arr是否包含元素target。如果包含,则输出Array contains the element 3,否则输出Array does not contain the element 3

0