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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
#include <iostream>
#include <string>
#include <regex>
#include <map>
#include <vector>
#include <sstream>
#include <iomanip>
// if the line contains just a single integer of up to five digits, it is a query number
bool is_query_number( const std::string& line )
{
// beginning of string, zero or more white space, one to five decimal digits,
// zero or more white space, end of string
static const std::regex number_re( "^\\s*\\d{1,5}\\s*$" ) ;
return std::regex_match( line, number_re ) ;
}
// return a vector of alphanumeric tokens in the line
std::vector<std::string> get_tokens( const std::string& line )
{
static const std::regex words_re( "\\w++" ) ;
std::vector<std::string> result ;
std::sregex_iterator iter( line.begin(), line.end(), words_re ), end ;
for( ; iter != end ; ++iter ) result.push_back( iter->str() ) ;
return result ;
}
std::map< int, std::vector<std::string> > get_queries( std::istream& stm )
{
std::map< int, std::vector<std::string> > result ;
int query_number = 0 ; // default initial query number is zero
std::string line ;
while( std::getline( stm, line ) ) // for each line in the stream
{
// if this line contains a new query number, update query_number
if( is_query_number(line) ) query_number = std::stoi(line) ;
// otherwise add the tokens in the line to the current query
else for( const auto& tok : get_tokens(line) ) result[query_number].push_back(tok) ;
}
return result ;
}
int main()
{
// create some data for testing
std::istringstream file( "1\n"
"seven five one\n"
" 2 \n"
" three four nine fifteen \n"
"twelve 44 nineteen\n"
"3\n"
"two one\n"
"eleven\n"
) ;
for( const auto& [ query_number, tokens ] : get_queries(file) )
{
std::cout << "query #" << query_number << " [ " ;
for( const auto& tok : tokens ) std::cout << std::quoted(tok) << ' ' ;
std::cout << "]\n\n" ;
}
}
|