fork download
  1. /*
  2. String const hello("hello");
  3. String const hell = hello[0][4]; // теперь в hell хранится подстрока "hell"
  4. String const ell = hello[1][4]; // теперь в ell хранится подстрока "ell"
  5.  
  6.  
  7. Обратите внимание, что i может равняться j, в этом случае результатом должна быть пустая строка. Гарантируется,
  8. что i никогда не будет больше j, и они не будут выходить за пределы длины строки.
  9.  
  10. Требования к реализации: При выполнении задания вы можете создавать любые методы/конструкторы или даже структуры/классы,
  11. если они вам нужны. Реализовывать методы, которые уже объявленны в шаблоне, не нужно они уже реализованы. При выполнении
  12. задания не вводите и не выводите что-либо. Реализовывать функцию main не нужно.
  13. */
  14.  
  15. #include <cstddef> // size_t
  16. #include <cstring>
  17.  
  18. struct String {
  19. String(const char *str = "");
  20. String(size_t n, char c);
  21.  
  22. String(const String &other);
  23. String &operator=(const String &other);
  24.  
  25. void append(const String &other);
  26. ~String();
  27.  
  28. class Proxy {
  29. public:
  30. Proxy(char* const str):str(str), i(0) { }
  31.  
  32. String operator[](size_t j){
  33. return substr(i, j);
  34. }
  35.  
  36. String substr(size_t begin, size_t end) {
  37. char* tmp = static_cast<char*>(memcpy(new char[end - begin + 1], &str[begin], sizeof(char) * (end - begin)));
  38. tmp[end-begin] = '\0';
  39. return String(tmp);
  40. }
  41.  
  42. Proxy& setI(size_t i) {
  43. this->i = i;
  44. return *this;
  45. }
  46.  
  47. private:
  48. size_t i;
  49. char* const str;
  50. };
  51.  
  52. Proxy operator[] (size_t i) const {
  53. return Proxy(str).setI(i);
  54. }
  55.  
  56. size_t size;
  57. char *str;
  58. };
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
stdout
Standard output is empty