首页 > 编程语言 >【C++】 bind examples

【C++】 bind examples

时间:2023-09-05 11:33:09浏览次数:37  
标签:std bind C++ print examples Test include numbers

Simple Example

#include <algorithm>
#include <vector>
#include <iostream>

void print(std::string prefix, int number)
{
    std::cout << prefix << " - " << number << std::endl;
}

int main(int argc, char* argv[])
{
    std::vector<int> numbers;
    for (int i = 0; i < 100; ++i)
    {
        numbers.push_back(i);
    }
    std::for_each(numbers.begin(), numbers.end(), std::bind(print, "main()", std::placeholders::_1));
    return 0;
}

Bind to Class Member function

#include <algorithm>
#include <vector>
#include <iostream>

class Test{
public:
    Test(){
        age = 100;
    }
    void print(int task_id){
        std::cout << task_id << " - " << age << std::endl;
    }
private:
    int age;
};

int main(int argc, char* argv[])
{
    std::vector<int> numbers = {1, 2, 3};
    Test test;
    std::for_each(numbers.begin(), numbers.end(), std::bind(&Test::print, &test, std::placeholders::_1));
    return 0;
}

标签:std,bind,C++,print,examples,Test,include,numbers
From: https://www.cnblogs.com/dilex/p/17414288.html

相关文章

  • C++系列十:日常学习-类型转换
    目录介绍:案例:介绍:C++提供了几种类型转换(typeconversion)的方式,包括静态转换(static_cast)、动态转换(dynamic_cast)、常量转换(const_cast)和重新解释转换(reinterpret_cast)。static_cast:这是最通用的转换,可以用于非多态类型之间的转换,也可以用于多态类型到非多态类型的转换。没有运......
  • C++系列十:日常学习-非多态类型和多态类型
    目录介绍:案例:介绍:非多态类型和多态类型是C++中的两种类型,它们的主要区别在于是否包含虚函数和是否能进行动态绑定。非多态类型是指没有虚函数的类型。这种类型在编译时进行类型检查,因此不能在运行时改变其静态类型。对于非多态类型,编译器在编译时就已经确定了其所有信息,包括成......
  • 《C++》11新特性--多线程
    thread创建线程,不会阻塞主线程thread成员函数voidthread::join();阻塞线程,当前线程执行完毕才会往下执行boolthread::joinble();线程是否可以连接,返回voidthread::detach();分离主线程和子线程的关联voidprintI(){for(size_ti=0;i<100;i++){std::c......
  • C++将派生类赋值给基类
    在C/C++中经常会发生数据类型的转换,例如将int类型的数据赋值给float类型的变量时,编译器会先把int类型的数据转换为float类型再赋值;反过来,float类型的数据在经过类型转换后也可以赋值给int类型的变量。数据类型转换的前提是,编译器知道如何对数据进行取舍。例如:inta=......
  • 二、c++容器学习vector
    1、Vector介绍1.1vector基本概念vector与普通数组区别:不同区别是数组是静态空间,而vector可以是动态扩展。动态扩展:并不是在原空间之后续接新空间,而是找更大的内存空间,然后将原始数据拷贝新空间,释放原空间。vector在C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构......
  • [C++] std::optional与RVO:最高效的std::optional实践与探究
    返回值优化RVO在cppreference中,是这么介绍RVO的Inareturnstatement,whentheoperandisthenameofanon-volatileobjectwithautomaticstorageduration,whichisn'tafunctionparameteroracatchclauseparameter,andwhichisofthesameclasstype(igno......
  • 线程池至少需要线程数——23秋招招行网络科技第一批技术测评_后端(c++)
    题目:有n个计划,每个计划有开始,结束时间,求线程池最少需要多少个线程?例:输入:2,[[1,2],[3,4]],输出:1输入:2, [[1,3],[2,4]],输出:2 思路:贪心算法PS:其实我不是很理解下面代码第11行,分别对a,b数组排序1#include<bits/stdc++.h>2usingnamespacestd;34intma......
  • 解释C++中类的不同成员类型和成员列表的含义--GPT
    C++定义的class的PublicMemberFunctions|StaticPublicMemberFunctions|PublicAttributes|StaticPublicAttributes|StaticProtectedAttributes|Listofallmembers都是什么意思?GPT:在C++中,一个类(class)可以定义多种类型的成员,这些成员包括函数(成员函数)和变......
  • C++语言学习08
    一、智能指针常规指针的缺点:当一个常规指针离开了作用域时,只有该指针变量本身占用的内存空间(4/8字节)会被释放,而它指向的内存空间不会自动释放,当free\delete\delete[]语句忘记执行或者无法执行,形成内存泄露(如何定位哦内存泄露、如何预防内存泄露)智能指针的优点:智能指......
  • C++11——3.21-3.22 move,forward
    ★★★原文链接★★★:https://subingwen.cn/cpp/move-forward/3.21move资源的转移3.22forward完美转发3.21move资源的转移move方法可以将左值转换为右值使用这个函数并不能移动任何东西,它将一个对象的所有权从这个对象转移到另一个对象,只是转移,没有内存拷贝。move语......