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..
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'.
#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
}