How to Filter Items of a Vector based on a Condition in C++


How to Filter Items of a Vector based on a Condition in C++ ?

Answer

To filter items of a vector based on a condition in C++, you can use the `std::remove_if` algorithm provided by the algorithm header in combination with the erase-remove idiom.



✐ Examples

1 Filter Items Greater Than a Threshold

In this example,

  1. First, you define a vector containing the elements you want to filter.
  2. Next, you specify the condition for filtering, such as greater than a threshold, using a lambda function or a functor.
  3. Then, you use the `std::remove_if` algorithm with the specified condition to move the elements that satisfy the condition to the end of the vector.
  4. Finally, you erase the elements from the end of the vector to remove them.

C++ Program

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

int main() {
    // Define a vector containing elements
    std::vector<int> vec = {1, 5, 10, 15, 20};

    // Specify the threshold for filtering
    int threshold = 10;

    // Define a lambda function to filter elements greater than the threshold
    auto condition = [threshold](int x) { return x > threshold; };

    // Use std::remove_if algorithm with the condition
    vec.erase(std::remove_if(vec.begin(), vec.end(), condition), vec.end());

    // Print the filtered vector
    for (int i : vec) {
        std::cout << i << " ";
    }
    std::cout << std::endl;
    return 0;
}

Output

15 20

2 Filter Items Meeting a Condition

In this example,

  1. Define a vector containing elements.
  2. Specify the condition for filtering using a lambda function or a functor.
  3. Use the `std::remove_if` algorithm with the specified condition to move the elements meeting the condition to the end of the vector.
  4. Erase the elements from the end of the vector to remove them.

C++ Program

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

int main() {
    // Define a vector containing elements
    std::vector<int> vec = {1, 5, 10, 15, 20};

    // Specify the condition for filtering
    auto condition = [](int x) { return x % 2 == 0; }; // Select even numbers

    // Use std::remove_if algorithm with the condition
    vec.erase(std::remove_if(vec.begin(), vec.end(), condition), vec.end());

    // Print the filtered vector
    for (int i : vec) {
        std::cout << i << " ";
    }
    std::cout << std::endl;
    return 0;
}

Output

1 5 15

Summary

In this tutorial, we learned How to Filter Items of a Vector based on a Condition in C++ language with well detailed examples.




More C++ Vectors Tutorials

  1. How to create an Empty Vector in C++ ?
  2. How to Initialize a Vector in C++ ?
  3. How to Get Length of a Vector in C++ ?
  4. How to create a Vector of Size N in C++ ?
  5. How to create a Vector of Numbers from 1 to N in C++ ?
  6. How to create a Vector of Integers in C++ ?
  7. How to create a Vector of Strings in C++ ?
  8. How to create a Vector of Empty Vectors in C++ ?
  9. How to Access Items in a Vector in C++ ?
  10. How to get Item in a Vector at a Specific Index in C++ ?
  11. How to get First Item in a Vector in C++ ?
  12. How to get Last Item in a Vector in C++ ?
  13. How to Iterate Over a Vector in C++ ?
  14. How to Iterate Over a Vector with Index in C++ ?
  15. How to Iterate Over a Vector in Reverse Order in C++ ?
  16. How to check if a Vector is Empty in C++ ?
  17. How to check if a Vector is Not Empty in C++ ?
  18. How to get Sub Vector in C++ ?
  19. How to get the Index of Specified Item in a Vector in C++ ?
  20. How to check if a Specific Item is present in the Vector in C++ ?
  21. How to check if a Vector contains all the items of Another Vector in C++ ?
  22. How to count the Number of Occurrences of Specific Item in the Vector in C++ ?
  23. How to find the Item with Maximum Number of Occurrences in a Vector in C++ ?
  24. How to find the Item with Minimum Number of Occurrences in a Vector in C++ ?
  25. How to Sort a Vector in C++ ?
  26. How to Sort a Vector in Ascending Order in C++ ?
  27. How to Sort a Vector in Descending Order in C++ ?
  28. How to create a Two Dimensional Vector in C++ ?
  29. How to Iterate over a Two Dimensional Vector in C++ ?
  30. How to create a Three Dimensional Vector in C++ ?
  31. How to Copy a Vector in C++ ?
  32. How to Split a Vector in C++ ?
  33. How to Join Vectors in C++ ?
  34. How to Append an Item to a Vector in C++ ?
  35. How to Insert an Item at Specific Index in a Vector in C++ ?
  36. How to Append a Vector to another Vector in C++ ?
  37. How to Concatenate Two Vectors in C++ ?
  38. How to check if Two Vectors are Equal in C++ ?
  39. How to check if Two Vectors have Same Items (Regardless of Order) in C++ ?
  40. How to Convert a Vector of Integers to a Vector of Strings in C++ ?
  41. How to Convert a Vector of Strings to a Vector of Integers in C++ ?
  42. How to Convert a Vector of Floats to a Vector of Strings in C++ ?
  43. How to Convert a Vector of Strings to a Vector of Floats in C++ ?
  44. How to Reverse a Vector in C++ ?
  45. How to Shuffle a Vector in C++ ?
  46. How to Slice a Vector in C++ ?
  47. How to get First N Items from a Vector in C++ ?
  48. How to get Last N Items from a Vector in C++ ?
  49. How to Rotate Items in a Vector in C++ ?
  50. How to Filter Items of a Vector based on a Condition in C++ ?
  51. How to Remove Duplicates in a Vector in C++ ?
  52. How to Remove Item at a Specific Index from a Vector in C++ ?
  53. How to Remove Specific Item from a Vector in C++ ?
  54. How to Remove Items from Vector based on a Condition in C++ ?
  55. How to Sort a String Vector in Dictionary Order in C++ ?
  56. How to Concatenate Strings in Vector in C++ ?
  57. How to create a Vector of Vectors in C++ ?