C++ 回调函数加载初始化配置和使用心得

实际代码

一开始是通过读取.csv文件完成初始化配置,结果因为数据结构复杂,加载逻辑和读取文件逻辑混在一起越来越乱,后来师傅重构的时候,教了回调函数的用法。很简洁得将回调函数的使用和实现区分出来

#include<iostream>
#include <functional>
#include<vector>
#include<queue>
#include<fstream>
//声明函数数据结构
typedef std::function<void(std::vector<std::string> infoTemp)> TestFunction;
//IO逻辑
void testCallBack(TestFunction f) {
	std::ifstream testInputStream("data/test.csv");
	if (!testInputStream) {
		std::cout << "cannot open the file\n";
		return;
	}
	std::vector<char> buffer(1024, 0);
	std::vector<std::string> infoTemp;
	while (testInputStream) {
		memset(buffer.data(), 0, sizeof(buffer));
		infoTemp.clear();
		testInputStream.getline(buffer.data(), sizeof(buffer));
		int pos = -1;
		std::string line(buffer.data());
		while ((pos = line.find_first_of(",")) != std::string::npos) {
			infoTemp.push_back(line.substr(0, pos));
		
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值