C/C++

Список с элементами типа char.

Список с элементами типа char. Дополнительно перегрузить следующие
операции:
• + – объединить списки (list+list);
• -- – удалить элемент из начала (типа --list);
• == – проверка на равенство.
#include <iostream>
#include <list>
#include <string>
using namespace std;
class List {
public:
List() = default;
List(const List&) = default;
List(List&&) = default;
List& operator=(const List&) = default;
List& operator=(List&&) = default;
List(initializer_list<char> li) : box(li) {}
List(const string& s) : box(s.begin(), s.end()) {}
List(const char* s) {
auto i = 0U;
while (s[i]) box.push_back(s[i++]);
}
void push_back(const char ch) {
box.push_back(ch);
}
List& operator--() {
box.pop_front();
return *this;
}
List operator--(int) {
auto tmp = *this;
box.pop_front();
return tmp;
}
auto begin()const {
return box.begin();
}
auto end()const {
return box.end();
}
private:
list<char> box;
friend List operator+(const List& a, const List& b) {
List box = a;
for (auto ch : b.box) box.push_back(ch);
return box;
}
friend bool operator==(const List& a, const List& b) {
return a.box == b.box;
}
};
void show(const char* msg, const List& box) {
cout << msg;
for (auto x : box) cout.put(x);
puts("");
}
int main() {
List a = "01234";
show("a: ", a);
List b = "56789";
show("b: ", b);
auto ab = a + b;
show("a+b: ", ab);
auto c = ab;
show("c=ab: ", c);
if (c == ab) puts("c == ab");
else puts("c != ab");
--c;
show("--c: ", c);
if (c == ab) puts("c == ab");
else puts("c != ab");
ab--;
show("ab--: ", ab);
if (c == ab) puts("c == ab");
else puts("c != ab");
system("pause > nul");
}
Игорь Поляруш
Игорь Поляруш
50 547
Лучший ответ