Original Post
What makes your day-to-day programming a little easier? What classes can you just not live without? Here's a simple function I've found myself testing with often enough to hold on to it. I serve it a standard container and my choice of descriptive text, delimiter or output stream:
// : --- it's definition ------------------------------------------------------
#include <iostream>
#include <string>
// outputs to an ostream a standard container with string prefix and delimiter
template< typename container >
void output_container( const container & c,
const std::string s = "", const std::string d = " ",
std::ostream & os = std::cout
)
{
os << s;
std::copy( c.begin(), c.end(), std::ostream_iterator< container::value_type >( os, d.data() ) );
os << std::endl;
return;
}
// : --- and it's usage -------------------------------------------------------
#include <vector>
#include <list>
#include <fstream>
int main() {
// create some containers and open a file for testing
std::vector< int > v( 5, 420 );
std::list< float > l( 5, 69.5f );
std::ofstream file( "test.txt" );
output_container( v, "vector = ", ":" ); // outputs "vector = 420:420:420:420:420:" to std::cout
output_container( l, "", ", ", file ); // outputs "69.5, 69.5, 69.5, 69.5, 69.5," to file "test.txt"
file.close();
return 0;
}