首页 > 编程语言 >c++_Template

c++_Template

时间:2023-04-18 22:26:53浏览次数:81  
标签:std function 变量 iterator 捕获 c++ Template 函数

c++_Template

std::string

Member functions:

Iterators:

  • begin

    Return iterator to beginning (public member function)

  • end

    Return iterator to end (public member function)

  • rbegin

    Return reverse iterator to reverse beginning (public member function)

  • rend

    Return reverse iterator to reverse end (public member function)

  • cbegin

    Return const_iterator to beginning (public member function)

  • cend

    Return const_iterator to end (public member function)

  • crbegin

    Return const_reverse_iterator to reverse beginning (public member function)

  • crend

    Return const_reverse_iterator to reverse end (public member function)

Vector

upper_bound in C++(>)

upper_bound() is a standard library function in C++ defined in the header . It returns an iterator pointing to the first element in the range [first, last] that is greater than value, or last if no such element is found.

The elements in the range shall already be sorted or at least partitioned with respect to val.

lower_bound in C++(>=)

The lower_bound() method in C++ is used to return an iterator pointing to the first element in the range [first, last) which has a value not less than val. This means that the function returns an iterator pointing to the next smallest number just greater than or equal to that number. If there are multiple values that are equal to val, lower_bound() returns the iterator of the first such value.

The elements in the range shall already be sorted or at least partitioned with respect to val.

// CPP program to illustrate using
// std :: upper_bound with vectors
#include <bits/stdc++.h>

// Driver code
int main()
{
	std::vector<int> v{ 10, 20, 30, 40, 50 };

	// Print vector
	std::cout << "Vector contains :";
	for (int i = 0; i < v.size(); i++)
		std::cout << " " << v[i];
	std::cout << "\n";

	std::vector<int>::iterator upper1, upper2;

	// std :: upper_bound
	upper1 = std::upper_bound(v.begin(), v.end(), 35);
	upper2 = std::upper_bound(v.begin(), v.end(), 45);

	std::cout << "\nupper_bound for element 35 is at position : "
			<< (upper1 - v.begin());
	std::cout << "\nupper_bound for element 45 is at position : "
			<< (upper2 - v.begin());

	return 0;
}
//Vector contains : 10 20 30 40 50
//upper_bound for element 35 is at position : 3
//upper_bound for element 45 is at position : 4

vector.begin()/rbegin()和find_if( , , )

#include <iostream>
#include <algorithm>
#include "vector"
using namespace std;
int main() {
    std::vector<int> v{1,2,3,4,5,6,7,8,9,10};
    for(auto i:v){
        cout<<i;
    }
    cout<<endl;
//    cout<<"v.end():"<<*(v.end()-1);//10
//    cout<<"v.begin():"<<*(v.begin());//1
//    cout<<"v.begin()地址:"<<&v[7];//0x1ee535e1b5c
//    cout<<"v.begin()地址:"<<*&v[7];//8
//
//    cout<<"v.rbegin():"<<*(v.rbegin());//10
//    cout<<"v.rend():"<<*(v.rend()-1);//1
    cout<<*find_if(v.rbegin(),v.rend(),[](auto it){return it==1;});
//    cout<<&find_if(v.rbegin(),v.rend(),[](auto it){return it==1;});
    cout<<*find_if(v.rbegin(),v.rend(),[](auto it){return it==1;}).base();
    
/*擦除从第一个1开始直到最后*/
    v.erase(find_if(v.rbegin(),v.rend(),[](auto it){return it==1;}).base(),
            v.end());
    for(auto i:v){
        cout<<i;
    }
/*最后只输出了1*/
    return 0;
}

Function_1:

vector.begin().rbegin().end().rend()

image-20230305193536566

Function_2:

find_if(InputIterator first, InputIterator last, UnaryPredicate pred)
UnaryPredicate pred(只能接受一个参数|返回Boolean)

UnaryPredicate is a concept defined in the C++ Standard Library that describes a function or a function object that takes a single argument of any type and returns a Boolean value. The argument type can be explicitly specified or deduced by the compiler.

struct IsEven {
  bool operator()(int x) const {
    return x % 2 == 0;
  }
};

