Actually the task itself sounds like this:

Create a class string that should contain the following information: the length of the string, the number of bytes that the string occupies. The class must have a destructor, a default constructor and a constructor with a parameter (Allocate memory for a line in blocks of a given length).

Write a method of combining strings, the result should contain only those characters that are in the first line and not in the second, followed by the characters of the second line which are not in the first.

It seems like I was able to deal with the allocation of memory, but then I got stuck and do not understand what to do.

Here is the code:

#include <iostream> #include <string.h> using namespace std; class String { private: char *_string; uint32_t memlen, step, len; public: String():_string(new char[0]), memlen(0), step(10), len(0){} String(const char *str, int step = 10) { if (step > 0) this -> step = step; else this -> step = 16; len = strlen(str); cout << this -> len << endl; memlen = step * ((len + step) / step); this -> _string = new char[memlen]; strcpy(_string, str); } ~String() { delete [] _string; } // ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ°, Ρ€Π°Π±ΠΎΡ‚Π°Π΅Ρ‚ Π»ΠΈ Π²Ρ‹Π΄Π΅Π»Π΅Π½ΠΈΠ΅ памяти void show() { for(uint32_t i=0; i<len; i++) cout<<_string[i]; cout<<endl; } }; int main() { String s("Hello World!"); s.show(); return 0; } 
  • Not an answer, but you were not told the most important thing: Without the rule of three, your class cannot be used in real programs. :( - HolyBlackCat
  • I understand that all this is one big crutch, but for the time being, I cannot do otherwise. - Sweet _ Cat

1 answer 1

Well, something like this. Works for O (n), where n is the greatest of the lengths of two lines

 ...класс String... void str_union(const char *str2) { const size_t alphabet_size = 128; // мноТСства символов ΠΏΠ΅Ρ€Π²ΠΎΠΉ ΠΈ Π²Ρ‚ΠΎΡ€ΠΎΠΉ строк // Ρ‚ΡƒΡ‚ Π±Ρ‹Π»ΠΎ Π±Ρ‹ Π»ΡƒΡ‡ΡˆΠ΅ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ std::unordered_set<char>, Π½ΠΎ ΠΈ Ρ‚Π°ΠΊ сойдёт bool char_set1[alphabet_size]{}; bool char_set2[alphabet_size]{}; const char *str1 = _string; size_t len1 = len; size_t len2 = strlen(str2); // заносим символы ΠΏΠ΅Ρ€Π²ΠΎΠΉ строки Π² мноТСство for (size_t i = 0; i < len1; i++) { char_set1[str1[i]] = true; } // заносим символы Π²Ρ‚ΠΎΡ€ΠΎΠΉ строки Π² мноТСство for (size_t i = 0; i < len2; i++) { char_set2[str2[i]] = true; } // ΠΏΠΎΡ‚Π΅Π½Ρ†ΠΈΠ°Π»ΡŒΠ½Π°Ρ Π΄Π»ΠΈΠ½Π° Π½ΠΎΠ²ΠΎΠΉ строки с Π½ΡƒΠ»ΡŒ символом; size_t new_size = len1 + len2 + 1; size_t new_memlen = step * ((new_size + step - 1) / step); // -1 для ΠΊΠΎΡ€Ρ€Π΅ΠΊΡ‚Π½ΠΎΠ³ΠΎ округлСния Π²Π²Π΅Ρ€Ρ… char *new_string = new char[new_memlen]; size_t new_len = 0; for (size_t i = 0; i < len1; i++) { // провСряСм Π΅ΡΡ‚ΡŒ Π»ΠΈ символ ΠΈΠ· ΠΏΠ΅Ρ€Π²ΠΎΠΉ строки Π²ΠΎ мноТСствС символов Π²Ρ‚ΠΎΡ€ΠΎΠΉ строки if (char_set2[str1[i]] == false) { //Ссли Π½Π΅Ρ‚, Ρ‚ΠΎ ΠΊΠΎΠΏΠΈΡ€ΡƒΠ΅ΠΌ Π² Π½ΠΎΠ²ΡƒΡŽ строку new_string[new_len++] = str1[i]; } } for (size_t i = 0; i < len2; i++) { // провСряСм Π΅ΡΡ‚ΡŒ Π»ΠΈ символ ΠΈΠ· Π²Ρ‚ΠΎΡ€ΠΎΠΉ строки Π²ΠΎ мноТСствС символов ΠΏΠ΅Ρ€Π²ΠΎΠΉ строки if (char_set1[str2[i]] == false) { new_string[new_len++] = str2[i]; } } new_string[new_len] = '\0'; // для ΡƒΠΌΠ΅Π½ΡŒΡˆΠ΅Π½ΠΈΡ памяти size_t buff_memlen = step * ((new_len + 1 + step - 1) / step); // +1 для Π½ΡƒΠ»ΡŒ-символа if (buff_memlen < new_memlen) { char *buff_string = new char[buff_memlen]; strcpy(buff_string, new_string); delete[] new_string; new_string = buff_string; new_memlen = buff_memlen; } len = new_len; memlen = new_memlen; delete[] _string; _string = new_string; } ...класс String... ...Π² main... String s("Hello World!"); s.str_union("ello rd;hei-TlhoErRdE"); s.show();