STL高級語法

1.STL智能函數指針

#include <functional>

void test1(const std::string& s) {
    std::cout << "test1:" << s << '\n';
}

class ATestClass{
public:
    void test2(const std::string& s) {
        std::cout << "test2:" << s << '\n';
    }
};

typedef std::function<void(const std::string&)> FunPtr;

FunPtr  f = std::bind(&ATestClass::test2, &instance,std::placeholders::_1);
/*
    或者可以這樣表示
*/
auto  fun = std::bind(&ATestClass::test2, &instance,std::placeholders::_1);

f("4396");
fun("2800");
f = std::bind(&test1, std::placeholders::_1);
f(str);

2.容器階乘

#include <iostream>
#include <vector>
#include <string>
#include <numeric>


int main()
{
	std::vector<int> ivec = { 8, 1, 1, 1 };

	std::vector<std::string> colors = { "red","green","blue" };
	/// 自定義計算方法
	auto calc = [](auto op1, auto op2) 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章