// Usage:
std::vector<int> v = {1, 2, 3, 4, 5};
auto it = std::find_if(v.begin(), v.end(), IsEven{});

operator():the operator() allows you to treat an object as if it were a function.
class MyFunction{
    public:
    int operator()(int x){
        retutn std::abs(x);
    }
}
int main()
{
	Myfunction f;
    return f(32);
}
以下是 C++ 中经常使用的一些数学方法:(#include "math.h")
  1. 平方根函数 sqrt():计算给定数的平方根。
  2. 绝对值函数 abs():计算给定数的绝对值。
  3. 幂函数 pow():计算给定数的幂。
  4. 三角函数 sin()、cos()、tan():计算给定角度的正弦、余弦和正切值。
  5. 对数函数 log()、log10():计算给定数的自然对数和以 10 为底的对数。
  6. 最大值函数 max():计算给定一组数中的最大值。
  7. 最小值函数 min():计算给定一组数中的最小值。
  8. 随机数生成函数 rand():生成一个伪随机数。
  9. 数值积分函数 integrate():计算给定函数的数值积分。
  10. 线性代数函数,如解线性方程组、求特征值和特征向量等:这些功能通常需要使用数学库,如 Eigen、GSL 或 Armadillo。
匿名函数/lambda function(可以接受多个参数)

Lambda functions were introduced in C++11 and are a powerful feature that allows developers to write more expressive and readable code. Lambda functions can capture variables from the enclosing scope, have their own parameter list and return type, and can be passed as arguments to other functions or stored in variables.

[capture-list] (parameter-list) -> return-type { function-body }

其中,capture-list 可以是以下三种形式之一:

  1. 按值捕获:使用 = 指定按值捕获变量。

    c++Copy codeint a = 10;
    auto f = [=]() { return a; };
    

    在上述代码中,Lambda 函数 f 按值捕获了变量 a,在函数体内部可以访问变量 a,但不能修改其值。

  2. 按引用捕获:使用 & 指定按引用捕获变量。

    c++Copy codeint b = 20;
    auto g = [&]() { return b; };
    

    在上述代码中,Lambda 函数 g 按引用捕获了变量 b,在函数体内部可以访问并修改变量 b 的值。

  3. 捕获所有变量:使用 &= 指定捕获所有变量。

    c++Copy codeint c = 30;
    auto h = [&](int x) { return x + a + b + c; };
    

    在上述代码中,Lambda 函数 h 捕获了变量 abc,并按引用捕获了变量 x

  4. 捕获列表还可以按照变量的类型来捕获变量,如下所示:

    c++Copy codeint d = 40;
    double e = 3.14;
    auto i = [d, &e]() { return d + e; };
    

​ 在上述代码中,Lambda 函数 i 按值捕获了变量 d,按引用捕获了变量 e,在函数体内部可以访问变量 de,但不能修改变量 d 的值。

  1. 捕获列表还可以为空,此时函数体内部无法访问任何外部变量。例如:

    c++Copy code
    auto j = []() { return 1 + 2; };
    

​ 在上述代码中,Lambda 函数 j 没有捕获任何变量,函数体内部只能访问常量 12

​ 总之,捕获列表可以根据需要来指定要捕获的变量及其捕获方式,可以灵活地控制函数体内部对外部变量的访问权限,提高了代码的灵活性和可读性。

[](auto it)->bool{return it==1;}
//[] is used to difine a lambda function

cin

c复试2

c++

//
// Created by 20998 on 3/6/2023.
//
#include "iostream"
#include "string"
class Solution{
private:
    std::string s;
public:
    void input(){
        std::getline(std::cin,s);//std::getline可以读入空格,回车停止,把std::cin所读内容放到s处;
        std::cin>>s;//读到空格停止,这里会重新读入s
    }
    void Print(){
        std::cout<<s<<std::endl;
    }
};

int main()
{
    Solution so;
    so.input();
    so.Print();
    printf("0X%x0%d0%o",811,811,811);
    return 0;
}
//output:
/*
wo de tian a 
wo de tian a
0X32b081101453
 */

标签:std,function,变量,iterator,捕获,c++,Template,函数
From: https://www.cnblogs.com/LogicHan/p/17331391.html

相关文章

  • C++ 学习 第七天
    今日内容:指针 函数 指针:指针是用于记录一块内存空间地址的符号& 后面跟的是一个变量的时候,代表的是取址符符号*  乘号:需要左右两侧的操作数  指针符:当*的前方跟的是一个数据类型的 时候  解引用符:当*后方跟的是指针的时候 int number=20 int* ptr=......
  • 4.18 c++图形库easyx的基础编程
    头文件#include<graphcis.h>一基础绘图概念1.颜色用三原色表示RGB(红色部分,绿色部分,蓝色部分)每一部分的数值范围(0~255)。基本大写英文单词已对应例如BLUE蓝色2.窗口坐标的默认原点在左上角(0,0)x轴正方向向右,y轴正方向向下。二窗口函数initgraph(intwidth,intheigh......
  • C++入门
       本篇文章与大家分享一些c++的关键字、命名空间及输入输出的相关知识。首先,我们先了解一下c++的关键词,具体如下:C++的关键词c++的关键词,共计64个,部分与c语言相似,这些关键字需不需要背呢?答案是否定。回顾我们学c语言的过程,关键词用着用就记住了,没必要刻意去记忆。对于c++的......
  • C++第三章课后练习题
    编写可以求两个数最大公约数和最小公倍数的函数。1#include<iostream>2#include<cmath>3usingnamespacestd;4intfun1(intx,inty)5{6inttemp;7if(x<y)8{9temp=x;10x=y;11y=temp;12}13......
  • c++打卡第八天
    一、问题描述。   我国古代有一种说话叫三天打鱼两天晒网,如果一个人从1990年1月1日开始,开始三天打鱼两天晒网,问输入一个年月日,此时他是在打鱼还是晒网。二、设计思路。①、我们可以通过计算从输入日期到1990年1月1日总共有多少天,总天数除余周期五,如果结果为1.2.3则此人此......
  • 69、K8S-Helm-template导出独立的yaml文件
    1、将helm项目导出为独立yaml文件-实践1.1、需求有时候,我们需要导出yaml分析yaml编写情况,而不是直接部署到k8s,这个时候,就需要使用template来实现了1.2、开始操作1.2.1、创建存放yaml文件的目录helm_prometheus]#cd/opt/helm_prometheus/&&mkdirprometheus-tplhelm_p......
  • 深度解读C++引用
    什么是引用引用不是新定义一个变量,而是给已存在对象取了一个别名,从语言逻辑角度看,引用不占用内存空间,而与被引用的对象共用同一块内存空间。使用引用时,需要注意以下几点:引用在定义时必须初始化;一个变量可以有多个引用;C++中的引用一旦初始化便不能转移;在语法逻辑角度,引用不占用额外......
  • 0001笔记【并行计算】CUDA在现代C++中如何运用?看这一个就够了
    目录SM(流多处理器)和板块(block)一个板块会被调度到一个SM上,直到执行结束常用函数cudaMalloc在显存上分配内存cudaMallocHost在主存上分配锁页内存cudaMemcpy在主存和显存之间拷贝数据cudaMallocManagerd统一内存优化时间依赖和空间依赖线程太多不行:防止寄存器打翻(registerspill)......
  • c++初阶入门(持续更新)
    1.命名空间目的:解决c语言的缺陷,命名冲突。#include<stdio.h>intrand=0;intmain(){printf("%d",rand);}上面这段程序是可以运行的但是!#include<stdio.h>#include<stdlib.h>intrand=0;intmain(){printf("%d",rand);}那么上面的代码就会报错。因为stdlib.h......
  • 【内附源码和文档】基于C++14异步蒙特卡洛工具函数
    Simple-Monte-Carlo-Tool-Function这是一个使用C++实现的简单的异步蒙特卡洛算法工具函数C++标准:C++14使用autores=MonteCarlo(sample_nums,check_sample_funtion,generate_sample_funtion,…args);doublep=res.get();std::cout<<p<<std::endl;sample_nums:需要生成的样......