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,846 questions

51,767 answers

573 users

How to find out if an item is present in a std::vector with C++

1 Answer

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

int main() {
    std::vector<int> vec = {1, 3, 21, 9, 88, 6, 7, 2, 45}; 
    int item = 88;

    // Print result using std::boolalpha for better readability
    std::cout << std::boolalpha << (std::find(vec.begin(), vec.end(), item) != vec.end()) << "\n";

    // Print yes/no message
    std::cout << (std::find(vec.begin(), vec.end(), item) != vec.end() ? "yes\n" : "no\n");

    // Conditional output
    if (std::find(vec.begin(), vec.end(), item) != vec.end()) {
        std::cout << "Found\n";
    } else {
        std::cout << "Not found\n";
    }
}

 
 
/*
run:
 
true
yes
Found
 
*/
   
 

 



answered May 11, 2025 by avibootz
...