C++ iomanip expression -


variables:

static const float    s_period[]    = { 100, 50, 25, 12, 5, 7, 3, 2, 1 }; static const unsigned s_timerscount = sizeof( s_period ) / sizeof( s_period[0] ); float  min = 10000000; float  max = 0; double sum = 0.0; 

c++ version:

for( unsigned = 0; < s_timerscount; ++i ) {    ...    std::cout       << "id: "         << std::setw(2) << (i+1)       << ", expected: " << std::setw(3) << s_period[i]       << ", min: "      << std::setw(3) << min       << ", max: "      << std::setw(3) << max       << ", avg: "      << std::fixed << std::setw(10) << std::setprecision(6) << avg       << std::endl;    std::cout.unsetf( std::ios_base::floatfield ); } 

c version:

for( unsigned = 0; < s_timerscount; ++i ) {    ...    printf( "id: %2d, expected: %3.0f, min: %3.0f, max: %3.0f, avg: %10.6f\n",       ( + 1 ), s_period[i], min, max, avg ); } 

the loop important in example because have reset ios_base::floatfield next loop.

the c++ version more verbose c equivalent, can propose more compact c++ version?

i don't consider verbosity of c++ approach problematic; in fact, seems easier read , understand c version.

that said, can achieve printf-style formatting using c++ iostreams via boost.format:

#include <boost/format.hpp> #include <iostream>  using boost::format; using boost::io::group;  int main() {     const float    s_period[]    = { 100, 50, 25, 12, 5, 7, 3, 2, 1 };     const unsigned s_timerscount = sizeof( s_period ) / sizeof( s_period[0] );     float  min = 10000000;     float  max = 0;     double sum = 0.0;      (size_t = 0; < s_timerscount; ++i) {         // ...         std::cout << format("id: %2d, expected: %3.0f, min: %3.0f, max: %3.0f, avg: %10.6f\n")                             % ( + 1 ) % s_period[i] % min % max % sum;     }      return 0; } 

(live example)


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -