Set <string>

How can I move through a set of strings?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <set>
#include <string>
#include <iterator>

int main()
{
    std::set<std::string> set { "int", "main()", "#include", "<iterator>", "#include", "<string>", "<set>", "<iostream>" };

    {
        // range-based loop
        for( const auto& str : set ) std::cout << str << ' ' ;
        std::cout << '\n' ;
    }

    {
        // iterators
        for( auto iter = std::begin(set) ; iter != std::end(set) ; ++iter ) std::cout << *iter << ' ' ;
        std::cout << '\n' ;

        // iterators, in reverse
        for( auto iter = set.rbegin() ; iter != set.rend() ; ++iter ) std::cout << *iter << ' ' ;
        std::cout << '\n' ;
    }

    {
        // iterators (legacy)
        for( std::set<std::string>::iterator iter = set.begin() ; iter != set.end() ; ++iter ) std::cout << *iter << ' ' ;
        std::cout << '\n' ;

        // iterators, in reverse (legacy)
        for( std::set<std::string>::reverse_iterator iter = set.rbegin() ; iter != set.rend() ; ++iter ) std::cout << *iter << ' ' ;
        std::cout << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/69bab3a6e811bc57
1
2
3
4
for(auto const &str : my_set_of_strings)
{
    std::cout << str << std::endl;
}
Topic archived. No new replies allowed.