The so-called generic algorithm
is actually a series of common operations, most of which are independent of any particular container and are generic, so called generic algorithms.
Generic algorithms contain a lot of things, and only some basic operations are recorded here. Lambda expressions, closure operations and other iterators, and the operation of the use, after the subsequent recording. Find:
The Find function is used to look up a certain range of elements, the data ordered unordered can be, if you want to find the type is not the basic data type, you need to overload operator = =
Find function of the source code as follows, very good understanding.
Template<class Inputiterator, class t>
inputiterator find (Inputiterator-I, Inputiterator last, const t& Amp val)
{while
(first!=last) {
if (*first==val)
return A; ++first;
}
return to last;
}
Three examples of finding arrays, finding containers, and finding the types you define
Code from the CPP website, modified
#include <bits/stdc++.h> using namespace std; struct node {int x;
BOOL operator = = (Const node &n) const {return this->x==n.x;}};
int main () {Ios::sync_with_stdio (false);
Use pointer int myints[]={10, 20, 30, 40};
int* p;
P=find (myints, myints+4, 30);
if (p!=myints+4) cout<<*p<<endl; Or you can do so auto Pp=find (myints), End (myints), 30;//Returns a pointer if (Pp!=end (myints)) cout<<*pp<<
Endl
Using iterators vector<int> myvector (myints,myints+4);
Vector<int>::iterator it;
It=find (Myvector.begin (), Myvector.end (), 30);
if (It!=myvector.end ()) cout<<*it<< ' \ n ';
Self-defined type vector<node> ve;
for (int i=0;i<10;i++) Ve.push_back (Node{i});
Node Mark;
mark.x=3;
Auto Pos=find (Ve.begin (), Ve.end (), Mark);
if (Pos!=ve.end ()) cout<<pos->x<<endl;
return 0; }
The lookup function also has lower_bound, binary_search and so on function, the subsequent record. Sort:
The most frequently used algorithm in C + + should be the sort algorithm, which is also the sort function. And, of course, there are partial_sort.
As well as stable_sort, not commonly used, after the introduction.
Sort functions are sorted by default from small to large, and if you want to sort custom types, you can overload operators or customize comparison functions.
#include <bits/stdc++.h>
using namespace std;
BOOL MyFunction (int I,int j) {return (I<J);}
struct MyClass
{
bool operator () (int i,int j) {return (I<J);}
} MyObject;
int main ()
{
Ios::sync_with_stdio (false);
int myints[] = {32,71,12,45,26,80,53,33};
Sort the array without using an iterator
(Begin (Myints), End (myints));
Or as follows, the effect is the same
sort (myints,myints+8);
If you use iterators, it is best to put them in a container to operate
std::vector<int> Myvector (myints, myints+8);
comparison//using default (Operator <):
Std::sort (Myvector.begin (), myvector.be Gin () +4); (comp) (Std::sort) (
myvector.begin () +4, Myvector.end (), myfunction); (m)
//using object as Comp
std::sort (Myvector.begin (), Myvector.end (), MyObject);
//(a) return
0;
to weigh:
The unique effect is to remove the repeating element between adjacent elements.
The unique function enables you to weight the data in a sorted order, and if it is a custom type, you need to overload the operator.
But the unique go-to-weight is not the real deletion of duplicate elements, but the repetition of the elements into the back, with the erase function can play to repeat the role of elements.
The source code is as follows (very classic Oh, when the interview usually like this, hehe)
Template <class forwarditerator>
forwarditerator Unique (ForwardIterator, ForwardIterator, last)
{
if (first==last) return to last
; ForwardIterator result = A;
while (++first!= last)
{
if (!) ( *result = = *first)) //Or:if (!pred (*result,*first)) for version (2)
* (++result) =*first;
}
return ++result;
}
Example:
Code from the CPP official website
#include <bits/stdc++.h> using namespace std;
BOOL MyFunction (int i, int j) {return (I==J);}
int main () {Ios::sync_with_stdio (false); int myints[] = {10,20,20,20,30,30,20,20,10};
The std::vector<int> myvector (myints,myints+9);
Using default Comparison:std::vector<int>::iterator it; Move the adjacent repeating elements to the back. It = Std::unique (Myvector.begin (), Myvector.end ()); 10 20 30 20 10? ? ?
? ^ The//distance function is to compute the distance between two iterators, and then resize the memory to remove duplicates after the calculation myvector.resize (std::d istance (myvector.be Gin (), it)); 10 20 30 20 10//Custom Operations Std::unique (Myvector.begin (), Myvector.end (), myfunction);
(no changes)//print out Content:std::cout << "myvector contains:";
For (It=myvector.begin (); It!=myvector.end (); ++it) std::cout << ' << *it;
Std::cout << ' \ n '; return 0;
}
Simple to go to the heavy code
#include <bits/stdc++.h>
using namespace std;
int main ()
{
Ios::sync_with_stdio (false);
Vector<int> ve={1,2,3,4,2,4,1,3};
Sort (Ve.begin (), Ve.end ());
Auto End_it=unique (Ve.begin (), Ve.end ());
Ve.erase (End_it,ve.end ());
for (auto x:ve)
cout<<x<<endl;
return 0;
}
add up:
The accumulate function enables the accumulation of a set of data that can be accumulated, and if the user overloads the operator, it can do similar things. Users can also add custom function operations to implement different ways of accumulating.
The source code is as follows, where Init is the initial value
Template <class Inputiterator, class t>
t accumulate (inputiterator-I, Inputiterator last, T init)
{ C17/>while (first!=last) {
init = init + *first; Or:init=binary_op (Init,*first) for the binary_op version
++first;
}
return init;
}
Use case, code from CPP website
#include <bits/stdc++.h> using namespace std; int myfunction (int x, int y) {return x+2*y.} struct MyClass {int operator () (int x, int y) {return x+3*y;}} MyObject
;
int main () {Ios::sync_with_stdio (false);
int init = 100;
int numbers[] = {10,20,30};
Std::cout << "Using default accumulate:";
Std::cout << std::accumulate (numbers,numbers+3,init);//160 std::cout << ' \ n ';
Std::cout << "Using functional ' s minus:"; Std::cout << std::accumulate (Numbers, numbers+3, init, std::minus<int> ());//40 Std::cout << ;
' \ n ';
Std::cout << "Using custom Function:";
Std::cout << std::accumulate (Numbers, numbers+3, init, myfunction);//220 std::cout << ' \ n ';
Std::cout << "Using custom object:";
Std::cout << std::accumulate (Numbers, numbers+3, init, MyObject);//280 std::cout << ' \ n ';
return 0; }
Assign value:
The fill function can be assigned a value to a certain range within the container, accepting 3 parameters altogether, similar to the Memset function.
Source code is as follows
Template <class ForwardIterator, class t>
void Fill (ForwardIterator, ForwardIterator, last, const t& V AL)
{while
(the!= last) {
*first = val;
++first
}
}
Use examples, very simple
#include <bits/stdc++.h>
using namespace std;
int main ()
{
Ios::sync_with_stdio (false);
Std::vector<int> Myvector (8); myvector:0 0 0 0 0 0 0 0
std::fill (myvector.begin (), Myvector.begin () +4,5); Myvector:5 5 5 5 0 0 0 0
std::fill (myvector.begin () +3,myvector.end () -2,8); Myvector:5 5 5 8 8 8 0 0 return
0;
}
There is also a fill_n function, which differs from the fill function by the parameters it receives. Three parameters it accepts are not ranges
Outputiterator Fill_n (Outputiterator, Size N, const t& val);
First is the starting address, n indicates that the number of n-1 from the beginning to the following is assigned to Val, and undefined behavior occurs if the range of n exceeds the size of the container.
Source code is as follows
Template <class Outputiterator, class Size, class t>
outputiterator fill_n (Outputiterator-A, Size N, const t& val)
{while
(n>0) {
*first = val;
++first; --n;
}
return A; Since C++11
}
Use examples
Code from the CPP official website
#include <bits/stdc++.h>
using namespace std;
int main ()
{
Ios::sync_with_stdio (false);
Std::vector<int> Myvector (8,10); Myvector:10 (
myvector.begin (), 4,20) Std::fill_n; Myvector:20
Std::fill_n (Myvector.begin () +3,3,33); Myvector:20 return
0;
}
Copy:
The copy function accepts three parameters
Outputiterator copy (Inputiterator, inputiterator, outputiterator result);
First and last represent the range of replicated data, and result represents the starting position of the assigned container, where the assigned container size is at least as long as [First,last].
Source:
Template<class Inputiterator, class outputiterator>
outputiterator copy (Inputiterator-I, InputIterator Last, outputiterator result)
{while
(first!=last) {
*result = *first;
++result; ++first;
}
return result;
}
Use examples
Code from the CPP official website
#include <bits/stdc++.h> using namespace std;
int main () {Ios::sync_with_stdio (false);
int myints[]={10,20,30,40,50,60,70};
Std::vector<int> Myvector (7);
Std::copy (myints, Myints+7, Myvector.begin ());
The example on the book int a1[]={1,2,3,4,5};
int a2[sizeof (A1)/sizeof (*A1)];//a2 the same size as A1 Auto Ret=copy (begin (A1), end (A1), a2);
The RET returns to the end of the A2 assignment return 0; }