| Example Program Iterator Basics Simple iterator functions. File "iterator.cpp"
1 | #include <iostream>
2 | #include <seqan/sequence.h>
3 | #include <seqan/file.h>
4 |
5 | int main()
6 | {
7 | seqan::String<char> str = "admn";
The metafunction Iterator returns the iterator type for a given container type.
8 | seqan::Iterator<seqan::String<char> >::Type it = begin(str);
9 |
10 | std::cout << *it; //output: 'a'
The following lines show a loop through str in the standard library style:
11 | while (it != end(str)) //output: "admn"
12 | {
13 | std::cout << *it;
14 | ++it;
15 | }
16 | std::cout << std::endl;
Seqan offers an alternative style for accessing iterators that avoids operators. Note that the functions goBegin and atEnd do net get str as arguments, because it2 is a rooted iterator. The following loop increments each character in str:
17 | seqan::Iterator<seqan::String<char>, seqan::Rooted >::Type it2 = begin(str);
18 | for (goBegin(it2); !atEnd(it2); goNext(it2))
19 | {
20 | ++value(it2);
21 | }
This is a reverse loop through str. Note that goPrevious is called before the value of it2 is accessed, because the end position of a container is the position behind the last item in the container:
22 | goEnd(it2);
23 |
24 | while (!atBegin(it2)) //output: "oneb"
25 | {
26 | goPrevious(it2);
27 | std::cout << getValue(it2);
28 | }
29 | std::cout << std::endl;
Another (write only) way to access the value of an iterator is assignValue:
30 | assignValue(begin(str), 'X');
31 |
32 | std::cout << str << std::endl; //output: "Xeno"
33 |
34 |
35 | return 0;
36 | }
See atBegin, atEnd, begin, end, getValue, goBegin, goEnd, goNext, goPrevious, Iterator, setValue, value See Also Rooted Iterators SeqAn - Sequence Analysis Library - www.seqan.de