CannibalSmith ([info]cannibalsmith) rakstīja [info]koderi kopienā,
Yeah, baby, yeah! Šitais strādā! Urā! Aleluja! (Paskaidrojums: tas ir orientēts grafs, un iterators to pārmeklē plašumā.)
class Node
{
public:
	template <class T>
	class Iterator : public std::iterator<forward_iterator_tag, Node>
	{
	public:
		Iterator(T* t) { open.push_back(t); }
		Iterator() { }
		Iterator(const Iterator& i) : open(i.open), closed(i.closed) { }
		Iterator& operator =(const Iterator& i) { open = i.open; closed = i.closed; }
		bool operator ==(const Iterator& i) const
		{
			if (!open.empty() && !i.open.empty())
			{
				return open.front() == i.open.front();
			}
			else if (open.empty() && i.open.empty())
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		bool operator !=(const Iterator& i) const { return !(*this == i); }
		T& operator *()
		{
			if (open.empty())
			{
				throw "Iterator cannot be dereferenced.";
			}
			return *open.front();
		}
		operator T&() { return **this; }
		T* operator ->() { return &**this; }
		Iterator& operator ++()
		{
			if (open.empty())
			{
				throw "Iterator cannot be incremented.";
			}
			T* i = open.front();
			open.pop_front();
			FOREACH(T* j, i->children)
			{
				if (find(closed.begin(), closed.end(), j) == closed.end() && find(open.begin(), open.end(), j) == open.end())
				{
					open.push_back(j);
				}
			}
			closed.push_back(i);
			return *this;
		}
		Iterator operator ++(int) { Iterator i(*this); ++*this; return i; }
	protected:
		list<T*> open;
		list<T*> closed;
	};
	typedef Iterator<Node> iterator;
	typedef Iterator<const Node> const_iterator;
	iterator begin() { return iterator(this); }
	iterator end() { return iterator(); }
	const_iterator begin() const { return const_iterator(this); }
	const_iterator end() const { return const_iterator(this); }

	/* [..] */
}


(Lasīt komentārus)

Nopūsties:

No:
Lietotājvārds:
Parole:
Ievadi te 'qws' (liidzeklis pret spambotiem):
Temats:
Tematā HTML ir aizliegts
  
Ziņa:

Gandrīz jau aizmirsu pateikt – šis lietotājs ir ieslēdzis IP adrešu noglabāšanu. Operatore Nr. 65.
Neesi iežurnalējies. Iežurnalēties?