diff options
| author | Packy Anderson <PackyAnderson@gmail.com> | 2024-05-06 23:41:04 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-06 23:41:04 -0400 |
| commit | 8da3f7983b54d8c8eb96326f4d7f3d52f1a78d9b (patch) | |
| tree | 4cc9a6443382d2a32208246c481cd566b6ea0142 /challenge-266/ulrich-rieke/cpp/ch-1.cpp | |
| parent | e4a2c66b30346606d347ecc4a08b83b733797d2a (diff) | |
| parent | c1756b0e7aed0ad70fa63feb2565c69215c9d426 (diff) | |
| download | perlweeklychallenge-club-8da3f7983b54d8c8eb96326f4d7f3d52f1a78d9b.tar.gz perlweeklychallenge-club-8da3f7983b54d8c8eb96326f4d7f3d52f1a78d9b.tar.bz2 perlweeklychallenge-club-8da3f7983b54d8c8eb96326f4d7f3d52f1a78d9b.zip | |
Merge branch 'manwar:master' into challenge-268
Diffstat (limited to 'challenge-266/ulrich-rieke/cpp/ch-1.cpp')
| -rwxr-xr-x | challenge-266/ulrich-rieke/cpp/ch-1.cpp | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/challenge-266/ulrich-rieke/cpp/ch-1.cpp b/challenge-266/ulrich-rieke/cpp/ch-1.cpp new file mode 100755 index 0000000000..84477e81ec --- /dev/null +++ b/challenge-266/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,57 @@ +#include <iostream>
+#include <vector>
+#include <string>
+#include <map>
+#include <utility>
+#include <ranges>
+#include <algorithm>
+
+namespace rng = std::ranges ;
+
+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> firstWords { split( line, " " ) } ;
+ std::cout << "Enter some more words, separated by blanks!\n" ;
+ line.clear( ) ;
+ std::getline( std::cin , line ) ;
+ std::vector<std::string> secondWords { split( line, " " ) } ;
+ std::map<std::string, int> frequencies ;
+ for ( auto it = firstWords.begin( ) ; it != firstWords.end( ) ; it++ )
+ frequencies[*it]++ ;
+ for ( auto it = secondWords.begin( ) ; it != secondWords.end( ) ; it++ )
+ frequencies[*it]++ ;
+ std::vector<std::pair<std::string, int>> allWords { frequencies.begin( ) ,
+ frequencies.end( ) } ;
+ std::vector<std::string> selected ;
+ if ( allWords.size( ) > 0 ) {
+ std::cout << "( " ;
+ for ( auto p : allWords | rng::views::filter([]( auto pa ){ return pa.second ==
+ 1 ; } )) {
+ selected.push_back( p.first ) ;
+ }
+ for ( auto w : selected )
+ std::cout << w << ' ' ;
+ std::cout << ")\n" ;
+ }
+ else {
+ std::cout << "()\n" ;
+ }
+ return 0 ;
+}
+
+
|
