Here is an example of a C++ program that uses multiple STL algorithms to perform 
various operations on a vector:

Copy code
#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
    std::vector<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    std::vector<int> evenNumbers;

    // Using the copy_if algorithm to copy even numbers to a new vector
    std::copy_if(numbers.begin(), numbers.end(), std::back_inserter(evenNumbers), [](int x){ return x % 2 == 0; });

    // Using the for_each algorithm to print the even numbers
    std::cout << "Even numbers: ";
    std::for_each(evenNumbers.begin(), evenNumbers.end(), [](int x){ std::cout << x << " "; });
    std::cout << std::endl;

    // Using the sort algorithm to sort the numbers in ascending order
    std::sort(numbers.begin(), numbers.end());

    // Using the find algorithm to search for a specific value
    int valueToFind = 5;
    auto result = std::find(numbers.begin(), numbers.end(), valueToFind);
    if (result != numbers.end()) {
        std::cout << "The value " << valueToFind << " was found in the vector at index " << std::distance(numbers.begin(), result) << std::endl;
    } else {
        std::cout << "The value " << valueToFind << " was not found in the vector." << std::endl;
    }

    // Using the min_element and max_element algorithms to find the smallest and largest value in the vector
    auto minValue = *std::min_element(numbers.begin(), numbers.end());
    auto maxValue = *std::max_element(numbers.begin(), numbers.end());
    std::cout << "The smallest value in the vector is " << minValue << std::endl;
    std::cout << "The largest value in the vector is " << maxValue << std::endl;

    return 0;
}
In this program, we first define a vector of integers and then use the copy_if 
algorithm to copy only even numbers to a new vector. Then, we use the for_each 
algorithm to print the even numbers.

We then use the sort algorithm to sort the numbers in ascending order. Next, we 
use the find algorithm to search for a specific value in the vector. If the value
is found, the index of the value is printed, otherwise, a message saying that the 
value was not found is printed.

Finally, we use the min_element and max_element algorithms to find the smallest 
and largest value in the vector, respectively. The results are then printed to the
console.

Overall, this program demonstrates how the STL algorithms can be used to perform a
variety of operations on a container with minimal code, making your code more
readable and efficient.



Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: