The algorithm copy_if is useful, it can help us to get all elements that meet special conditions.
C++
template< class InputIt, class OutputIt, class UnaryPredicate >
OutputIt copy_if( InputIt first, InputIt last,
                  OutputIt d_first,
                  UnaryPredicate pred );
Possible implement:
C++
template<class InputIt, class OutputIt, class UnaryPredicate>
OutputIt copy_if(InputIt first, InputIt last, 
                 OutputIt d_first, UnaryPredicate pred)
{
    while (first != last) {
        if (pred(*first))
            *d_first++ = *first;
        first++;
    }
    return d_first;
}
Usage example:
C++
#include 
#include 
#include 
#include 
using namespace std;

int main(int argc, char *argv[])
{
    vector<int> from_vector(10);
    iota(from_vector.begin(), from_vector.end(), 1);

    vector<int> to_vector;
    copy_if(from_vector.begin(), from_vector.end(),
            std::back_inserter(to_vector), [](int e1){ return (e1 & 1); });
    
    for( auto it: to_vector )
    {
        cout << it << "t";
    }
    cout << endl;
    return 0;
}
output:
Bash
1   3   5   7   9

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments

Tex To PDF
: convert the Latex file which suffix is tex to a PDF file

X
0
Would love your thoughts, please comment.x
()
x