Increment by 10 in set iterators

My container (set) is a dictionary consisting 25000 words and I need to get the output of 10 words at a time using the iterator and ask user to proceed. if yes print another 10.
With the following code I can print the whole lot but I can no seem to change the iterator to it+=10.
Appreciate any help..
1
2
3
4
5
6
7
        set<string>::iterator it;

        cout<<"mySet contains: "<<endl;
	for (it=Dictionary.begin(); it!=Dictionary.end(); it++)
	{
		cout<<*it<<endl;
	}	
The set iterator is only required to satisfy BidirectionalIterator
http://en.cppreference.com/w/cpp/concept/BidirectionalIterator

Use std::advance
http://en.cppreference.com/w/cpp/iterator/advance
> I can no seem to change the iterator to it+=10.

The iterator of std::set is not a RandomAccessIterator

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
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <set>
#include <string>

bool more()
{
    std::cout << "print another 10 words (y/n)? " ;
    char c ;
    return std::cin >> c && ( c == 'y' || c == 'Y' ) ;
}

int main()
{
    std::set<std::string> dictionary ;
    // populate dictionary

    {
        // print 10 words at a time (C++11)
        std::size_t cnt = 0 ;
        for( const std::string& word : dictionary )
        {
            std::cout << word << '\n' ;
            ++cnt ;
            
            if( cnt == 10 )
            {
                if( more() ) cnt = 0 ;
                else break ;
            }
        }
    }

    {
        // print 10 words at a time (legacy C++)
        std::size_t cnt = 0 ;
        for( std::set<std::string>::iterator iter = dictionary.begin() ; iter != dictionary.end() ; ++iter )
        {
            std::cout << *iter << '\n' ;
            ++cnt ;
            
            if( cnt == 10 )
            {
                if( more() ) cnt = 0 ;
                else break ;
            }
        }
    }
}
Thank you for the above code. I got the idea now.
Really appreciate it.
Hmm.... break, is it?

It is generally considered bad form to use jump statements other than return
(break, continue and goto) if they can be avoided.

Though, the run of the mill palooka believes that goto is 'evil' and would often recommend an even more convoluted construct involving either break or continue (or both) as the 'improvement'.

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
36
37
38
39
40
41
42
#include <iostream>
#include <set>
#include <string>

bool more( std::size_t n )
{
    std::cout << "print another " << n << " words (y/n)? " ;
    char c ;
    return std::cin >> c && ( c == 'y' || c == 'Y' ) ;
}

// print the next n entries; return false if end of set is reached
// note: the iterator (passed by reference) is updated
bool print_n( const std::set<std::string>& dictionary,
              std::set<std::string>::const_iterator& iterator,
              std::size_t n )
{
    while( iterator != dictionary.end() && n > 0 )
    {
        std::cout << *iterator << '\n' ;
        ++iterator ;
        --n ;
    }

    return iterator != dictionary.end() ;
}

// print the n entries at a time, prompting the user for next n
void print_n( const std::set<std::string>& dictionary, std::size_t n = 10 )
{
    auto iter = dictionary.begin() ;
    while( print_n( dictionary, iter, n ) && more(n) ) ;
}


int main()
{
    std::set<std::string> dictionary ;
    // populate dictionary

    print_n( dictionary, 10 ) ; // print 10 words at a time
}
Topic archived. No new replies allowed.