文件目录结构,未编译的目录. ├── CMakeLists.txt ├── config.ini ├── include │ └── ConfigReader.h ├── main.cpp └── src └── ConfigReader.cpp代码实现ConfigReader.h实现#pragma once #include string #include stdexcept #include map #include unordered_map class ConfigReader{ public: //explicit 禁用隐式调用使用时必须显示的调用 explicit ConfigReader(const std::string fileName); // const std::string key 表示不会对key进行修改不会修改传入的参数值 避免拷贝 // const 表示是一个常量成员函数不会修改类的任何成员变量 std::string getString(const std::string key) const; int getInt(const std::string key) const; double getDouble(const std::string key) const; bool getBool(const std::string key) const; private: //存放解析结果、 //map 使用红黑树实现键唯一插入的键值对会按键的升序排序查找、插入、删除速度为O(logn) //unordered_map使用hash表实现键唯一无序平局查找、插入、删除时间复杂度为O(1)最坏o(n)占内存更多维护hash表 //multimap使用红黑树实现可以键重复按键升序排列查找、插入、删除速度为O(logn) std::unordered_mapstd::string,std::string configMap; //去除字符串空白字符 static std::string trim(const std::string str); //解析配置文件 void parseFile(const std::string fileName); };ConfigReader.cpp#include ConfigReader.h #include fstream ConfigReader::ConfigReader(const std::string fileName){ parseFile(fileName); } //把所有非注释的行添加到map结构中 void ConfigReader::parseFile(const std::string fileName){ std::ifstream file(fileName); if(!file.is_open()){ throw std::runtime_error(Failed to open config file:fileName); } std::string line; while(std::getline(file,line)){ //获取第一个#或/的位置 size_t commentPos line.find_first_of(#/); //如果该行有#或者/则认为改行为注释行 if(std::string::npos ! commentPos){ continue; } size_t equalPos line.find() ! std::string::npos ? line.find() : line.find(:); if(std::string::npos equalPos){ continue; } std::string key trim(line.substr(0,equalPos)); std::string value trim(line.substr(equalPos1)); if(!key.empty()){ configMap[key] value; } } } //处理字符串首尾的空格 std::string ConfigReader::trim(const std::string str){ if(str.empty()) return str; auto start str.begin(); while(start ! str.end() std::isspace(*start)){ start; } auto end str.end(); do{ end--; }while(std::distance(start,end)0 std::isspace(*end)); return std::string(start,end1); } std::string ConfigReader::getString(const std::string key) const{ auto it configMap.find(key); if(it configMap.end()){ throw std::out_of_range(config key not found:key); } return it-second; } int ConfigReader::getInt(const std::string key) const{ std::string value getString(key); try{ return std::stoi(value); } catch(const std::exception e){ throw std::runtime_error(Invalid integer value for keykey:value); } } } return it-second; } double ConfigReader::getDouble(const std::string key) const{ std::string value getString(key); try{ return std::stod(value); } catch(const std::exception e){ throw std::runtime_error(Invalid double value for keykey:value); } } bool ConfigReader::getBool(const std::string key) const{ std::string value getString(key); try{ if(value true) return true; else if(value false) return false; } catch(const std::exception e){ throw std::runtime_error(Invalid integer value for keykey:value); } }main.cpp#include iostream #include ConfigReader.h int main() { try { ConfigReader config(config.ini); std::string ip config.getString(server_ip); int port config.getInt(server_port); double timeout config.getDouble(timeout); bool debug config.getBool(debug_mode); std::string log_path config.getString(log_path); std::cout Server IP: ip std::endl; std::cout Server Port: port std::endl; std::cout Timeout: timeout seconds std::endl; std::cout Debug Mode: (debug ? ON : OFF) std::endl; std::cout log_path: log_path std::endl; } catch (const std::exception e) { std::cerr Error: e.what() std::endl; return 1; } return 0; }CMakeLists.txt# 指定构建此项目所需的最低CMake版本为3.0 # 这确保CMake会检查版本兼容性如果系统CMake版本低于3.0会报错 cmake_minimum_required(VERSION 3.0) # 定义项目名称为ConfigReader # 这会设置一些CMake内置变量如PROJECT_NAME并初始化项目的基本配置 project(ConfigReader) # 设置源代码目录路径变量 # ${CMAKE_SOURCE_DIR}是CMake内置变量表示顶级CMakeLists.txt所在目录 # 这里将src子目录路径赋给SOURCE_DIR变量 set(SOURCE_DIR ${CMAKE_SOURCE_DIR}/src) # 使用file(GLOB)命令收集所有.cpp源文件 # 这会匹配SOURCE_DIR目录下所有.cpp文件并将文件列表存入SOURCE_FILES变量 # 注意GLOB会在配置时立即执行新增文件需要重新运行CMake file(GLOB SOURCE_FILES ${SOURCE_DIR}/*.cpp) # 设置包含目录路径变量 # 将include子目录路径赋给INCLUDE_DIR变量 set(INCLUDE_DIR ${CMAKE_SOURCE_DIR}/include) # 添加包含目录到编译器搜索路径 # 这样编译器就能找到INCLUDE_DIR目录下的头文件 include_directories(${INCLUDE_DIR}) # 设置编译器选项 # -g: 生成调试信息 # -stdc11: 使用C11标准 # -O2: 优化级别2 # -Wall: 启用所有警告 add_compile_options(-g -stdc11 -O2 -Wall) # 设置构建类型为Debug模式 # Debug模式会包含调试信息且不做优化方便调试 # 可选值通常有: Debug, Release, RelWithDebInfo, MinSizeRel set(CMAKE_BUILD_TYPE Debug) # 定义要构建的可执行文件main # 包含main.cpp和SOURCE_FILES中收集的所有源文件 add_executable(main main.cpp ${SOURCE_FILES})config.ini# 这是一个示例配置文件 server_ip 192.168.1.100 server_port 8080 timeout 5.5 debug_mode true