Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL-Like Selects in Imperative Languages
    primarykey
    data
    text
    <p>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:</p> <pre><code>using std::vector; vector&lt; int &gt; numbers; for ( int i = -10; i &lt;= 10; ++i ) { numbers.push_back( i ); } vector&lt; int &gt; positive_numbers; for ( vector&lt; int &gt;::const_iterator it = numbers.begin(), end = numbers.end(); it != end; ++it ) { if ( number &gt; 0 ) { positive_numbers.push_back( *it ); } } </code></pre> <p>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&lt; int > :</p> <pre><code>SELECT * INTO positive_numbers FROM numbers WHERE num &gt; 0 </code></pre> <p>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. </p> <p>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!</p> <p>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++?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload