Find Element in Vector
C++
Easy
5 views
Problem Description
Search for element in vector using find().
Real Life: Searching for item in list.
Step-by-Step Logic:
1. Use find() function
2. It returns iterator
3. If found, iterator points to element
4. If not found, returns end()
Official Solution
void stl_q5_find_element() {
vector<int> numbers = {10, 20, 30, 40, 50};
int searchValue = 30;
auto it = find(numbers.begin(), numbers.end(), searchValue);
if(it != numbers.end()) {
int position = it - numbers.begin();
cout << "Element " << searchValue << " found at position " << position << endl;
} else {
cout << "Element not found" << endl;
}
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!