//professional c++ 4th edition p489
#include <algorithm>
#include <numeric>
#include <iostream>
#include <iterator>
#include <vector>
template<typename Container>
void populateContainer(Container& cont)
{
int num;
while (true) {
std::cout << "Enter a number (0 to quit): ";
std::cin >> num;
if (num == 0) {
break;
}
cont.push_back(num);
}
}
int main()
{
std::vector<int> myVector1(10);
iota(begin(myVector1), end(myVector1), 1); // Fill vector with 1,2,3...10
// Print the contents of the vector.
copy(cbegin(myVector1), cend(myVector1), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
//professional c++ 4th edition p490
std::cout << "Enter numbers separated by white space." << std::endl;
std::cout << "Press Ctrl+D followed by Enter to stop." << std::endl;
std::istream_iterator<int> numbersIter(std::cin);
std::istream_iterator<int> endIter;
int sum = accumulate(numbersIter, endIter, 0);
std::cout << "Sum: " << sum << std::endl;
//professional c++ 4th edition p490
std::vector<int> myVector2;
populateContainer(myVector2);
int num;
std::cout << "Enter a number to find: ";
std::cin >> num;
auto it1 = std::find(begin(myVector2), end(myVector2), num);// type of it1 is vector<int>::iterator
auto it2 = std::find(rbegin(myVector2), rend(myVector2), num);// type of it2 is vector<int>::reverse_iterator
if (it1 != end(myVector2)) {
std::cout << "Found " << num << " at position " << it1 - begin(myVector2)
<< " going forward." << std::endl;
std::cout << "Found " << num << " at position "
<< it2.base() - 1 - begin(myVector2) << " going backward." << std::endl;
} else {
std::cout << "Failed to find " << num << std::endl;
}
//professional c++ 4th edition p490
std::vector<int> vectorOne, vectorTwo;
populateContainer(vectorOne);
std::back_insert_iterator<std::vector<int>> inserter(vectorTwo);
std::copy_if(cbegin(vectorOne), cend(vectorOne), inserter,
[](int i){ return i != 100; });
std::copy_if(cbegin(vectorOne), cend(vectorOne),
std::back_inserter(vectorTwo), [](int i) { return i != 100; });
// Using C++17's template argument deduction for constructors.
std::copy_if(cbegin(vectorOne), cend(vectorOne),
std::back_insert_iterator(vectorTwo), [](int i) { return i != 100; });
copy(cbegin(vectorTwo), cend(vectorTwo), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
return 0;
}
/*
wannian07@wannian07-PC:~$ g++ -std=c++17 -o c13 c13.cpp -pthread
wannian07@wannian07-PC:~$ ./c13
1 2 3 4 5 6 7 8 9 10
Enter numbers separated by white space.
Press Ctrl+D followed by Enter to stop.
11 22 33 44 55 66 Sum: 231
Enter a number (0 to quit): 11
Enter a number (0 to quit): 22
Enter a number (0 to quit): 33
Enter a number (0 to quit): 22
Enter a number (0 to quit): 11
Enter a number (0 to quit): 0
Enter a number to find: 22
Found 22 at position 1 going forward.
Found 22 at position 3 going backward.
Enter a number (0 to quit): 11
Enter a number (0 to quit): 22
Enter a number (0 to quit): 33
Enter a number (0 to quit): 44
Enter a number (0 to quit): 0
11 22 33 44
Enter a number (0 to quit): 11
Enter a number (0 to quit): 22
Enter a number (0 to quit): 33
Enter a number (0 to quit): 44
Enter a number (0 to quit): 55
Enter a number (0 to quit): 0
11 22 33 44 55 11 22 33 44 55 11 22 33 44 55
*/