zoukankan      html  css  js  c++  java
  • c++ string split function

    #include <string>
    #include <vector>
    #include <regex>
    
    struct SplitListItem
    {
    	std::string key;
    	std::string value;
    };
    
    std::vector<SplitListItem> splitString(std::string origenString, std::regex pattern)
    {
    	std::smatch result;
    	std::string::const_iterator iterStart = origenString.begin();
    	std::string::const_iterator iterEnd = origenString.end();
    
    
    	std::vector<std::string> splitList = {};
    	std::vector<std::string> splitKeys = {};
    	std::vector<SplitListItem> resultSplitList = {};
    
    	while (regex_search(iterStart, iterEnd, result, pattern))
    	{
    		splitList.emplace_back(iterStart, result[0].first);
    		splitKeys.push_back(result[0].str());
    		iterStart = result[0].second;
    	}
    	splitList.emplace_back(iterStart, iterEnd);
    
    
    	for (size_t i = 0; i < splitList.size(); i++)
    	{
    		resultSplitList.push_back(SplitListItem{ i > 0 ? splitKeys[i - 1] : "",  splitList[i] });
    	}
    	return resultSplitList;
    }
    
    
    std::vector<SplitListItem>  r = splitString("game.exe+009E820C+4+8-4", (std::regex)"[+-]");
    for (size_t i = 0; i < r.size(); i++)
    {
        std::cout << r[i].key << std::endl;
        std::cout << r[i].value << std::endl;
    }
    
    vector<string> string_split(string str, regex reg)
    {
    	smatch m;
    	string::const_iterator iterStart = str.begin();
    	string::const_iterator iterEnd = str.end();
    
    	vector<string> resultSplitList = {};
    
    	while (regex_search(iterStart, iterEnd, m, reg))
    	{
    		resultSplitList.emplace_back(iterStart, m[0].first);
    		iterStart = m[0].second;
    	}
    	resultSplitList.emplace_back(iterStart, iterEnd);
    	return resultSplitList;
    }
    
    string_split(str, regex("\s+"));
    
  • 相关阅读:
    批量解密SQLSERVER数据库中的各种对象的工具dbForge SQL Decryptor
    微软压力测试工具 web application stress
    使用Microsoft Web Application Stress Tool对web进行压力测试
    数据库
    字体转换方法整理
    C# 开源框架
    IP、操作系统、移动OS
    互联网上的业务
    三元组与序列化
    TCP/IP协议(数据封装与拆装过程)
  • 原文地址:https://www.cnblogs.com/ajanuw/p/13360275.html
Copyright © 2011-2022 走看看