CannibalSmith ([info]cannibalsmith) rakstīja [info]koderi kopienā,
@ 2008-05-07 09:27:00

Previous Entry  Add to memories!  Tell a Friend!  Next Entry
STL iteratori
Kas ir tas minimums klases metožu, kas nepieciešams, lai implementētu ar STL saderīgu uz priekšu ejošu iteratoru, kas lasa un modificē elementus, savai custom datu struktūrai? Gūglē neatradu tiešu un saprotamu atbildi. Mans galvenais mērķis ir spēt izmantot bezgala ērto BOOST_FOREACH.


(Lasīt komentārus) - (Ierakstīt jaunu komentāru)


[info]cannibalsmith
2008-05-23 12:40 (saite)
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); }

	/* [..] */
}

(Atbildēt uz šo) (Iepriekšējais) (Diskusija)


[info]cannibalsmith
2008-05-23 12:42 (saite)
Whoops, ; beigās aizmirsu un pareizi ir const_iterator end() const { return const_iterator(); } .

(Atbildēt uz šo) (Iepriekšējais) (Diskusija)


[info]bubu
2008-05-23 14:24 (saite)
Tev tur bugaina metode:
Iterator& operator =(const Iterator& i) { open = i.open; closed = i.closed; }

Tur vajag return *this;

(Atbildēt uz šo) (Iepriekšējais) (Diskusija)


[info]cannibalsmith
2008-05-23 14:27 (saite)
Paldies!

(Atbildēt uz šo) (Iepriekšējais)


(Lasīt komentārus) -

Neesi iežurnalējies. Iežurnalēties?