C++ study notes three-the usage of some generic functions

Today, I simply learned the functions of a few generic algorithms, and I will make a record here.
According to the type of return value, it is divided into the following categories. The
return value is of int type:

  1. accumulate(): Add the element values ​​in the container, plus the initial value specified by the third parameter.
  2. adjacent_difference(): Keep the first element in the container unchanged, and subtract the first element from the second element (default operation). You can also perform other operations by adding special commands: multiplication, addition. The first two parameters of the function are passed into the starting position of the container, and the third parameter is the beginning of the container (a.begin()), and the calculated value is passed into the third container.
    3) Adjacent_find(), search for the first group of adjacent and repeated elements, it will return an iterator, you can also change the parameters, greater than or less than can be
    4) copy () copy the elements of the value in the first container one by one Into the second container. The first parameter is the start of the container, and the second is the end of the container. The third is the beginning of the target container.
    5) copy_backward() The result of the reverse copy operation is the same as the previous copy operation but the order of operations is the same.
    6) count() Count returns the number of equals to the specified value in the container
    count (svec.begin(),svec.end(),value); the
    return value is bool type
    1) equal to judge whether it is equal
    2) fill() will Each element in the container is set to a specific value one by one
    3) fill_n() sets the elements
    in the container to a specific value one by one 4) binary_search Binary search searches for a specific value in the container

The following is a program written by myself using the above several generic function algorithms,

# 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;
			}

}

Guess you like

Origin blog.csdn.net/qq_41803340/article/details/107212568