cpp primer笔记070-算法函数

2023-10-07 07:20
文章标签 算法 函数 笔记 cpp primer 070

本文主要是介绍cpp primer笔记070-算法函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  1. accumulate的第三个参数的类型决定了函数中使用哪个加法运算符以及返回值的类型,如果返回值是自定义类型,需要使用accumlate,则需要重载运算符,该接口的第三个参数返回的是一个需要处理的数据类型的一个变量。

    std::vector<std::string> v{ "wer","Sdf","sdf" };
    std::string sum = std::accumulate(v.begin(), v.end(), std::string(""));
    std::cout << sum << std::endl;
    
    werSdfsdf
    
  2. fill接受一对迭代器表示一个范围,还接受一个值作为第三个参数,将该范围的所有元素的值初始化为该参数。如果使用fill_n,很可能导致数组越界而产生异常。

    std::vector<std::string> v{ "wer","Sdf","sdf" };
    std::fill(v.begin(), v.end(), "wer");
    for (auto x : v)
    {std::cout << x << " ";
    }
    std::fill_n(v.begin(), v.size(), "2");
    for (auto x : v)
    {std::cout << x << " ";
    }
    
    wer wer wer 2 2 2
    
  3. lambda表达式一般构成部分为一下四种,(注意:lambda表达式不能有默认参数,捕获列表只能用于局部非静态变量,对于局部静态变量和全局变量可以直接使用,如果采取值捕获的方式,则变量可以被拷贝,并且被捕获的变量的值是在lambda创建时拷贝的,而不是调用的时候。若采取引用类型捕获的话,如果引用的变量在lambda表达式被调用的时候已经被销毁,则会产生异常。如果想要改变被捕获的值而不是引用变量,则lambda-specifiers可以是mutable):

    • [captures] &lttparams&gt (params) lambda-specifiers { body }
    • [captures] (params) trailing-return-type { body }
    • [captures] (params) { body }
    • [captures] lambda-specifiers { body }
    • captures:捕获列表,lambda可以把上下文变量以值或者引用的方式捕获,在body中直接使用。
    • tparams模板参数列表:让lambda可以像模板函数一样被调用。
    • params参数列表,相当于函数的产生列表,在C++14之后允许使用auto左右参数类型,可以省略。
    • lambda-specifiers lambda说明符,一些可选的参数,比较常用的的参数包括mutable和exception
    • trailing-return-type返回值类型,一般可以省略掉,由编译器来推导。
    • body函数体,函数的具体逻辑。
      [captures]可以使用的几种常用捕获方式:
    • []什么也不捕获,lambda无法使用所在函数体内任何变量。
    • [=]按值的方式捕获所有变量。
    • [&]按引用的方式捕获所有变量。
    • [=,&a,&b]除了变量a和b按引用捕获,其他按值捕获。(这里如果a不加&号会报错)
    • [&,a,b,c]除了变量a,b,c按值捕获,其他按引用捕获
    • [a,&b,&c]以值的方式捕获a,以引用的方式捕获b和c
    • [this]在成员函数中,可以直接捕获this指针,其实在成员函数中,[=]和[&]也会捕获this指针。
    #include <iostream>
    #include <fstream>
    #include <array>
    #include <vector>
    #include <string>
    #include <exception>
    #include <stack>
    #include <deque>
    #include <numeric>
    class Vector3
    {
    private:double x = 0;double y = 0;double z = 0;
    public:Vector3(double a, double b, double c) :x(a), y(b), z(c) {};const Vector3 operator+(const Vector3& vec) const{return [this](const Vector3& vec)->Vector3{ return { double(this->x + vec.x),double(this->y + vec.y),double(this->z + vec.z) }; }(vec);}Vector3& operator=(const Vector3& vec){auto ptrFunc = [this](const Vector3& vec) ->Vector3& {this->x = vec.x;this->y = vec.y;this->z = vec.z;return *this;};return ptrFunc(vec);}friend void Swap(Vector3& vec1, Vector3& vec2){[&vec1](Vector3& vec2){Vector3 tempVec = vec1;vec1 = vec2;vec2 = tempVec;}(vec2);}friend std::ostream& operator<<(std::ostream& os, const Vector3 vec){os << "Vector3 (" << vec.x << ", " << vec.y << ", " << vec.z << ")";return os;}
    };
    int main()
    {Vector3 vec1{ 1.0, 2.0, 3.0 }, vec2{ 4.0, 5.0, 6.0 };std::cout << vec1 << " " << vec2 << std::endl;std::cout << vec1 + vec2 << std::endl;Swap(vec1, vec2);std::cout << vec1 << " " << vec2 << std::endl;return 0;
    }
    
    Vector3 (1, 2, 3) Vector3 (4, 5, 6)
    Vector3 (5, 7, 9)
    Vector3 (4, 5, 6) Vector3 (1, 2, 3)
    
  4. bind函数的第一个参数是一个函数指针,后面的参数是这个函数指针中的参数列表,返回值为一个函数指针,其中_1,_2代表bind中第二个参数,第三个参数。

    using namespace std::placeholders;
    int main()
    {int a = 10, b = 20;auto max = std::bind([](int a, int b, int c) {return a > b ? a : b; }, a, b, _1);std::cout << max(a, b) << std::endl;std::cout << std::bind(max, a, b)(a, b) << std::endl;return 0;
    }
    
  5. iterator头文件定义了额外的一下几种迭代器:

    • 插入迭代器:这些迭代器被绑定到一个容器上,可以用来向容器插入元素。
    • 流迭代器:这些迭代器被绑定到输入或者输出流上,可以用来遍历所有关联的IO流。
    • 反向迭代器:这些迭代器向后移动,而不是向前移动,除了forward_list之外的标准库容器都有反向迭代器。
    • 移动迭代器:这些专用的迭代器不是拷贝其中的元素,而是移动他们。
  6. 插入迭代器包括back_inserter,front_inserter和inserter

    #include <iostream>
    #include <fstream>
    #include <array>
    #include <vector>
    #include <string>
    #include <exception>
    #include <algorithm>
    #include <stack>
    #include <deque>
    #include <numeric>
    #include <functional>
    int main()
    {std::deque<int> deq1, deq2;auto it = std::back_inserter(deq1);*it = 42;*it = 10;//vec.resize(10);//std::fill_n(vec.begin(), 10, 0);//下面的一行相当于上面的两行,由于插入迭代器自增无效,解引用,//所以fill_n不断让迭代器自增的语句无效,但是赋值语句有效std::fill_n(std::back_inserter(deq1), 10, 0);std::for_each(deq1.cbegin(), deq1.cend(), [](int val) {std::cout << val << " "; });deq1.clear();std::cout << std::endl;for (int i = 0; i < 10; ++i) *it = i;std::copy(deq1.cbegin(), deq1.cend(), std::front_inserter(deq2));std::for_each(deq2.cbegin(), deq2.cend(), [](int val) {std::cout << val << " "; });deq2.clear();std::cout << std::endl;std::copy(deq1.cbegin(), deq1.cend(), std::inserter(deq2, deq2.begin()));std::for_each(deq2.cbegin(), deq2.cend(), [](int val) {std::cout << val << " "; });return 0;
    }
    //给back_inserter赋值相当于push_back,给inserter相当于push_front
    //给front_inserter赋值相当于push_front
    
    42 10 0 0 0 0 0 0 0 0 0 0
    9 8 7 6 5 4 3 2 1 0
    0 1 2 3 4 5 6 7 8 9
    

    ![[Pasted image 20230922214426.png]]

  7. 流迭代器包括istream_iterator和ostream_iterator
    ![[Pasted image 20230922231536.png]]
    ![[Pasted image 20230923000228.png]]

		#include <iostream>#include <fstream>#include <array>#include <vector>#include <string>#include <exception>#include <algorithm>#include <stack>#include <deque>#include <numeric>#include <functional>int main(){std::vector<int> vec1;std::vector<char> vec2;std::istream_iterator<int> ini_it(std::cin);std::istream_iterator<int> int_eof;//上一个语句声明了开头//,下一个语句只要是类型相同就会声明一个结尾std::ifstream in("sdfksjdfk");//从sdfksjdfk文件读取字符串,而不是从这个字符串中读取std::istream_iterator<char> str_begin(in);std::istream_iterator<char> str_end;//std::cout << std::accumulate(ini_it, int_eof, 0) << std::endl;//上面的语句会使得ini_it自增不断从输入流读出下一个字符while (ini_it!=int_eof){auto it = std::back_inserter(vec1);*it = *ini_it++;}std::for_each(vec1.begin(), vec1.end(), [](int val) {std::cout << val << " "; });std::cout.put(10);while (str_begin != str_end){vec2.push_back(*str_begin++);}for (auto ch : vec2){std::cout << ch;}std::cout << std::endl;return 0;}
	123235234 534^Z123 235 234 534
	#include <iostream>#include <fstream>#include <array>#include <vector>#include <string>#include <exception>#include <algorithm>#include <stack>#include <deque>#include <numeric>#include <functional>int main(){std::vector<int> vec{ 1, 2, 34, 45, 5, 32134, 234 };std::ostream_iterator<int> out(std::cout, ",");for (auto x : vec)out = x;std::cout << std::endl;std::copy(vec.cbegin(), vec.cend(), out);std::cout << std::endl;return 0;}
