Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it good practice to fake an inserter?

Tags:

c++

algorithm

stl

We're taught to create function objects to use algorithms.

There are algorithms that call the operator(), like:

  • for_each
  • find_if
  • remove_if
  • max_element
  • count_if

These function objects should typically inherit from unary_function or binary_function, to behave like a function, a predicate, etc.

But books don't generally demonstrate examples for creating OutputIterators:

e.g. to traverse the output of functions like std::set_intersection(), I have to provide a destination container, and then traverse the result:

std::vector<int> tmp_dest;

std::set_difference (
        src1.begin(), src1.end(), 
        src2.begin(), src2.end(), 
        std::back_inserter(tmp_dest));

std::for_each( tmp_dest.begin(), tmp_dest.end(), do_something );
int res = std::accumulate( tmp_dest.begin(), tmp_dest.end(), 0 );

but think that it would be more efficient sometimes to use the values of each algorithm, without storing them first, like:

std::set_difference (
        src1.begin(), src1.end(), 
        src2.begin(), src2.end(), 
        do_something );

Accumulator accumulate(0);  // inherits from std::insert_iterator ?
std::set_difference (
        src1.begin(), src1.end(), 
        src2.begin(), src2.end(), 
        accumulate );
  • Should we generally create classes like this Accumulator ?
  • What should its design look like?
  • What should it inherit from ? Accumulator could inherit from insert_iterator, but it is not really an iterator (eg it does not implement operator++() )

what are the widely accepted practices?

like image 461
Grim Fandango Avatar asked Sep 19 '13 14:09

Grim Fandango


People also ask

Should you mock the database?

Mocking and stubbing are the cornerstones of having quick and simple unit tests. Mocks are useful if you have a dependency on an external system, file reading takes too long, the database connection is unreliable, or if you don't want to send an email after every test.

Can you mock a database?

Database Mocking is a technique that allows you to set the desired database state (for different tables) in your tests to let specific data sets ready for future test execution.

How can I insert more than 1000 rows in SQL Server?

A table can store upto 1000 rows in one insert statement. If a user want to insert multiple rows at a time, the following syntax has to written. If a user wants to insert more than 1000 rows, multiple insert statements, bulk insert or derived table must be used.

Should I unit test database calls?

Unit tests are incredibly important to us as developers because they allow us to demonstrate the correctness of the code we've written. More importantly, unit tests allow us to make updates to our code base with confidence that we haven't broken anything.


1 Answers

If you want an output iterator that calls your own function for every value received, use Boost.Iterator's function_output_iterator.

like image 73
Sebastian Redl Avatar answered Nov 07 '22 16:11

Sebastian Redl