# C++11 algorithms for C++98

Email
 Submitted on: 9/14/2016 3:30:29 PM By: Thomas1965 Level: Beginner User Rating: Unrated Compatibility: C++ (general), Microsoft Visual C++, Borland C++ Views: 2598

Show how to implement the C++11 algorithms any_of, none_of and all_of for previous versions of C++

code:
Can't Copy and Paste this?
 ``` //************************************** // Name: C++11 algorithms for C++98 // Description:Show how to implement the C++11 algorithms any_of, none_of and all_of for previous versions of C++ // By: Thomas1965 //************************************** #include #include typedef std::vector IntVector; namespace tom { // Test if any element in range fulfills condition template bool any_of (InputIterator first, InputIterator last, UnaryPredicate pred) { for(; first != last; ++first) { if(pred(*first)) { return true; } } return false; } // Test if all elements in range fulfills condition template bool all_of (InputIterator first, InputIterator last, UnaryPredicate pred) { for(; first != last; ++first) { if(!pred(*first)) { return false; } } return true; } //Test if no elements fulfill condition template bool none_of (InputIterator first, InputIterator last, UnaryPredicate pred) { for(; first != last; ++first) { if(pred(*first)) { return false; } } return true; } } void TestAnyOf(IntVector& vec); void TestAllOf(IntVector& vec); void TestNoneOf(IntVector& vec); int main() { int numbers[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int count = sizeof(numbers) / sizeof(numbers[0]); IntVector iv(&numbers[0], &numbers[count]); TestAnyOf(iv); TestAllOf(iv); TestNoneOf(iv); system("PAUSE"); return 0; } bool greater_than_10(int num) { return num > 10; } struct less_than_10 { bool operator() (int num) { return num < 10; } }; bool negativ(int num) { return num < 0; } void TestAnyOf(IntVector& vec) { std::cout << "\n** Testing std::any_of **\n"; if (tom::any_of(vec.begin(), vec.end(), greater_than_10)) std::cout << "At least one number is greater than 10" << "\n\n"; else std::cout << "Not one number is greater than 10" << "\n\n"; } void TestAllOf(IntVector& vec) { std::cout << "\n** Testing std::all_of **\n"; if (tom::all_of(vec.begin(), vec.end(), less_than_10())) std::cout << "All numbers are less than 10" << "\n\n"; else std::cout << "Not all numbers are less than 10" << "\n\n"; } void TestNoneOf(IntVector& vec) { std::cout << "\n** Testing std::none_of **\n"; if (tom::none_of(vec.begin(), vec.end(), negativ)) std::cout << "Not one number is negativ" << "\n\n"; else std::cout << "At least one number is negativ" << "\n\n"; }```

Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)