1,2,34,45,5,32134,234,
1,2,34,45,5,32134,234,
  1. 反向迭代器包括rbegin,rend,crbegin,crend
    #include <iostream>
    #include <fstream>
    #include <array>
    #include <vector>
    #include <string>
    #include <exception>
    #include <algorithm>
    #include <stack>
    #include <deque>
    #include <numeric>
    #include <functional>
    int main()
    {std::vector<int> vec{ 1, 2, 3, 5, 6, 7, 8, 9 };std::ostream_iterator<int> out(std::cout, ",");for (auto r_iter = vec.crbegin(); r_iter != vec.crend(); ++r_iter){out = *r_iter;}std::cout.put(10).put(10);std::sort(vec.rbegin(), vec.rend());for_each(vec.cbegin(), vec.cend(), [&out](int val) { out = val; });return 0;
    }
    
    9,8,7,6,5,3,2,1,9,8,7,6,5,3,2,1,
    
  2. list和forward_list成员函数版本的算法
    ![[Pasted image 20230923092947.png]]

![[Pasted image 20230923093345.png]]

这篇关于cpp primer笔记070-算法函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/156895

相关文章

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

Python函数作用域示例详解

《Python函数作用域示例详解》本文介绍了Python中的LEGB作用域规则,详细解析了变量查找的四个层级,通过具体代码示例,展示了各层级的变量访问规则和特性,对python函数作用域相关知识感兴趣... 目录一、LEGB 规则二、作用域实例2.1 局部作用域(Local)2.2 闭包作用域(Enclos

Java中的雪花算法Snowflake解析与实践技巧

《Java中的雪花算法Snowflake解析与实践技巧》本文解析了雪花算法的原理、Java实现及生产实践,涵盖ID结构、位运算技巧、时钟回拨处理、WorkerId分配等关键点,并探讨了百度UidGen... 目录一、雪花算法核心原理1.1 算法起源1.2 ID结构详解1.3 核心特性二、Java实现解析2.

MySQL count()聚合函数详解

《MySQLcount()聚合函数详解》MySQL中的COUNT()函数,它是SQL中最常用的聚合函数之一,用于计算表中符合特定条件的行数,本文给大家介绍MySQLcount()聚合函数,感兴趣的朋... 目录核心功能语法形式重要特性与行为如何选择使用哪种形式?总结深入剖析一下 mysql 中的 COUNT

MySQL 中 ROW_NUMBER() 函数最佳实践

《MySQL中ROW_NUMBER()函数最佳实践》MySQL中ROW_NUMBER()函数,作为窗口函数为每行分配唯一连续序号,区别于RANK()和DENSE_RANK(),特别适合分页、去重... 目录mysql 中 ROW_NUMBER() 函数详解一、基础语法二、核心特点三、典型应用场景1. 数据分

MySQL数据库的内嵌函数和联合查询实例代码

《MySQL数据库的内嵌函数和联合查询实例代码》联合查询是一种将多个查询结果组合在一起的方法,通常使用UNION、UNIONALL、INTERSECT和EXCEPT关键字,下面:本文主要介绍MyS... 目录一.数据库的内嵌函数1.1聚合函数COUNT([DISTINCT] expr)SUM([DISTIN

Python get()函数用法案例详解

《Pythonget()函数用法案例详解》在Python中,get()是字典(dict)类型的内置方法,用于安全地获取字典中指定键对应的值,它的核心作用是避免因访问不存在的键而引发KeyError错... 目录简介基本语法一、用法二、案例:安全访问未知键三、案例:配置参数默认值简介python是一种高级编

python 常见数学公式函数使用详解(最新推荐)

《python常见数学公式函数使用详解(最新推荐)》文章介绍了Python的数学计算工具,涵盖内置函数、math/cmath标准库及numpy/scipy/sympy第三方库,支持从基础算术到复杂数... 目录python 数学公式与函数大全1. 基本数学运算1.1 算术运算1.2 分数与小数2. 数学函数

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

C++ 函数 strftime 和时间格式示例详解

《C++函数strftime和时间格式示例详解》strftime是C/C++标准库中用于格式化日期和时间的函数,定义在ctime头文件中,它将tm结构体中的时间信息转换为指定格式的字符串,是处理... 目录C++ 函数 strftipythonme 详解一、函数原型二、功能描述三、格式字符串说明四、返回值五