温馨提示×

c++ contains能否用于数组

c++
小樊
147
2024-07-16 00:10:50
栏目: 编程语言
C++开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

是的,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

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:c++ contains能否用于unordered_map

0