fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <array>
  4. #include <algorithm>
  5. #include <cstring>
  6.  
  7. const uint STRING_LENGTH = 25;
  8.  
  9. class fixedString
  10. {
  11. std::array<char, STRING_LENGTH+1> cdata;
  12.  
  13. public:
  14. fixedString(const std::string &s)
  15. : fixedString(s.c_str(), s.size())
  16. {
  17. }
  18.  
  19. fixedString(const char *s)
  20. : fixedString(s, std::strlen(s))
  21. {
  22. }
  23.  
  24. fixedString(const char *s, const size_t len)
  25. {
  26. std::copy_n(s, std::min<size_t>(len, STRING_LENGTH), cdata.begin());
  27. if (len < STRING_LENGTH)
  28. std::fill_n(cdata.begin()+len, STRING_LENGTH-len, ' ');
  29. cdata[STRING_LENGTH] = '\0';
  30. }
  31.  
  32. friend std::ostream& operator<<(std::ostream& os, const fixedString &s)
  33. {
  34. os << s.cdata.data();
  35. return os;
  36. }
  37. };
  38.  
  39. struct parameter {
  40. double value;
  41. fixedString name;
  42.  
  43. parameter(double _value, const fixedString &_name)
  44. : value(_value), name(_name)
  45. {
  46. }
  47. };
  48.  
  49. int main() {
  50. parameter A(0.5, "my_variable");
  51. parameter B(-0.1, "my other variable with the name that is too long and has to be truncated!");
  52.  
  53. std::cout << "parameter name: " << A.name << "\tis:\t" << A.value << '\n';
  54. std::cout << "parameter name: " << B.name << "\tis:\t" << B.value << '\n';
  55. }
Success #stdin #stdout 0.01s 5492KB
stdin
Standard input is empty
stdout
parameter name: my_variable              	is:	0.5
parameter name: my other variable with th	is:	-0.1