+1 vote
in C Plus Plus by

New C++ Algorithms

1 Answer

0 votes
by

The C++11 Standard Library defines new algorithms that mimic the set theory operations all_of()any_of() and none_of(). The following listing applies the predicate ispositive() to the range [first, first+n) and uses all_of()any_of() and none_of() to examine the range's properties:

#include <algorithm>

//C++11 code

//are all of the elements positive?

all_of(first, first+n, ispositive()); //false

//is there at least one positive element?

any_of(first, first+n, ispositive());//true

// are none of the elements positive?

none_of(first, first+n, ispositive()); //false

A new category of copy_n algorithms is also available. Using copy_n(), copying an array of 5 elements to another array is a cinch:

#include

int source[5]={0,12,34,50,80};

int target[5];

//copy 5 elements from source to target

copy_n(source,5,target);

The algorithm iota() creates a range of sequentially increasing values, as if by assigning an initial value to *first, then incrementing that value using prefix ++. In the following listing, iota() assigns the consecutive values {10,11,12,13,14} to the array arr, and {'a', 'b', 'c'} to the char array c.

include <numeric>

int a[5]={0};

char c[3]={0};

iota(a, a+5, 10); //changes a to {10,11,12,13,14}

iota(c, c+3, 'a'); //{'a','b','c'}

C++11 still lacks a few useful libraries such as an XML API, sockets, GUI, reflection — and yes, a proper automated garbage collector. However, it does offer plenty of new features that will make C++ more secure, efficient (yes, even more efficient than it has been thus far! See Google's benchmark tests), and easier to learn and use.

If the changes in C++11 seem overwhelming, don't be alarmed. Take the time to digest these changes gradually. 

Related questions

+1 vote
asked Jan 2, 2020 in C Plus Plus by GeorgeBell
+1 vote
asked Jan 2, 2020 in C Plus Plus by GeorgeBell
+1 vote
asked Jan 2, 2020 in C Plus Plus by GeorgeBell
+1 vote
asked Jan 2, 2020 in C Plus Plus by GeorgeBell
+1 vote
asked Jan 2, 2020 in C Plus Plus by GeorgeBell
+1 vote
asked Jan 2, 2020 in C Plus Plus by GeorgeBell
+1 vote
asked Jan 2, 2020 in C Plus Plus by GeorgeBell
+1 vote
asked Jan 2, 2020 in C Plus Plus by GeorgeBell
0 votes
asked Jan 2, 2020 by GeorgeBell
+1 vote
asked Mar 17, 2020 in C Plus Plus by SakshiSharma
...