partition in STL using vector

Using the partition function in C++ STL with Vector

#include <iostream>
#include <algorithm>
#include <vector>

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

    std::partition(numbers.begin(), numbers.end(), [](int x) { return x % 2 == 0; });

    for (int num : numbers) {
        std::cout << num << " ";
    }

    return 0;
}
  1. Include Necessary Headers:
  2. #include <iostream>: Input/output stream.
  3. #include <algorithm>: Standard template library algorithms.
  4. #include <vector>: Vector container.

  5. Define Main Function:

  6. Begin the main function.

  7. Create Vector:

  8. Define a vector called numbers and initialize it with some integer values.

  9. Use partition Function:

  10. Call the std::partition function with the range of the vector and a lambda function as arguments. The lambda function returns true if the number is even.

  11. Output the Partitioned Vector:

  12. Iterate through the vector and output the elements to the console.

  13. Return from Main:

  14. End the main function and return 0.