aboutsummaryrefslogtreecommitdiff
path: root/challenge-279/ulrich-rieke/cpp/ch-1.cpp
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <mohammad.anwar@yahoo.com>2024-07-22 13:50:28 +0100
committerMohammad Sajid Anwar <mohammad.anwar@yahoo.com>2024-07-22 13:50:28 +0100
commit274bf4a39a2a89b95ebbc89cea4d775f2c8eaead (patch)
tree1e8169c57fcd742a6f6d337300182ec6772b9a2a /challenge-279/ulrich-rieke/cpp/ch-1.cpp
parent68787dcb78ada3349ae5f986f2cc39f33a665029 (diff)
downloadperlweeklychallenge-club-274bf4a39a2a89b95ebbc89cea4d775f2c8eaead.tar.gz
perlweeklychallenge-club-274bf4a39a2a89b95ebbc89cea4d775f2c8eaead.tar.bz2
perlweeklychallenge-club-274bf4a39a2a89b95ebbc89cea4d775f2c8eaead.zip
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-279/ulrich-rieke/cpp/ch-1.cpp')
-rwxr-xr-xchallenge-279/ulrich-rieke/cpp/ch-1.cpp38
1 files changed, 38 insertions, 0 deletions
diff --git a/challenge-279/ulrich-rieke/cpp/ch-1.cpp b/challenge-279/ulrich-rieke/cpp/ch-1.cpp
new file mode 100755
index 0000000000..8d6d6444c8
--- /dev/null
+++ b/challenge-279/ulrich-rieke/cpp/ch-1.cpp
@@ -0,0 +1,38 @@
+#include <iostream>
+#include <string>
+#include <algorithm>
+#include <utility>
+#include <sstream>
+#include <vector>
+
+std::vector<std::string> tokenize( const std::string & text , char delimiter ) {
+ std::istringstream allTokens { text } ;
+ std::vector<std::string> tokens ;
+ std::string tok ;
+ while ( std::getline( allTokens , tok , delimiter ) ) {
+ tokens.push_back( tok ) ;
+ }
+ return tokens ;
+}
+
+int main( ) {
+ std::cout << "Enter some letters, separated by blanks!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ std::vector<std::string> letterstrings { tokenize( line , ' ' ) } ;
+ std::cout << "Enter some weights, separated by blanks!\n" ;
+ std::getline( std::cin , line ) ;
+ std::vector<std::string> weightstrings { tokenize( line , ' ' ) } ;
+ std::vector<std::pair<char , int>> allPairs ;
+ for ( int i = 0 ; i < letterstrings.size( ) ; i++ ) {
+ allPairs.push_back( std::make_pair( letterstrings[i].front( ) , std::stoi(
+ weightstrings[i].substr( 0 , 1 ) ) ) ) ;
+ }
+ std::sort( allPairs.begin( ) , allPairs.end( ) , []( const auto & p1 , const auto & p2) {
+ return p1.second < p2.second ; } ) ;
+ for ( auto p : allPairs )
+ std::cout << p.first ;
+ std::cout << '\n' ;
+ return 0 ;
+}
+