- max(x,y):返回x、y中的最大值。
- min(x,y):返回x、y中的最小值。
#include <iostream>
#include <algorithm>
int main () {
std::cout << "max(1,2)==" << std::max(1,2) << '\n';
std::cout << "max(2,1)==" << std::max(2,1) << '\n';
std::cout << "max('a','z')==" << std::max('a','z') << '\n';
std::cout << "max(3.14,2.73)==" << std::max(3.14,2.73) << '\n';
return 0;
}
- abs(x):返回整数x的绝对值,注意x必须是整数。浮点型的绝对值用math头文件下的fabs(x).
- swap(x,y):交换x,y的值。 reverse(it,it2):将数组或容器的[it,it2]区间内的元素反转,it、it2为指针或者迭代器。常用于字符串的反转。
int main () {
int x=10, y=20;
std::swap(x,y);
std::vector<int> foo (4,x), bar (6,y);
std::swap(foo,bar);
std::cout << "foo contains:";
for (std::vector<int>::iterator it=foo.begin(); it!=foo.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
- fill(it,it2,value):把数组或容器中的某一段区间赋为某个值。
- sort( 首元素地址(必填),尾元素地址(必填),比较函数(选填) ):实现数组或容器内的排序。
- lower_bound(first,last,value):用来寻找在数组或容器的[first,last)范围内第一个值大于等于value的元素位置,如果是数组,则返回该位置的指针,如果是容器。则返回该位置的迭代器。
- upper_bound(first,last,value):用来寻找在数组或容器的[first,last)范围内第一个值大于value的元素位置,如果是数组,则返回该位置的指针,如果是容器。则返回该位置的迭代器。
- copy: copy(InputIterator first, InputIterator last, OutputIterator result)
copy-backward(以相反的顺序copy)
int myints[]={10,20,30,40,50,60,70};
int copyArray[7];
copy(myints,myints+7,copyArray);
cout<<"copyArray:";
for(int i=0;i<7;i++)
{
cout<<copyArray[i]<<" ";
}
- count:功能类似于find,这个函数使用一对迭代器和一个值做参数,返回这个值出现次数的统计结果
int main () {
int myints[] = {10,20,30,30,20,10,10,20};
int mycount = std::count (myints, myints+8, 10);
std::cout << "10 appears " << mycount << " times.\n";
std::vector<int> myvector (myints, myints+8);
mycount = std::count (myvector.begin(), myvector.end(), 20);
std::cout << "20 appears " << mycount << " times.\n";
return 0;
}

- equal :比较范围内的元素和first2开始的范围的元素是否相等,如果范围内全部匹配,则返回true,否则返回false.
#include <iostream>
#include <algorithm>
#include <vector>
bool mypredicate (int i, int j) {
return (i==j);
}
int main () {
int myints[] = {20,40,60,80,100};
std::vector<int>myvector (myints,myints+5);
if ( std::equal (myvector.begin(), myvector.end(), myints) )
std::cout << "The contents of both sequences are equal.\n";
else
std::cout << "The contents of both sequences differ.\n";
myvector[3]=81;
if ( std::equal (myvector.begin(), myvector.end(), myints, mypredicate) )
std::cout << "The contents of both sequences are equal.\n";
else
std::cout << "The contents of both sequences differ.\n";
return 0;
}
#include <iostream>
#include <algorithm>
#include <vector>
bool myfunction (int i, int j) {
return (i==j);
}
int main () {
int myints[] = {10,20,20,20,30,30,20,20,10};
std::vector<int> myvector (myints,myints+9);
std::vector<int>::iterator it;
it = std::unique (myvector.begin(), myvector.end());
myvector.resize( std::distance(myvector.begin(),it) );
std::unique (myvector.begin(), myvector.end(), myfunction);
std::cout << "myvector contains:";
for (it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
