First, non-mutation algorithm
is a set of template functions that do not break operational data, which is used to process sequential data, element lookup, subsequence Search, statistics, and match. Non-mutation algorithms have very wide applicability and can be applied to various containers basically.
1 Finding container element find
It is used to find elements that are equal to a value. It looks for an element equal to value on the iterator interval [first,last) (closed interval), returns the iterator I if the element referred to by the iterator I satisfies *i=value, and fails to find the element that satisfies the condition. Function prototype: Find (V1.begin (), V1.end (), num_to_find);
Copy Code code as follows:
#include <vector>
#include <algorithm>
#include <iostream>
using namespace Std;
void Main ()
{
int num_to_find = 6;
Vector<int> v1;
for (int i = 0; i < i++)
V1.push_back (2*i);
Vector<int>::iterator result;
result = Find (V1.begin (), V1.end (), num_to_find);
if (result = = V1.end ())
cout << "no element match found" << num_to_find << Endl;
Else
cout << "The index value of the matching element is" << result-v1.begin () << Endl;
}
2 Condition Lookup container element find_if
Using predicates that return Boolean values to Judge pred, check every element on the iterator interval [first,last) (closed interval), if iterator I satisfies pred (*i) =true, indicates that the element is found and returns the iteration value I (the first qualifying element found); Return to last position. Function prototype: find_if (V.begin (), V.end (), divby5);
Copy Code code as follows:
#include <vector>
#include <algorithm>
#include <iostream>
using namespace Std;
BOOL Divby5 (int x)
{
return x%5?0:1;
}
void Main ()
{
Vector<int> V (20);
for (int i=0;i<v.size (); i++)
{
v[i]= (i+1) * (i+3);
cout<<v[i]<< ';
}
cout<<endl;
Vector<int>::iterator ilocation;
Ilocation=find_if (V.begin (), V.end (), divby5);
if (Ilocation!=v.end ())
cout<< "Find the first element to be divisible by 5: The index position of the <<*ilocation<<endl<< element is:" <<ilocation-v.begin () < <endl;
}
3 Count the number of container elements equal to a value
List<int> l;
Count (L.begin (), L.end (), value)
4 Condition Statistics COUNT_IF
Count_if (L.begin (), L.end (), pred). The predicate pred the predicate in the same find_if as the meaning. Examples can refer to Example 2.
5 Sub-sequence search
The search algorithm function searches a sequence for a subsequence that matches another sequence. The parameters are the starting position of a sequence, the end position, and the beginning of another sequence, ending position.
Function prototypes: Search (V1.begin (), V1.end (), V2.begin (), V2.end ());
Copy Code code as follows:
#include <vector>
#include <algorithm>
#include <iostream>
using namespace Std;
void Main ()
{
Vector<int> v1;
cout<< "v1:";
for (int i=0;i<5;i++)
{
V1.push_back (i+5);
Note: The V1 definition does not have a given size, so it is not possible to use an assignment statement directly.
cout<<v1[i]<< ';
}
cout<<endl;
Vector<int> v2;
cout<< "V2:";
for (i=0;i<2;i++)
{
V2.push_back (I+7);
cout<<v2[i]<< ';
}
cout<<endl;
Vector<int>::iterator ilocation;
Ilocation=search (V1.begin (), V1.end (), V2.begin (), V2.end ());
if (Ilocation!=v1.end ())
cout<< "V2 elements are contained in v1, the starting element is" << "v1[" <<ilocation-v1.begin () << '] ' <<endl;
Else
cout<< "V2 elements are not included in V1" <<endl;
}
6 repeat element subsequence search Search_n
Search_n algorithm function Search sequence whether there is a series of element values are a given value of a child sequence. Function prototype: Search_n (V.begin (), V.end (), 3,8), found 3 consecutive elements in V 8
Copy Code code as follows:
#include <vector>
#include <algorithm>
#include <iostream>
using namespace Std;
void Main ()
{
Vector<int> v;
V.push_back (1);
V.push_back (8);
V.push_back (8);
V.push_back (8);
V.push_back (6);
V.push_back (6);
V.push_back (8);
Vector<int>::iterator i;
I=search_n (V.begin (), V.end (), 3,8);
if (I!=v.end ())
cout<< "Find 3 consecutive elements in V 8" <<endl;
Else
cout<< "3 consecutive elements not found in V 8" <<endl;
}
7 Last subsequence Search Find_end
Function prototype Find_end (V1.begin (), V1.end (), V2.begin (), V2.end ()), and locate the sequence required in V1 in the location required in the V2.
Copy Code code as follows:
#include <vector>
#include <algorithm>
#include <iostream>
using namespace Std;
void Main ()
{
Vector<int> v1;
V1.push_back (-5);
V1.push_back (1);
V1.push_back (2);
V1.push_back (-6);
V1.push_back (-8);
V1.push_back (1);
V1.push_back (2);
V1.push_back (-11);
Vector<int> v2;
V2.push_back (1);
V2.push_back (2);
Vector<int>::iterator i;
I=find_end (V1.begin (), V1.end (), V2.begin (), V2.end ());
if (I!=v1.end ())
cout<< "V1 to find the last matching v2 sequence in" << "v1[" <<i-v1.begin () << "]" <<endl;
}
second, mutation algorithm
is a set of template functions that can modify container element data. Copy (V.begin (), V.end (), L.begin ()), and the elements in V are copied into L.
1 Elements Copy Copy
Copy Code code as follows:
#include <vector>
#include <list>
#include <algorithm>
#include <iostream>
using namespace Std;
void Main ()
{
Vector<int> v;
V.push_back (1);
V.push_back (3);
V.push_back (5);
List<int> l;
L.push_back (2);
L.push_back (4);
L.push_back (6);
L.push_back (8);
L.push_back (10);
Copy (V.begin (), V.end (), L.begin ());
List<int>::iterator i;
For (I=l.begin (); I!=l.end (); i++)
cout<<*i<< ';
cout<<endl;
}
2 element Transform transform change
Function Prototypes: Transform (V.begin (), V.end (), L.begin (), square); replication, but replicated by some scheme.
Copy Code code as follows:
#include <vector>
#include <list>
#include <algorithm>
#include <iostream>
using namespace Std;
int square (int x)
{
return x*x;
}
void Main ()
{
Vector<int> v;
V.push_back (5);
V.push_back (15);
V.push_back (25);
List<int> L (3);
Transform (V.begin (), V.end (), L.begin (), square);
List<int>::iterator i;
For (I=l.begin (); I!=l.end (); i++)
cout<<*i<< ';
cout<<endl;
}
3 replacing replace
The replace algorithm replaces the specified element value with the new value.
Copy Code code as follows:
#include <vector>
#include <algorithm>
#include <iostream>
using namespace Std;
void Main ()
{
Vector<int> v;
V.push_back (13);
V.push_back (25);
V.push_back (27);
V.push_back (25);
V.push_back (29);
Replace (V.begin (), V.end (), 25,100);
Vector<int>::iterator i;
For (I=v.begin (); I!=v.end (); i++)
cout<<*i<< ';
cout<<endl;
}
Output is 13 100 27 100 29
4 Condition Replacement Replace_if
Function prototype: replace_if (V.begin (), V.end (), odd,100);
Copy Code code as follows:
#include <vector>
#include <algorithm>
#include <iostream>
using namespace Std;
bool Odd (int x)
{
return x%2;
}
void Main ()
{
Vector<int> v;
for (int i=1;i<10;i++)
V.push_back (i);
Replace_if (V.begin (), V.end (), odd,100);
Vector<int>::iterator ilocation;
For (Ilocation=v.begin (); Ilocation!=v.end (); ilocation++)
cout<<*ilocation<< ';
cout<<endl;
}
5 n times filled Fill_n
Function prototype Fill_n (V.begin (), 5,-1); jumps to the back 5 positions starting from V.begin-1
Copy Code code as follows:
#include <vector>
#include <algorithm>
#include <iostream>
using namespace Std;
void Main ()
{
Vector<int> V (10);
Fill_n (V.begin (), 5,-1);
Vector<int>::iterator ilocation;
For (Ilocation=v.begin (); Ilocation!=v.end (); ilocation++)
cout<<*ilocation<< ';
cout<<endl;
}
Output results:-1-1-1-1-1 0 0 0 0 0
6 randomly generated n elements generate
Function prototypes: Generate_n (V.begin (), 5,rand), and randomly fill in the data from the back 5 positions starting at V.begin.
Copy Code code as follows:
#include <vector>
#include <algorithm>
#include <iostream>
using namespace Std;
void Main ()
{
Vector<int> V (10);
Generate_n (V.begin (), 5,rand);
Vector<int>::iterator ilocation;
For (Ilocation=v.begin (); Ilocation!=v.end (); ilocation++)
cout<<*ilocation<< ';
cout<<endl;
}
7 Condition Removal remove_if
The return value is equivalent to removing the end () value of the new vector formed after the element that satisfies the condition is removed.
Function prototype: remove_if (V.begin (), V.end (), even);
Copy Code code as follows:
#include <vector>
#include <algorithm>
#include <iostream>
using namespace Std;
bool even (int x)
{
return x%2?0:1;
}
void Main ()
{
Vector<int> v;
for (int i=1;i<=10;i++)
V.push_back (i);
Vector<int>::iterator Ilocation,result;
cout<< "before removing:";
For (Ilocation=v.begin (); Ilocation!=v.end (); ilocation++)
cout<<*ilocation<< ';
cout<<endl;
Result=remove_if (V.begin (), V.end (), even);
cout<< "after removal:";
For (Ilocation=v.begin (); ilocation!=result;ilocation++)
cout<<*ilocation<< ';
cout<<endl;
}
8 Eliminate continuous repeating elements unique
Function prototype: Unique (V.begin (), V.end ());
Copy Code code as follows:
#include <vector>
#include <algorithm>
#include <iostream>
using namespace Std;
void Main ()
{
Vector<int> v;
V.push_back (2);
V.push_back (6);
V.push_back (6);
V.push_back (6);
V.push_back (9);
V.push_back (6);
V.push_back (3);
Vector<int>::iterator Ilocation,result;
Result=unique (V.begin (), V.end ());
For (Ilocation=v.begin (); ilocation!=result;ilocation++)
cout<<*ilocation<< ';
cout<<endl;
}
Output results: 2 6 9 6 3
Third, sorting algorithm
1. Create Heap Make_heap
2, element into the heap push_heap (default insert last Element)
3, elements out of the heap pop_heap (as with Push_heap, pop_heap must be on the heap operation to have meaning)
Copy Code code as follows:
#include <vector>
#include <algorithm>
#include <iostream>
using namespace Std;
void Main ()
{
Vector<int> v;
V.push_back (5);
V.push_back (6);
V.push_back (4);
V.push_back (8);
V.push_back (2);
V.push_back (3);
V.push_back (7);
V.push_back (1);
V.push_back (9);
Make_heap (V.begin (), V.end ());
V.push_back (20);
Push_heap (V.begin (), V.end ());
Vector<int>::iterator ilocation;
For (Ilocation=v.begin (); Ilocation!=v.end (); ilocation++)
cout<<*ilocation<< ';
cout<<endl;
Pop_heap (V.begin (), V.end ());
For (Ilocation=v.begin (); Ilocation!=v.end (); ilocation++)
cout<<*ilocation<< ';
cout<<endl;
}
4 heap Sort sort_heap
Use:
Copy Code code as follows:
Make_heap (V.begin (), V.end ());
Sort_heap (V.begin (), V.end ());
#include <vector>
#include <algorithm>
#include <iostream>
using namespace Std;
void Main ()
{
Vector<int> v;
V.push_back (3);
V.push_back (9);
V.push_back (6);
V.push_back (3);
V.push_back (17);
V.push_back (20);
V.push_back (12);
Vector<int>::iterator ilocation;
For (Ilocation=v.begin (); Ilocation!=v.end (); ilocation++)
cout<<*ilocation<< ';
cout<<endl;
Make_heap (V.begin (), V.end ());
Sort_heap (V.begin (), V.end ());
For (Ilocation=v.begin (); Ilocation!=v.end (); ilocation++)
cout<<*ilocation<< ';
cout<<endl;
}
Output results:
3 9 6 3 17 20 12
3 3 6 9 12 17 20
5 sorting sort
Function prototypes: Sort (V.begin (), V.end ());
Copy Code code as follows:
#include <vector>
#include <algorithm>
#include <iostream>
using namespace Std;
void Main ()
{
Vector<int> v;
V.push_back (2);
V.push_back (8);
V.push_back (-15);
V.push_back (90);
V.push_back (26);
V.push_back (7);
V.push_back (23);
V.push_back (30);
V.push_back (-27);
V.push_back (39);
V.push_back (55);
Vector<int>::iterator ilocation;
For (Ilocation=v.begin (); Ilocation!=v.end (); ilocation++)
cout<<*ilocation<< ';
cout<<endl;
Sort (V.begin (), V.end ());//Comparison function default
For (Ilocation=v.begin (); Ilocation!=v.end (); ilocation++)
cout<<*ilocation<< ';
cout<<endl;
}