Using a generic algorithm must contain header files #inlucde <algorithm>
The standard library also defines a set of generalized arithmetic algorithms that have the same naming conventions as generic algorithms, including header file # include <numeric>
Find
Vector<int>::const_iterator result = Find (Vec.begin (), Vec.end (), Search_value)
When found, returns an iterator to the element, returning the second iterator argument without finding it.
1vector<string> VEC = {"a","b","C"}; Requires compiler support C++112vector<string>::iterator it = Find (Vec.begin (), Vec.end (),"a");3 strings = *it;4cout<<s<<endl;
Because the find operation is iterator-based, you can use the same find function to look up values in any container.
Similarly, because the pointer behaves like an iterator acting on a built-in array, you can also use Find to search for an array: int ia[6] = {27, 210, 12, 47, 109, 83};
int *result = FIND (IA, IA + 6, 12);
Accumulate
The algorithm is defined in the numeric header file. Assuming that VEC is a vector object of type int, the following code:
int sum = accumulate (Vec.begin (), Vec.end (), 42);
Set sum to the element of VEC plus 42. The third parameter is the cumulative initial value.
The third argument that specifies the cumulative starting value is necessary because accumulate is ignorant of the type of element that will be accumulated
This fact has a two-story meaning. First, you must pass a starting value when calling the function, otherwise accumulate will not know what starting value to use. Second, the element type inside the container must match the type of the third argument, or it can be converted to a type of the third argument.
Find_first_of
The algorithm takes two pairs of iterator parameters to mark the range of two elements, finds the element that matches any element in the second paragraph in the first range, and then returns an iterator that points to the first matching element. If no element is found, the end iterator of the first range is returned.
List<string>::iterator it = find_first_of (Roster1.begin (), Roster1.end (), Roster2.begin (), Roster2.end ());
The types of roster1 and roster2 do not have to match exactly: Roster1 can be a list object, and roster2 can be a vector object, as long as the elements of these two sequences can be compared using the equality (= =) operator.
Fill
Fill (Vec.begin (), Vec.end (), 0); Set iterator range element value to 0
If the input range is valid, it can be safely written. This algorithm only writes to elements that already exist in the input range.
Fill_n
The parameters that are included are an iterator, a counter, and a value. The function starts with the element that the iterator points to, setting the specified number of elements to the given value. The Fill_n function assumes that it is safe to write to a specified number of elements.
A common mistake for beginners is to invoke the Fill_n function (or a similar write-element algorithm) on an empty container without elements.
Vector<int> VEC; Empty
Fill_n (Vec.begin (), 10, 0);
The invocation of this fill_n function will have disastrous consequences. We specify to write 10 elements, but these elements do not exist--vec is empty. The result is undefined and is likely to cause a serious run-time error.
Back_inserter
This function is an iterator adapter that inserts an iterator that can add elements to the underlying container
Using Back_inserter, you can generate an iterator that points to the Fill_n write target:
Vector<int> VEC; Empty
Fill_n (Back_inserter (VEC), 10, 0);
Now, each time the Fill_n function writes a value, it is implemented by the back_inserter generated insert iterator. The effect is quite
To Push_back on the VEC, add 10 elements at the end of the VEC, with each element having a value of 0.
Copy
With three iterator parameters: the first two specify the input range, and the third point points to an element of the target sequence. The target sequence passed to copy must be at least as large as the input range. Assuming that Ilst is a list object that holds int data, you can copy it to a vector object as follows:
Vector<int> Ivec; Empty
Copy (Ilst.begin (), Ilst.end (), Back_inserter (Ivec));
Copy reads the elements from the input range and then copies them to the target Ivec.
Of course, the efficiency of this example is poor: In general, if you want to create a new container for a replica with an existing container, a better approach is to use the input range directly as the initialization of the new constructed container:
Vector<int> Ivec (Ilst.begin (), Ilst.end ());
Replace
This algorithm accepts the third iterator argument, specifying the target location to save the adjusted sequence.
Replace (Ilst.begin (), Ilist.end (), 0,42)
If you do not want to change the original sequence, call Replace_copy ()
Vector<int> Ivec;
Replace_copy (Ilst.begin (), Ilst.end (), Back_inserter (Ivec), 0, 42);
After calling the function, Ilst does not change, Ivec stores ilst a copy, and all of the 0 in Ilst becomes 42 in Ivec.
The algorithm for reordering container elements
Sort Sort by word length
Remove all duplicate words call the unique "delete" of the adjacent duplicate values. The "delete" is quoted because the unique does not actually delete any elements, but instead copies the duplicate elements to the front of the sequence, overwriting the adjacent repeating elements. The unique returned iterator points to the next position beyond the end of the no-duplicates element range. Then use erase.
Count the number of words with a length equal to or more than 6 characters define the function yourself
1#include <iostream>2 using namespacestd;3#include <vector>4#include <algorithm>5 6 BOOLGT6 (Const string&s)//define the function to count whether the word length exceeds 67 {8 returnS.size () >=5;9 }Ten One intMain () A { -vector<string> VEC = {"Fox","jumps"," Over","Quick","Red","Red","Slow"," the"," the","Turtle"}; -Sort (Vec.begin (), Vec.end ());//Sort thevector<string>::iterator it = unique (Vec.begin (), Vec.end ());//moves a repeating element, returning an iterator that does not repeat the next position -Vec.erase (It,vec.end ());//Delete duplicate elements - -vector<string>::iterator it1 =Vec.begin (); + intn =0; - while(It1! =vec.end ()) + { A if(GT6 (*it1)) atn++; -cout<<*it1++<<" "; - } -cout<<endl<<"The words size no less than 5 has:"<<n<<Endl; - - return 0; in}
You can also use the COUNT_IF function statistic, note that GT6 is not bracketed
1 int n = count_if (Vec.begin (), Vec.end (), GT6); 2 cout<<endl<<"Thewords size no less than 5 has:"<<n<<endl;
Talk about iterators again (P347 don't see much)
The iterator defined by the standard library does not depend on a specific container
Other iterators: Inserting iterators, iostream iterators, reverse iterators, const_iterator
C + + primer 11 Generic algorithm