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
|
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <iterator>
#include <cctype>
std::vector<std::string> split( const std::string & startline ,
const std::string & sep ) {
std::vector<std::string> separated ;
std::string::size_type start { 0 } ;
std::string::size_type pos ;
do {
pos = startline.find_first_of( sep , start ) ;
separated.push_back( startline.substr(start , pos - start )) ;
start = pos + 1 ;
} while ( pos != std::string::npos ) ;
return separated ;
}
int main( ) {
std::cout << "Enter some words, separated by blanks!\n" ;
std::string line ;
std::getline( std::cin , line ) ;
std::vector<std::string> entered ( split( line , " " ) ) ;
int len = entered.size( ) ;
std::vector<std::string> allLower ;
for ( auto s : entered ) {
std::transform( s.begin( ) , s.end( ) , s.begin( ) , tolower ) ;
allLower.push_back( s ) ;
}
std::set<char> old_intersection ;
for ( char c : allLower[ 0 ] )
old_intersection.insert( c ) ;
if ( len == 1 ) {
}
else {
std::set<char> new_intersection ;
for ( int i = 1 ; i < len ; i++ ) {
std::set<char> wordSet ;
for ( char c : allLower[ i ] )
wordSet.insert( c ) ;
std::set_intersection( old_intersection.begin( ) ,
old_intersection.end( ) , wordSet.begin( ) , wordSet.end( ) ,
std::inserter( new_intersection , new_intersection.begin( ) ) ) ;
old_intersection = new_intersection ;
}
std::cout << "( " ;
std::copy( old_intersection.begin( ) , old_intersection.end( ) ,
std::ostream_iterator<char>( std::cout , " " )) ;
std::cout << ")" << std::endl ;
}
return 0 ;
}
|