powered by simpleCommunicator - 2.0.61     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / C++ [игнор отключен] [закрыт для гостей] / Пользовательские итераторы -- интеграция с STL
1 сообщений из 1, страница 1 из 1
Пользовательские итераторы -- интеграция с STL
    #32462926
black zorro
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Написал я тут давеча класс Кольцевого Списка
Добавил к нему итератор и пробую к нему добавить поддержку работы
со стандартными алгоритмами stl, для find , count получилось без проблемм
а вот для sort проблемы подскажите каков шаблон итератора должен быть
на всякий случай ниже привожу код класса может кому интересно будет

Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
134.
135.
136.
137.
138.
139.
140.
141.
142.
143.
144.
145.
146.
147.
148.
149.
150.
151.
152.
153.
154.
155.
156.
157.
158.
159.
160.
161.
162.
163.
164.
165.
166.
167.
168.
169.
170.
171.
172.
173.
174.
175.
176.
177.
178.
179.
180.
181.
182.
183.
184.
185.
186.
187.
188.
189.
190.
191.
192.
193.
194.
195.
196.
197.
198.
199.
200.
201.
202.
203.
204.
205.
206.
207.
208.
209.
210.
211.
212.
213.
214.
215.
216.
217.
218.
219.
220.
221.
222.
223.
224.
225.
226.
227.
228.
229.
230.
231.
232.
233.
234.
235.
236.
237.
238.
239.
240.
241.
242.
243.
244.
245.
246.
247.
248.
249.
250.
251.
252.
253.
254.
255.
256.
257.
258.
259.
260.
261.
262.
263.
264.
265.
266.
267.
268.
269.
270.
271.
272.
273.
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>

using namespace std;

struct empty_quenue_operation {
	string operation;
	empty_quenue_operation (string mes) : operation (mes) {}
	operator string  (){
		return operation;
	}
};


template <class T>
class RoundedQuenue {
 struct TNode {
	T inf;
	TNode * next;
	operator T (){
		return inf;
	}
 };

 public:
 
 unsigned unique_time;//  ? ??????? ?? ???????
 struct iterator {
	 TNode * curNode;
	 RoundedQuenue * parent ; 

	 unsigned local_unique_time;//  ? ??????? ?? ???????
	 void test_correct_time (){
		 if (local_unique_time != parent->unique_time)
			 throw empty_quenue_operation ( "times are  different: invalid iterator" );
	 }

	 iterator & operator ++ (){
		 test_correct_time ();
		 if (curNode)
			curNode = curNode->next;
		 else
			throw empty_quenue_operation ( "iterator & operator ++ ()" );
		 return *this;
	 }
	 iterator & operator  -- (){
 
		 test_correct_time ();
		 if (curNode)
			 curNode = parent->goEnd (curNode);
		 else
			throw empty_quenue_operation ( "iterator & operator ++ ()" );
		 return *this;
	 }


	 iterator & operator ++ (int){
		  return ++(*this);
	 }

 	 iterator & operator  -- (int){
 
		  return  --(*this);
 
	 }

	 T & operator * (){
		 test_correct_time ();
		 if (curNode)
			return curNode->inf;
		 else
			throw empty_quenue_operation ( "T & operator * ()" );
	 }



	 bool operator == (const iterator & an){
		 test_correct_time ();
		 return curNode == an.curNode;
	 }

	 bool operator != (const iterator & an){
		 return curNode != an.curNode;
	 }
	
	 iterator (TNode * _curNode , RoundedQuenue * _parent,  unsigned time_image):
		curNode(_curNode),
		local_unique_time(time_image),
		parent (_parent)
	{}

	 iterator ():
		curNode(NULL),
		local_unique_time(- 1 ),
		parent (NULL)
	{}
 };

 

 TNode * entry_point;
 TNode * goEnd (TNode * end) {
	 if (end == NULL) return NULL;

	 TNode * cur = end;
	 while (cur->next != end)
		 cur = cur->next;
	 return cur;
 }
 public:
	 RoundedQuenue () : entry_point (NULL), unique_time ( 0 ) {};
	 void push_back (const T & info){

		 unique_time++;

		 TNode * end  =  goEnd (entry_point);
		 TNode * now = new TNode ();
  	     now->inf = info;
		 
		 if (end != NULL){
			 end->next = now;
			 now->next = entry_point;
			 return;
		 }
		 entry_point = now;
		 now->next = entry_point;
		 
	 }

