There are many algorithms STL, such as searching, sorting, copying, reordering, modifying, and other standard numerical computation algorithms.
High applicability of the STL algorithms, is not only great for the sole use of containers.
table of Contents
(Click on the blue header, go to the appropriate position to read)
min_element and max_element
call this function when the need to pass two parameters, namely the scope of treatment. It returns the iterator pointing to the largest or smallest element.
Examples
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef vector<int> Vector;
typedef Vector::iterator Iterator;
int main()
{
Vector vec;
Iterator p;
for(int i = 0;i <= 10;i++)
vec.push_back(rand() % 10);
cout << "生成的序列:" << endl;
for (auto tp : vec)
cout << tp << " ";
cout << endl;
p = min_element(vec.begin(), vec.end());
cout << *p << endl;
p = max_element(vec.begin(), vec.end());
cout << *p << endl;
return 0;
}
Implementation of the results
Note: When the maximum or minimum element is not only time returns the first iterator meet the conditions of maximum or minimum element.
the Sort
the Sort sorting algorithm is known as an element within the parameters set out by the two intervals, of course, the collation may be the default (default from small to large) or pass a sort criteria.
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef vector<int> Vector;
typedef Vector::iterator Iterator;
int main()
{
Vector vec;
Iterator p;
for(int i = 0;i <= 10;i++)
vec.push_back(rand() % 10);
cout << "生成的序列:" << endl;
for (auto tp : vec)
cout << tp << " ";
cout << endl;
sort(vec.begin(), vec.end());
cout << "排序后:" << endl;
for (auto tp : vec)
cout << tp << " ";
return 0;
}
Implementation of the results
Own election rules
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef vector<int> Vector;
typedef Vector::iterator Iterator;
template<class T>
bool cmp(T a, T b)
{
return a > b;
}
int main()
{
Vector vec;
Iterator p;
for(int i = 0;i <= 10;i++)
vec.push_back(rand() % 10);
cout << "生成的序列:" << endl;
for (auto tp : vec)
cout << tp << " ";
cout << endl;
sort(vec.begin(), vec.end(),cmp<int>);
cout << "排序后:" << endl;
for (auto tp : vec)
cout << tp << " ";
return 0;
}
Implementation of the results
should be noted that min_element and max_element
the function does not consider the end of the bounds of the elements in the calculation.
find
find an element in the container, if it returns a pointer to the iterator, no early return to container iterator to the next position of the last element.
find(vec.negin(),vec.end(),value);//valu就是需要查找的值
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef vector<int> Vector;
typedef Vector::iterator Iterator;
int main()
{
Vector vec;
Iterator p;
for(int i = 0;i <= 10;i++)
vec.push_back(rand() % 10);
cout << "生成的序列:" << endl;
for (auto tp : vec)
cout << tp << " ";
cout << endl;
p = find(vec.begin(), vec.end(), 2);
if (p != vec.end())
cout << *p << endl;
else
cout << "No" << endl;
return 0;
}
Implementation of the results
attention vector
and did not find
function exists, it is not vec.find(value)
called.
reverse
reverse(vec.begin(),vec.end());//反转区间元素
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef vector<int> Vector;
typedef Vector::iterator Iterator;
int main()
{
Vector vec;
Iterator p;
for(int i = 0;i <= 10;i++)
vec.push_back(rand() % 10);
cout << "生成的序列:" << endl;
for (auto tp : vec)
cout << tp << " ";
cout << endl;
reverse(vec.begin(), vec.end());
cout << "reverse后结果" << endl;
for (auto tp : vec)
cout << tp << " ";
return 0;
}
Implementation of the results
equal
euqal interval is used to compare for equality
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef vector<int> Vector;
typedef Vector::iterator Iterator;
int main()
{
Vector vec;
Iterator p;
for(int i = 0;i <= 10;i++)
vec.push_back(rand() % 10);
Vector vec_2(vec);
cout << "生成的序列vec:" << endl;
for (auto tp : vec)
cout << tp << " ";
cout << endl;
cout << "复制的序列vec_2:" << endl;
for (auto tp : vec_2)
cout << tp << " ";
cout << endl;
cout << "三个参数:"<<equal(vec.begin(), vec.end(), vec_2.begin()) << endl;
cout << "四个参数:" << equal(vec.begin(), vec.end(), vec_2.begin(),vec_2.end()) << endl;
vec_2.push_back(100);
cout << "vec_2增加一个元素后比较结果" << endl;
cout << "三个参数:" << equal(vec.begin(), vec.end(), vec_2.begin()) << endl;
cout << "四个参数:" << equal(vec.begin(), vec.end(), vec_2.begin(), vec_2.end()) << endl;
vec_2.pop_back();
vec.push_back(111);
cout << "vec增加一个元素后比较结果" << endl;
cout << "三个参数:" << equal(vec.begin(), vec.end(), vec_2.begin()) << endl;
cout << "四个参数:" << equal(vec.begin(), vec.end(), vec_2.begin(), vec_2.end()) << endl;
return 0;
}
Implementation of the results
wherein
three parameters : the first two parameters are the start and end of a sequence of iterations, a third parameter is the iteration is the second sequence, then the first and second sequence to a sequence comparison of the number of elements determined by the first length of the sequence. If the second sequence of elements contained in less than the first sequence, the result is undefined.
Four parameters : a sequence of start and end of the iteration, the second sequence start and end iterator, if different lengths of the two sequences, the result is always false.
Of course, equa
the comparison rules may be rewritten as
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef vector<int> Vector;
typedef Vector::iterator Iterator;
int main()
{
Vector vec,vec_2;
Iterator p;
for(int i = 0;i <= 10;i++)
vec.push_back(rand() % 10);
cout << "生成的序列vec:" << endl;
for (auto tp : vec)
cout << tp << " ";
cout << endl;
for (int i = 0; i <= 10; i++)
vec_2.push_back(rand() % 10);
cout << endl;
cout << "生成的序列vec_2:" << endl;
for (auto tp : vec_2)
cout << tp << " ";
cout << endl;
cout << "三个参数:" << equal(vec.begin(), vec.end(), vec_2.begin(), [](const int a, const int b) { return a == b; }) << endl;
cout << "四个参数:" << equal(vec.begin(), vec.end(), vec_2.begin(),vec_2.end()) << endl;
return 0;
}
Implementation of the results
should not with equal () to compare the map from a random sequence, or set of elements in the container. In the random container, a given element in the order given and may be stored in a different set of elements equal unordered another container, since the different elements of the container is likely to be assigned to a different grid. (This conclusion from c.biancheng.net )