aboutsummaryrefslogtreecommitdiff
path: root/challenge-098/ulrich-rieke/cpp/ch-2.cpp
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2021-02-05 08:10:36 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2021-02-05 08:10:36 +0000
commitec44ee8cda213a62a7cf5f123be96f799bfd86cd (patch)
tree432752c149a9624ce69532b9e2716ef3c1e27edc /challenge-098/ulrich-rieke/cpp/ch-2.cpp
parent058c1a99b31cd3ce5b6c6b9d1dd3e609b9b9acb9 (diff)
downloadperlweeklychallenge-club-ec44ee8cda213a62a7cf5f123be96f799bfd86cd.tar.gz
perlweeklychallenge-club-ec44ee8cda213a62a7cf5f123be96f799bfd86cd.tar.bz2
perlweeklychallenge-club-ec44ee8cda213a62a7cf5f123be96f799bfd86cd.zip
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-098/ulrich-rieke/cpp/ch-2.cpp')
-rw-r--r--challenge-098/ulrich-rieke/cpp/ch-2.cpp39
1 files changed, 39 insertions, 0 deletions
diff --git a/challenge-098/ulrich-rieke/cpp/ch-2.cpp b/challenge-098/ulrich-rieke/cpp/ch-2.cpp
new file mode 100644
index 0000000000..d4b571c3fc
--- /dev/null
+++ b/challenge-098/ulrich-rieke/cpp/ch-2.cpp
@@ -0,0 +1,39 @@
+#include <list>
+#include <iostream>
+#include <iterator>
+#include <string>
+#include <algorithm>
+#include <vector>
+#include <cstdlib>
+#include <iterator>
+
+std::list<int> enterArray( ) {
+ std::vector<int> numbers ;
+ std::cout << "Enter numbers, e to end!\n" ;
+ std::string input ;
+ std::getline( std::cin , input ) ;
+ while ( input != "e" ) {
+ numbers.push_back( std::stoi( input ) ) ;
+ std::getline( std::cin , input ) ;
+ }
+ std::sort( numbers.begin( ) , numbers.end( ) ) ;
+ //conversion to list in order to apply unique
+ std::list<int> nums( numbers.begin( ) , numbers.end( ) ) ;
+ nums.unique( ) ;
+ return nums ;
+}
+
+int main( int argc, char * argv[ 0 ] ) {
+ if ( argc != 2 ) {
+ std::cerr << "Error! syntax <challenge098_2> <number to insert>!\n" ;
+ return 1 ;
+ }
+ std::list<int> numbers = enterArray( ) ;
+ int n = std::atoi( argv[ 1 ] ) ;
+ //the algorithm lower_bound precisely does what is required here
+ //inserting a number into an array without disturbing the order
+ auto insertPlace = std::lower_bound( numbers.begin( ) , numbers.end( ) , n ) ;
+ std::cout << static_cast<int>( std::distance( numbers.begin( ) , insertPlace ) ) ;
+ std::cout << '\n' ;
+ return 0 ;
+}