Advertisement
Frumkin

Untitled

Apr 25th, 2022
1,365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstddef>
  4.  
  5. using size = std::size_t;
  6. using byte = std::uint8_t;
  7. using ByteVector = std::vector<byte>;
  8.  
  9. struct String {
  10.     private:
  11.         ByteVector bytes;
  12.     public:
  13.         ByteVector& Bytes() { return bytes; };
  14.         size Capacity() const { return bytes.capacity(); }
  15.         size Total() const { return bytes.size(); }
  16.         byte& Get(const size index) { return bytes[index]; }
  17.  
  18.         void Reserve(const size capacity) { bytes.reserve(capacity); }
  19.         void Resize(const size total) { bytes.resize(total); }
  20.         void Push(String string) {
  21.             bytes.reserve(bytes.size() + string.Total());
  22.             for (size index = 0; index < string.Total(); index += 1) {
  23.                 bytes[bytes.size() + index] = string.Get(index);
  24.             }
  25.         }
  26.  
  27.         String() { bytes = ByteVector(); }
  28.         String(const char* string) {
  29.             bytes = ByteVector(strlen(string));
  30.             for (size index = 0; index < bytes.size(); index += 1) {
  31.                 bytes[index] = *(string + index);
  32.             }
  33.         }
  34. };
  35.  
  36. std::ostream& operator <<(std::ostream& out, String& string) {
  37.     for (size index = 0; index < string.Capacity(); index += 1)
  38.         out << string.Get(index);
  39.     return out;
  40. }
  41.  
  42. int main(int argc, char** argv) {
  43.     String string("Hello world!");
  44.     string.Push(String(" This is my custom string."));
  45.  
  46.     std::cout << string << std::endl;
  47.  
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement