Skip to main content

Posts

Showing posts with the label locate the position of a specified value

Write a program in C++ using the find() algorithm to locate the position of a specified value in a sequence container.

  here's an example program using the find() algorithm to locate the position of a specified value in a vector: #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() {     // Create a vector of integers     vector<int> vec = { 5, 2, 7, 9, 1, 8 };     // Find the position of the value 7 in the vector     auto it = find(vec.begin(), vec.end(), 7);     // Check if the value was found and print the position     if (it != vec.end()) {         cout << "The position of the value 7 is: " << distance(vec.begin(), it) << endl;     } else {         cout << "The value 7 was not found in the vector." << endl;     }     return 0; } Output: The position of the value 7 is: 2 In this program, we first create a vector of integers with some random values. We then use the find() algor...