	 bool pop_back (void){
		return pop_selected (entry_point);
	 }

	 bool pop_selected (TNode * who){
		TNode * end  =  goEnd (who);
		if (end == NULL) return false;
		TNode * prev_end = goEnd (end);
		if (prev_end == end){
			// ???? ??????? ? ???????
			delete entry_point;
			entry_point  = NULL;
			return true;
		}
		prev_end->next = end->next;
		delete end;
		return true;
	 }

	 TNode * get_at_position (int offset_from_entry_point, bool cyclic = true){
		 if (cyclic){
			 TNode * pointer  = entry_point;
			 for (;offset_from_entry_point --;)
 
				 pointer = goEnd(pointer);
			 return pointer;
		 }
		 TNode * pointer  = entry_point;
		 for (;offset_from_entry_point -- ;){
 
			pointer = goEnd(pointer);
			if (pointer == entry_point) return NULL;
		 }
		 return pointer;
	 }

	 void printClock (void){
		 cout <<  "---- Rounded Quenue --- \n";
 
		 if (entry_point == NULL) {
			 cout << "() " << endl;
			 cout << "  ---- End Of Rounded Quenue --- \n";
 
			 return;
		 }
		 TNode * end = entry_point; 
		 do{
			 end  =  goEnd (end);	
			 cout << end->inf <<  ", " ;
		 }
		 while (end != entry_point);
		 cout <<  "\n---- End Of Rounded Quenue --- \n";
 

	 }
	 ~RoundedQuenue () {
		while (pop_back());
	 };

	 iterator iterate_entry_point (void){
		 return iterator (entry_point , this, unique_time);
	 }

	 iterator iterate_back_point (void){
		 return iterator (goEnd(entry_point) , this, unique_time);
	 }

	 T & operator [] (int cyclic_index) {
		 return get_at_position(cyclic_index)->inf;
	 }
	 operator int (){
		 if (entry_point == NULL) {
			  return  0  ;
		 }
		 int cnt =  0 ;
		 TNode * end = entry_point; 
		 do{
			 end  =  goEnd (end);	
			 cnt++;
		 }
		 while (end != entry_point);
		 return cnt;
	 }
};

void main (void){
	RoundedQuenue <char> rc;
	rc.printClock ();
	rc.push_back ('a');
	rc.printClock ();
	rc.push_back ('b');
	rc.printClock ();
	rc.push_back ('c');
	rc.printClock ();
	rc.pop_back ();
	rc.printClock ();
	rc.push_back ('d');
	rc.push_back ('b');
	rc.push_back ('z');
	rc.push_back ('b');
	rc.push_back ('a');
	rc.printClock ();

	RoundedQuenue <char>::iterator I = rc.iterate_entry_point ();
	RoundedQuenue <char>::iterator IStartedFrom = rc.iterate_entry_point ();
	
	do {
		cout << *I << endl;
		I++;
	}
	while (I !=IStartedFrom);
	I --;
 
	rc.push_back ('d');
	try{
		I --;
 
		cout << *I << endl;
	}
	catch (empty_quenue_operation e){
		cout << "Error:  "<< e.operator string () << endl;
	}

	RoundedQuenue <char>::iterator Ipoz = find (rc.iterate_entry_point () , rc.iterate_back_point () , 'b');
	int diff = count (rc.iterate_entry_point () , rc.iterate_back_point () , 'b');
	if (Ipoz == rc.iterate_back_point())
		cout << " Not Found ";
	else
		cout << " Found:  " << diff;

	cout << " \nGet at  5  PoZition:  "<< rc.get_at_position (5 )->operator char () << endl;
	cout << "\nGet at  6  PoZition:  "<< rc.get_at_position (6 )->operator char () << endl;
	cout << "Size Of RoundedQuenue = " << rc << endl;
	for (int i =  0 ; i < int(rc) ; i++)
		cout << char(rc [i]) << endl;

	rc.printClock ();

//	sort (rc.iterate_entry_point () , rc.iterate_back_point () );
// !!!! HOW !!!!!

}
...
Рейтинг: 0 / 0
1 сообщений из 1, страница 1 из 1
Форумы / C++ [игнор отключен] [закрыт для гостей] / Пользовательские итераторы -- интеграция с STL
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]