C++11 std::regex_搜索和他的std::smatch作为模板中的返回值

C++11 std::regex_搜索和他的std::smatch作为模板中的返回值,c++11,C++11,看一下我的代码,告诉我如何正确地执行此操作。首先,这是我的配置管理器类的一部分。一般来说,我只想将read函数用于三种主要类型的数据int、float、string(将来可能是bool) 模板 T读取(常量标准::字符串和名称) { T回顾; 字符串缓冲区; while(std::getline(m_hFile,buffer)) { if(strstrstr(buffer.c_str(),name.c_str())) { std::smatch匹配; 常量std:

看一下我的代码,告诉我如何正确地执行此操作。首先,这是我的配置管理器类的一部分。一般来说,我只想将read函数用于三种主要类型的数据int、float、string(将来可能是bool)

模板
T读取(常量标准::字符串和名称)
{
T回顾;
字符串缓冲区;
while(std::getline(m_hFile,buffer))
{
if(strstrstr(buffer.c_str(),name.c_str()))
{                
std::smatch匹配;
常量std::正则表达式正则表达式(“=\”(.*)\”;
标准::正则表达式搜索(缓冲区、匹配、正则表达式模式);
retVal=????
打破
}
}
返回返回;
}

我如何施展这个std::smatch来让它工作?我应该在那里做什么?我真的不知道。欢迎您提供任何提示或代码片段。

我还没有测试过这一点,但我想您已经明白了:

template<typename T>
T Read(const std::string& name) {
  T retVal;
  std::string buffer;

  while(std::getline(m_hFile, buffer)) {
    if(strstr(buffer.c_str(), name.c_str())) {
      std::smatch match;
      const std::regex regex_patern("=\"(.*)\"");
      std::regex_search(buffer, match, regex_patern);
      if (!match.empty()) {
    std::istreamstream ss(match[0]);
    ss >> retVal;
      }
      break;
    }
  }
  return retVal;
}
模板
T读取(常量标准::字符串和名称){
T回顾;
字符串缓冲区;
while(std::getline(m_hFile,buffer)){
if(strstrstr(buffer.c_str(),name.c_str())){
std::smatch匹配;
常量std::正则表达式正则表达式(“=\”(.*)\”;
标准::正则表达式搜索(缓冲区、匹配、正则表达式模式);
如果(!match.empty()){
std::iStream ss(匹配[0]);
ss>>检索;
}
打破
}
}
返回返回;
}
我通常用于读取配置文件:

// json example
// {
//   'foo': 'hello, world',
//   'bar': 1
// }  

boost::property_tree::ptree config;
boost::property_tree::read_json("path", config);
auto hello_world = config.get<std::string>("foo");
auto one = config.get<int>("bar");
//json示例
// {
//‘福’:‘你好,世界’,
//酒吧:1
// }  
boost::property_tree::ptree-config;
boost::property_tree::read_json(“path”,config);
auto hello_world=config.get(“foo”);
auto one=config.get(“bar”);

我还没有测试过这一点,但我想你已经明白了:

template<typename T>
T Read(const std::string& name) {
  T retVal;
  std::string buffer;

  while(std::getline(m_hFile, buffer)) {
    if(strstr(buffer.c_str(), name.c_str())) {
      std::smatch match;
      const std::regex regex_patern("=\"(.*)\"");
      std::regex_search(buffer, match, regex_patern);
      if (!match.empty()) {
    std::istreamstream ss(match[0]);
    ss >> retVal;
      }
      break;
    }
  }
  return retVal;
}
模板
T读取(常量标准::字符串和名称){
T回顾;
字符串缓冲区;
while(std::getline(m_hFile,buffer)){
if(strstrstr(buffer.c_str(),name.c_str())){
std::smatch匹配;
常量std::正则表达式正则表达式(“=\”(.*)\”;
标准::正则表达式搜索(缓冲区、匹配、正则表达式模式);
如果(!match.empty()){
std::iStream ss(匹配[0]);
ss>>检索;
}
打破
}
}
返回返回;
}
我通常用于读取配置文件:

// json example
// {
//   'foo': 'hello, world',
//   'bar': 1
// }  

boost::property_tree::ptree config;
boost::property_tree::read_json("path", config);
auto hello_world = config.get<std::string>("foo");
auto one = config.get<int>("bar");
//json示例
// {
//‘福’:‘你好,世界’,
//酒吧:1
// }  
boost::property_tree::ptree-config;
boost::property_tree::read_json(“path”,config);
auto hello_world=config.get(“foo”);
auto one=config.get(“bar”);
istringstream istr(匹配结果);istr>>检索另见istringstream istr(匹配结果);istr>>检索另请参见