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的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指

Python正则表达式语法及re模块中的常用函数详解

《Python正则表达式语法及re模块中的常用函数详解》这篇文章主要给大家介绍了关于Python正则表达式语法及re模块中常用函数的相关资料,正则表达式是一种强大的字符串处理工具,可以用于匹配、切分、... 目录概念、作用和步骤语法re模块中的常用函数总结 概念、作用和步骤概念: 本身也是一个字符串,其中

shell编程之函数与数组的使用详解

《shell编程之函数与数组的使用详解》:本文主要介绍shell编程之函数与数组的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录shell函数函数的用法俩个数求和系统资源监控并报警函数函数变量的作用范围函数的参数递归函数shell数组获取数组的长度读取某下的

openCV中KNN算法的实现

《openCV中KNN算法的实现》KNN算法是一种简单且常用的分类算法,本文主要介绍了openCV中KNN算法的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录KNN算法流程使用OpenCV实现KNNOpenCV 是一个开源的跨平台计算机视觉库,它提供了各

MySQL高级查询之JOIN、子查询、窗口函数实际案例

《MySQL高级查询之JOIN、子查询、窗口函数实际案例》:本文主要介绍MySQL高级查询之JOIN、子查询、窗口函数实际案例的相关资料,JOIN用于多表关联查询,子查询用于数据筛选和过滤,窗口函... 目录前言1. JOIN(连接查询)1.1 内连接(INNER JOIN)1.2 左连接(LEFT JOI

MySQL中FIND_IN_SET函数与INSTR函数用法解析

《MySQL中FIND_IN_SET函数与INSTR函数用法解析》:本文主要介绍MySQL中FIND_IN_SET函数与INSTR函数用法解析,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一... 目录一、功能定义与语法1、FIND_IN_SET函数2、INSTR函数二、本质区别对比三、实际场景案例分

利用Python快速搭建Markdown笔记发布系统

《利用Python快速搭建Markdown笔记发布系统》这篇文章主要为大家详细介绍了使用Python生态的成熟工具,在30分钟内搭建一个支持Markdown渲染、分类标签、全文搜索的私有化知识发布系统... 目录引言:为什么要自建知识博客一、技术选型:极简主义开发栈二、系统架构设计三、核心代码实现(分步解析

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

C语言函数递归实际应用举例详解

《C语言函数递归实际应用举例详解》程序调用自身的编程技巧称为递归,递归做为一种算法在程序设计语言中广泛应用,:本文主要介绍C语言函数递归实际应用举例的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录前言一、递归的概念与思想二、递归的限制条件 三、递归的实际应用举例(一)求 n 的阶乘(二)顺序打印

springboot+dubbo实现时间轮算法

《springboot+dubbo实现时间轮算法》时间轮是一种高效利用线程资源进行批量化调度的算法,本文主要介绍了springboot+dubbo实现时间轮算法,文中通过示例代码介绍的非常详细,对大家... 目录前言一、参数说明二、具体实现1、HashedwheelTimer2、createWheel3、n