Sorted Sequences ExampleThe following program reads a sequence of strings terminated by "stop" and builds a sorted sequencesortseq<string,int> .
int is a dummy type which is not used in the program. Then a pair (s1,s2)
of strings is read and all input strings between s1 and s2 in S are printed.
#include <LEDA/core/sortseq.h> #include <LEDA/core/string.h> using namespace leda; int main() { sortseq<string,int> S; //objects of type int (dummy), keys of type string string s1,s2; std::cout << "Input a sequence of strings terminated by 'stop'.\n"; while (std::cin >> s1 && s1 != "stop") S.insert(s1,0); while (true) { std::cout << "\nInput a pair of strings.\n\n"; std::cin >> s1 >> s2; std::cout << "All strings s with " << s1 << "<= s <= " << s2 << ":\n"; if (s2<s1) continue; seq_item last=S.locate_pred(s2); seq_item first=S.locate(s1); if (!first||!last||first==S.succ(last)) continue; seq_item it=first; while (true) { std::cout << "\n" << S.key(it); if (it==last) break; it=S.succ(it); } } return 0; } |
See also:Other data types with key of linearly ordered type: Data type with key of type pointer, item, or int: Maps Data type with key of hashed type: Hashing Arrays Manual Entries: |