example2_finding_elements.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include <algorithm>
#include <vector>
#include <iostream>

int main() {
    std::vector<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    
    auto it = std::find(numbers.begin(), numbers.end(), 7);
    
    if (it != numbers.end()) {
        std::cout << "Found 7 at position: " << std::distance(numbers.begin(), it) << std::endl;
    } else {
        std::cout << "7 not found in the vector" << std::endl;
    }
    
    return 0;
}
Back to algorithm