Advertisement
RobertDeMilo

WB4.8 Форматирование данных при выводе в поток

Oct 11th, 2023
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <iomanip>
  6.  
  7. using namespace std;
  8.  
  9. void Print(const vector<string>& names, const vector <double>& values, int width)
  10. {
  11.     cout << setfill('.');
  12.     cout << left;
  13.  
  14.     for (const auto& n : names)
  15.     {
  16.         cout << setw(width) << n << ' ';
  17.     }
  18.  
  19.     cout << endl;
  20.     cout << fixed << setprecision(2);
  21.     // потоковые манипуляторы
  22.     // fixed 0.000005 vs 5e-06
  23.     // setprecision кол - во знаков после запятой
  24.  
  25.     for (const auto& v : values)
  26.     {
  27.         // setw() сбрасывается после вывода первого значения
  28.         cout << setw(width) << v << ' ';
  29.     }
  30. }
  31.  
  32. int main()
  33. {
  34.     vector <string> names = { "a","b","c" };
  35.     vector <double> values = { 5, 0.01, 0.000005 };
  36.    
  37.     Print(names, values, 10);
  38.  
  39.     return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement