fork download
  1. #include <ostd/algorithm.hh>
  2. #include <ostd/vector.hh>
  3. #include <ostd/map.hh>
  4. #include <ostd/range.hh>
  5. #include <ostd/io.hh>
  6.  
  7. using namespace ostd;
  8.  
  9. struct Foo {
  10. template<typename R>
  11. bool to_format(R &writer, const FormatSpec &fs) const {
  12. switch (fs.spec()) {
  13. case 'i':
  14. writer.put_n("foo", 3);
  15. break;
  16. default:
  17. writer.put_n("bar", 3);
  18. break;
  19. }
  20. return true;
  21. }
  22. };
  23.  
  24. int main() {
  25. Vector<int> x = { 5, 10, 15, 20 };
  26. writefln("[%(%s|%)]", x);
  27.  
  28. int y[] = { 2, 4, 8, 16, 32 };
  29. writefln("{ %(%s, %) }", y);
  30.  
  31. writefln("[\n%([ %(%s, %) ]%|,\n%)\n]", map(range(10), [](int v) {
  32. return range(v + 1);
  33. }));
  34.  
  35. Map<String, int> m = {
  36. { "foo", 5 },
  37. { "bar", 10 },
  38. { "baz", 15 }
  39. };
  40. /* strings and chars are automatically escaped */
  41. writefln("{ %(%s: %d, %) }", m);
  42. /* can override escaping with the - flag */
  43. writefln("{ %-(%s: %d, %) }", m);
  44.  
  45. /* custom format */
  46. writefln("%s", Foo());
  47. writefln("%i", Foo());
  48.  
  49. /* format into string */
  50. auto s = appender<String>();
  51. format(s, "hello %s", "world");
  52. writeln(s.get());
  53.  
  54. return 0;
  55. }
  56.  
  57. /*
  58. [5|10|15|20]
  59. { 2, 4, 8, 16, 32 }
  60. [
  61. [ 0 ],
  62. [ 0, 1 ],
  63. [ 0, 1, 2 ],
  64. [ 0, 1, 2, 3 ],
  65. [ 0, 1, 2, 3, 4 ],
  66. [ 0, 1, 2, 3, 4, 5 ],
  67. [ 0, 1, 2, 3, 4, 5, 6 ],
  68. [ 0, 1, 2, 3, 4, 5, 6, 7 ],
  69. [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ],
  70. [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
  71. ]
  72. { "baz": 15, "bar": 10, "foo": 5 }
  73. { baz: 15, bar: 10, foo: 5 }
  74. bar
  75. foo
  76. hello world
  77. */
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:29: fatal error: ostd/algorithm.hh: No such file or directory
compilation terminated.
stdout
Standard output is empty