今天简单的学习了几个泛型算法的函数,特地在此做一个记录。
按照返回值的类型分为以下几类
返回值为 int 型 :
- accumulate() :将容器内的元素值相加,再加上第三个参数指定的初值.
- adjacent_difference() : 将容器内的第一个元素保持不变,第二个元素减去第一个元素(默认操作),也可以通过添加特殊命令执行其他运算:乘法,加法。 函数前两个参数传入容器的起始位置,第三个参数是容器的开始(a.begin()),将计算好的值传入到第三个容器内。
3)adjacent_find(),搜索第一组相邻且重复的元素,会返回一个 iterator,也可以改参数,大于,小于都可以
4)copy() 将第一个容器内的值的元素一一复制到第二个容器内。第一个参数为容器的起点,第二个为容器的结尾。第三个为目标容器的的开始
5) copy_backward() 逆向复制与上一个复制操作的结果相同但是操作顺序相同。
6)count() 计数 返回容器中与指定值相等的个数
count (svec.begin(),svec.end(),value);
返回值为bool型
1)equal 判断是否相等
2)fill() 将容器内的每一个元素一一设定为某特定值
3)fill_n()将容器内的元素一一设定为某特定值
4)binary_search 二元搜索在容器内搜索特定值
以下为自己写的程序分别使用以上的几个泛型函数算法,
# include <iostream>
#include <vector>
# include <list>
# include <numeric>
# include <string>
# include <algorithm>
using namespace std;
int main()
{
//所有容器的共通操作 begin() ,end(),insert,erase,size (),empty(),equality,clear()
int ia[8] = { 1, 2, 3, 10, 15, 21, 28, 36 };
vector <int> ivec(ia, ia + 8);
list <int> ilist(ia, ia + 8);
string sa[10] = { "the ", "light ", "untonsured ", "hair ", "grained ", "and ", "hued ", "like ", "pale ", "oak " };
vector <string> svec(sa, sa + 10);
list<string> slist(sa, sa + 10);
vector<int>iresult1(100);//vector 尽量在初始化时指定大小或者赋予初值,不然容易内存泄漏
// 元素累加操作 accumulate 将元素相加在加上第三个参数指定的初始值e
int iresult = accumulate(ia,ia+3,1);
cout << "the result is " << iresult << endl;
//测试vector 容器 list 以及字符串类型
iresult = accumulate(ivec.begin()+2, ivec.end()-1 , 0);//begin 可在后面添加+1 +2等从而改变元素的位置
cout << "the change result is " << iresult << endl;
iresult = accumulate(ilist.begin(), ilist.end(), 1);
cout << "the list result is " << iresult << endl;
string sum = accumulate(svec.begin(), svec.end(), string(" rui"));
cout << "the string reusult is " << sum << endl;
// 相邻元素的差额 adjacent_difference 具体的计算方式为 第一个元素保持不变,第二个元素减去第一个元素计算差额,该函数支持引进其他的运算取代默认操作符
adjacent_difference(ivec.begin(), ivec.end(), iresult1.begin());
cout << "the addjacent_difference - ressult is " << iresult1[1] << endl;
adjacent_difference(ivec.begin(), ivec.end(), iresult1.begin(), multiplies<int>());
cout << "the addjecent_difference * result is " << iresult1[2] << endl;
adjacent_difference(ivec.begin(), ivec.end(), iresult1.begin(),plus<int>());
cout << "the addjecent_difference + result is " << iresult1[2] << endl;
// adjacent_find() 搜索相邻的重复元素 默认情况下搜索第一组值相邻且重复的元素
//用迭代器接收返回的值
//如果判断两个元素相等的时候,需引入头文件 include <functional> 第一个元素大于第二个元素的时候 greater<>(),第一个元素小于第二个元素的时候less <>()
vector<int> ::iterator piter;
piter = adjacent_find(ivec.begin(), ivec.end());
cout << *adjacent_find(ia, ia + 8) << endl;
// 二元搜索
//binary_search() 二元搜索 默认处理的对象已经从小到大排好序。以二分法查找所想查找的值,有则返回true 如果没有则返回false
if (binary_search(ivec.begin(), ivec.begin() + 5, 3))
cout << "successful" << endl;
else
cout << "your input is wrong" << endl;
// copy() 复制 将一个容器的元素复制到另一个容器
vector <int> copy1(ivec.size());
copy(ivec.begin(), ivec.end(), copy1.begin());
for (int i = 0; i != copy1.size(); i++)
{
cout << "copy" << "[" << i << "]" << "is "<<copy1[i] << endl;
}
// copy_backward 逆向复制 操作与复制几乎一样,但是复制系统逆向行之,也就是从复制的位置不一样。数据顺序不变 本例找其中一个元素为例
vector <string> copy2(svec.size());
copy_backward(svec.begin(), svec.end(), copy2.end());
cout << "the copy_backward result is " << copy2[1] << endl;
//count 计数 返回容器中与指定值相等的元素个数
cout << "the value is "<< count(ivec.begin(), ivec.end(), 3);
cout << "the string equal is " << count(svec.begin(), svec.end(), "light ") << endl;
// equal 判断相等与否 如果相等 返回TRUE
int a1[] = { 1,1,2,3,5 };
int a2[] = { 1,1,2,3,5 };
if (equal(a1, a1 + 5, a2))
cout << "equal" << endl;
else
cout << "not equal" << endl;
//fill() 将容器内的元素一一改为设定值
fill(copy1.begin(), copy1.end(), 5);
for (int i = 0; i != copy1.size(); i++)
{
cout << copy1[i] << endl;
}
//fill_n() 改填元素值
fill_n(copy1.begin(), 3, 10);
for (int i = 0; i != copy1.size(); i++)
{
cout << "the fill_n result is "<< copy1[i] << endl;
}
}