Click here to Skip to main content
Click here to Skip to main content

cpplinq: set operators

By , 23 Nov 2012
 

In the previous posts I introduced cpplinq, a C++ template library that provides .NET-like query operators for sequences of objects in C++11. In this third installment I will discuss the set operators the library provides.

There are four set operators: distinct, union_with (called so because union is a keyword in C++), intersect_with (suffix _with is for consistency with the union operator) and except. These operators should be pretty straight forward: distinct eliminates the duplicate elements from a sequence, yielding a new sequence with only the distinct elements (in the order their original order), union_with produces the set union of two sequences, intersect_with produces the set intersection of two sequences and except produces the set difference of two sequences.

Before seeing some examples, it worth nothing that all these operators delay the traversal of the sequences until the resulting object is enumerated.

Let’s see some examples:

int numbers[] = {5,4,3,2,1,2,3,4,5};
auto result = from_array(numbers) >> distinct() >> to_vector(); // yields {5,4,3,2,1}
auto result = empty<int>() >> union_with(range(1,5)) >> to_vector(); // yields {1,2,3,4,5}

int set1[] = {5,4,3,2,1,2,3,4,5};
int set2[] = {4,5,6,7};
auto set3 = from_array(set1) >> union_with(from_array(set2)) >> to_vector(); // yields {5,4,3,2,1,6,7}
auto set4 = from_array(set2) >> union_with(from_array(set1)) >> to_vector(); // yields {4,5,6,7,3,2,1}
auto result1 = empty<int>() >> intersect_with( range(0, 10) ) >> to_vector(); // yields {}
auto result2 = range(0, 10) >> intersect_with( empty<int>() ) >> to_vector(); // yields {}

int set1[] = {5,4,3,2,1,2,3,4,5};
int set2[] = {4,5,6};
auto set3 = from_array(set1) >> intersect_with(from_array(set2)) >> to_vector(); // yields {5,4}
auto set3 = from_array(set2) >> intersect_with(from_array(set1)) >> to_vector(); // yields {4,5}
auto result1 = empty<int>() >> except( range(0, 10) ) >> to_vector(); // yields {}
auto result2 = range(0, 10) >> except( empty<int>() ) >> to_vector(); // yields {0,1,2,3,4,5,6,7,8,9}

int set1[] = {5,4,3,2,1,2,3,4,5};
int set2[] = {4,5,6,7};
auto set3 = from_array(set1) >> except(from_array(set2)) >> to_vector(); // yields {3,2,1}
auto set4 = from_array(set2) >> except(from_array(set1)) >> to_vector(); // yields {6,7}

You can learn more about these operators (and the others that are implemented) by reading the cpplinq query operators documentation.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Marius Bancila
Software Developer (Senior) Visma Software
Romania Romania
Member
Marius Bancila is a Microsoft MVP for VC++. He works as a software developer for Visma, a Norwegian-based company. He is mainly focused on building desktop applications with VC++ and VC#. He keeps a blog at http://www.mariusbancila.ro/blog, focused on Windows programming. He is the co-founder of codexpert.ro, a community for Romanian C++ programmers.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 23 Nov 2012
Article Copyright 2012 by Marius Bancila
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid