Click here to Skip to main content
12,631,574 members (30,282 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

4.2K views
1 bookmarked
Posted

cpplinq: set operators

, 23 Nov 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

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)

Share

About the Author

Marius Bancila
Architect Visma Software
Romania Romania
Marius Bancila is a Microsoft MVP for VC++. He works as a system architect 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.

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161205.3 | Last Updated 23 Nov 2012
Article Copyright 2012 by Marius Bancila
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid