Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,894 questions

51,825 answers

573 users

How to check if a vector contains a value in C++

3 Answers

0 votes
#include <vector>
#include <iostream>
#include <algorithm>

int main()
{
    std::vector<int> v = { 5, 7, 1, 0, 9, 3, 8 };
    int val = 3;
 
    if (std::count(v.begin(), v.end(), val)) {
        std::cout << "found";
    }
    else {
        std::cout << "Not found";
    }
}




/*
run:

found

*/

 



answered Feb 25, 2022 by avibootz
edited Jan 18, 2023 by avibootz
0 votes
#include <vector>
#include <iostream>
#include <algorithm>

int main()
{
    std::vector<int> v = { 5, 7, 1, 0, 9, 3, 8 };
    int val = 3;
 
    if (std::find(v.begin(), v.end(), val) != v.end()) {
        std::cout << "found";
    }
    else {
        std::cout << "Not found";
    }
}




/*
run:

found

*/

 



answered Feb 25, 2022 by avibootz
edited Jan 18, 2023 by avibootz
0 votes
#include <vector>
#include <algorithm>
#include <iostream>

bool Contains(const std::vector<int> &v, int x) {
    return std::find(v.begin(), v.end(), x) != v.end();
}

int main() {
    std::vector<int> v = {5, 7, 1, 0, 9, 3, 8};
    int val = 9;

    std::cout << (Contains(v, val) ? "yes": "no");
}




/*
run:

yes

*/


 



answered Jan 18, 2023 by avibootz

Related questions

1 answer 162 views
1 answer 111 views
1 answer 130 views
1 answer 143 views
1 answer 147 views
...