fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Params {
  5. int p1;
  6. string p2;
  7. double p3;
  8. };
  9.  
  10. string foo(const Params p) {
  11. string result = "{";
  12. if (p.p1 != 0) {
  13. result += to_string(p.p1);
  14. }
  15.  
  16. if (p.p2.length() > 0) {
  17. if (result.length() > 0) {
  18. result += ", ";
  19. }
  20. result += p.p2;
  21. }
  22.  
  23. if (p.p3 != 0) {
  24. if (result.length() > 0) {
  25. result += ", ";
  26. }
  27. result += to_string(p.p3);
  28. }
  29.  
  30. result += "}";
  31. return result;
  32. }
  33.  
  34. int main() {
  35. cout << foo({2}) << endl;
  36. cout << foo({.p1 = 3, .p2 = "aa"}) << endl;
  37. cout << foo({.p1 = 3, .p2 = "aa", .p3 = 3.1}) << endl;
  38. //cout << foo({.p2 = "aa"}) << endl;
  39. cout << foo({.p1 = 4}) << endl;
  40. //cout << foo({.p3 = 5.0}) << endl;
  41. return 0;
  42. }
Success #stdin #stdout 0s 4376KB
stdin
Standard input is empty
stdout
{2}
{3, aa}
{3, aa, 3.100000}
{4}