std iterator



Автор Аммм задал вопрос в разделе ВУЗы, Колледжи

Составить программу на C++. и получил лучший ответ

Ответ от ReanMee[активный]
#include <fstream> #include <algorithm> #include <iterator> template <class> class list { struct node { T data; node * next; node(T const& d = T(), node * n = 0) : data(d), next(n) {} }; public: typedef T value_type; struct iterator : public std::iterator<std::forward_iterator_tag,> { T & operator *() const { return ptr->data; } iterator & operator ++() { increment(); return *this; } iterator operator ++(int) { iterator copy(*this); increment(); return copy; } bool operator ==(iterator const& rhs) const { return ptr == rhs.ptr; } bool operator !=(iterator const& rhs) const { return !(*this == rhs); } iterator(node * n = 0) : ptr(n) {} private: void increment() { ptr = ptr->next; } node * ptr; }; iterator begin() { return iterator(head); } iterator end() { return iterator(0); } void push_front(T const& data) { if (head) { node * newNode = new node(data, head); head = newNode; } else { head = new node(data); } } void unique() { if (head) { node * p = head; while (p->next) { if (p->data == p->next->data) { node * tmp = p->next->next; delete p->next; p->next = tmp; } else { p = p->next; } } } } void clear() { while (head) { node * tmp = head; head = head->next; delete tmp; } head = 0; } void sort() { if (head && head->next) { for (node * p = head; p; p = p->next) { for (node * q = p->next; q; q = q->next) { if (q->data < p->data) { std::swap(q->data, p->data); } } } } } list() : head(0) {} ~list() { clear(); } private: node * head; }; template<typename> std::ostream & operator <<(std::ostream & stream, list<t> /*const*/ & l) { for (auto itr = l.begin(); itr != l.end(); ++itr) { stream << *itr << \"->\"; } stream << \"x\"; return stream; } template<typename> std::istream & operator >>(std::istream & stream, list<t> & l) { T t; while (stream >> t) { l.push_front(t); } return stream; } int main() { std::ofstream fout(\"out.txt\", std::ios::out); list<int> l1; std::ifstream fin1(\"list1.txt\", std::ios::in); fin1 >> l1; fout << \"list number 1:\\t\" << l1 << std::endl; list<int> l2; std::ifstream fin2(\"list2.txt\", std::ios::in); fin2 >> l2; fout << \"list number 2:\\t\" << l2 << std::endl; l1.sort(); l1.unique(); l2.sort(); l2.unique(); list<int> l; std::set_difference(l1.begin(), l1.end(), l2.begin(), l2.end(), std::front_inserter(l)); fout << \"unique l1/l2:\\t\" << l << std::endl; l.clear(); std::set_difference(l2.begin(), l2.end(), l1.begin(), l1.end(), std::front_inserter(l)); fout << \"unique l2/l1:\\t\" << l << std::endl; }

Ответ от 22 ответа[гуру]
Привет! Вот подборка тем с похожими вопросами и ответами на Ваш вопрос: Составить программу на C++.
Общественное телевидение России на Википедии
Посмотрите статью на википедии про Общественное телевидение России
 

Ответить на вопрос:

Имя*

E-mail:*

Текст ответа:*
Проверочный код(введите 22):*