c++ contains能否用于数组

   2024-10-06 2110
核心提示:是的,C++中的contains函数可以用于数组。contains函数用于检查一个数组是否包含某个特定的元素。例如:#include iostream#inclu

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

 
举报打赏
 
更多>同类物流大全
推荐图文
推荐物流大全
点击排行

网站首页  |  关于我们  |  联系方式 | 网站留言    |  赣ICP备2021007278号