Iterator Basics

| Example Program Iterator Basics Simple iterator functions. File "iterator.cpp" A tutorial about the use of iterators.
1 | #include <iostream>
2 | #include <seqan/sequence.h>
3 | #include <seqan/file.h>
4 |
5 | int main()
6 | {
The metafunction Iterator returns the iterator type for a given container type.
7 | seqan::String<char> str = "admn";
8 | seqan::Iterator<seqan::String<char> >::Type it = begin(str);
9 | seqan::Iterator<seqan::String<char> >::Type itEnd = end(str);
We can use iterators to iterate over the elements of a container.
10 | while (it != itEnd) {
11 | ::std::cout << *it;
12 | ++it;
13 | }
14 | ::std::cout << ::std::endl;
Rooted iterators know their container (rooted iterator). Hence, the functions goBegin and atEnd do not get str as an argument. The following loop increments each character in str.
15 | seqan::Iterator<seqan::String<char>, seqan::Rooted >::Type it2 = begin(str);
16 | for (goBegin(it2); !atEnd(it2); goNext(it2))
17 | {
18 | ++value(it2);
19 | }
Some iterators support an iteration in reverse order. Note that goPrevious is called before the value of it2 is accessed. Remember that the end position of a container is always the position behind the last item in the container.
20 | goEnd(it2);
21 | while (!atBegin(it2))
22 | {
23 | goPrevious(it2);
24 | ::std::cout << getValue(it2);
25 | }
26 | ::std::cout << ::std::endl;
assignValue can be used to change the value of an iterator.
27 | assignValue(begin(str), 'X');
28 | ::std::cout << str << ::std::endl;
29 |
30 | return 0;
31 | }
See begin, setValue, end, goBegin, Iterator, atEnd, goEnd, atBegin, value, getValue, goPrevious, goNext See Also Rooted Iterators SeqAn - Sequence Analysis Library - www.seqan.de

Page built @2011/02/08 21:37:01