Diving Into Filtering Data Streams in C++

Welcome to our hands-on tutorial on data filtering in C++. In this session, we spotlight data filtering, a simple yet powerful aspect of programming and data manipulation. By learning to filter data, we can extract only the pieces of data that meet specific standards, decluttering the mess of unwanted data.

Grasping the Concept of Filtering

In the real world, data filtering mirrors the process of sieving. Let's visualize this. Imagine you're shopping online for a shirt. You have the ability to filter clothes based on color, size, brand, etc. Translating this to programming, our clothing items are our data, and our sieve is a selection of Boolean logic and algorithms used for filtering.

Discovering Data Filtering using Loops

In programming, loops enable coders to execute a block of code repetitively, making them handy tools in data filtering. C++ uses for and while loops that iterate through data structures like arrays or vectors, checking each data element against specific criteria.

For instance, let's build a class, DataFilter, that filters out numbers less than ten in a vector:

#include <vector>
#include <iostream>

class DataFilter {
public:
    std::vector<int> filterWithLoops(const std::vector<int> &dataStream) {
        std::vector<int> filteredData;
        for (int item : dataStream) {
            if (item < 10) {
                filteredData.push_back(item);
            }
        }
        return filteredData;
    }
};

int main() {
    std::vector<int> dataStream = {23, 5, 7, 12, 19, 2};
    DataFilter df;

    std::vector<int> filteredData = df.filterWithLoops(dataStream);
    std::cout << "Filtered data by loops:";
    for (int item : filteredData) {
        std::cout << " " << item;
    }
    std::cout << std::endl;
    // Output: Filtered data by loops: 5 7 2
    
    return 0;
}

Notice the for loop combined with a conditional if statement to filter out numbers less than ten and push them into filteredData.

Decoding Data Filtering with the `std::copy_if` Function

C++ incorporates the Standard Template Library (STL), which provides algorithms specifically designed for various operations. One such algorithm is std::copy_if, which helps in copying elements based on a condition.

Let’s use std::copy_if to refactor our filter function:

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

class DataFilter {
public:
    std::vector<int> filterWithCopyIf(const std::vector<int> &dataStream) {
        std::vector<int> filteredData;
        std::copy_if(dataStream.begin(), dataStream.end(), std::back_inserter(filteredData), [](int item) {
            return item < 10;
        });
        return filteredData;
    }
};

int main() {
    std::vector<int> dataStream = {23, 5, 7, 12, 19, 2};
    DataFilter df;

    std::vector<int> filteredData = df.filterWithCopyIf(dataStream);
    std::cout << "Filtered data by std::copy_if:";
    for (int item : filteredData) {
        std::cout << " " << item;
    }
    std::cout << std::endl;
    // Output: Filtered data by std::copy_if: 5 7 2
    return 0;
}

In the above example, std::copy_if is used to filter data in a more succinct and effective manner, with a lambda function checking if an item is less than ten.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal