Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL-Like Selects in Imperative Languages

Tags:

c++

sql

I'm doing some coding at work in C++, and a lot of the things that I work on involve analyzing sets of data. Very often I need to select some elements from a STL container, and very frequently I wrote code like this:

using std::vector;
vector< int > numbers;
for ( int i = -10; i <= 10; ++i ) {
    numbers.push_back( i );
}

vector< int > positive_numbers;
for ( vector< int >::const_iterator it = numbers.begin(), end = numbers.end();
        it != end; ++it 
) {
    if ( number > 0 ) {
        positive_numbers.push_back( *it );
    }
}

Over time this for loop and the logic contained within it gets a lot more complicated and unreadable. Code like this is less satisfying than the analogous SELECT statement in SQL, assuming that I have a table called numbers with a column named "num" rather than a std::vector< int > :

SELECT * INTO positive_numbers FROM numbers WHERE num > 0

That's a lot more readable to me, and also scales better, over time a lot of the if-statement logic that's in our codebase has become complicated, order-dependent and unmaintainable. If we could do SQL-like statements in C++ without having to go to a database I think that the state of the code might be better.

Is there a simpler way that I can implement something like a SELECT statement in C++ where I can create a new container of objects by only describing the characteristics of the objects that I want? I'm still relatively new to C++, so I'm hoping that there's something magic with either template metaprogramming or clever iterators that would solve this. Thanks!

Edit based on first two answers. Thanks, I had no idea that's what LINQ actually was. I program on Linux and OSX systems primarily, and am interested in something cross-platform across OSX, Linux and Windows. So a more educated version of this question would be - is there a cross-platform implementation of something like LINQ for C++?

like image 666
James Thompson Avatar asked Jan 20 '26 04:01

James Thompson


2 Answers

You've almost exactly described LINQ. It's a .NET 3.5 feature so you should be able to use it from C++.

like image 103
Matt Bridges Avatar answered Jan 22 '26 18:01

Matt Bridges


The functionality you're describing is commonly found in functional languages that support concepts such as closures, predicates, functors, etc.

The problem with the code above is that it combines:

  1. Logic for iterating over collection (the for loop)
  2. Condition that must be satisfied for an element to be copied to another collection
  3. Logic for copying an element from one collection to another

In reality (1) and (3) are boilerplate, insofar as every time you need to iterate over a collection copying some elements to another collection, it's probably only the conditional code that will change each time. Languages with support for functional programming eliminate this boilerplate. For example, in Groovy you can replace your for loop above with just

def positive_numbers = numbers.findAll{it > 0}

Even though C++ is not a functional language there may be libraries which provide support for functional-style programming with STL collections. For example, the Apache commons collection (and also possibly Google's collection library) provides support for functional style programming with Java collections, even though Java itself is not a functional language.

like image 32
Dónal Avatar answered Jan 22 '26 18:01

Dónal