網站首頁 編程語言 正文
C++11 字符串分割代碼示例如下,很顯然, 使用了C++11 特性,代碼簡潔好多
#include <iostream> #include <string> #include <vector> #include <regex> ? using namespace std; ? //沒有使用C++11特性 vector<string> testSplit(string srcStr, const string& delim) { ?? ?int nPos = 0; ?? ?vector<string> vec; ?? ?nPos = srcStr.find(delim.c_str()); ?? ?while(-1 != nPos) ?? ?{ ?? ??? ?string temp = srcStr.substr(0, nPos); ?? ??? ?vec.push_back(temp); ?? ??? ?srcStr = srcStr.substr(nPos+1); ?? ??? ?nPos = srcStr.find(delim.c_str()); ?? ?} ?? ?vec.push_back(srcStr); ?? ?return vec; } ? //使用C++11特性 vector<string> testSplit11(const string& in, const string& delim) { ? ? vector<string> ret; ? ? try ? ? { ? ? ? ? regex re{delim}; ? ? ? ? return vector<string>{ ? ? ? ? ? ? ? ? sregex_token_iterator(in.begin(), in.end(), re, -1), ? ? ? ? ? ? ? ? sregex_token_iterator() ? ? ? ? ? ?}; ? ? ? ? ? } ? ? catch(const std::exception& e) ? ? { ? ? ? ? cout<<"error:"<<e.what()<<std::endl; ? ? } ? ? return ret; } ? int main() { ?? ?vector<string>ret = testSplit("how many credits ?", " "); ?? ?for(int i = 0 ; i < ret.size(); ++i) ?? ?{ ?? ??? ?cout<<ret[i]<<endl; ?? ?} ?? ? ?? ?return 0; }
C++ 實現字符串分割函數 split
#include <iostream> #include <vector> using namespace std; vector<string> split( strData ) { vector<string> vecData; int nPos = strData.find( "," ); while( nPos > 0 ) { strTmp = strLine.substr( 0, nPos ); vecData.push_back( strTmp ); strLine.erase( 0, nPos+1 ); nPos = strData.find( "," ); } vecData.push_back( strData ); return vecData; }
原文鏈接:https://blog.csdn.net/liangzhao_jay/article/details/87782017
相關推薦
- 2022-12-26 .NET?6實現滑動驗證碼的示例詳解_實用技巧
- 2022-01-12 2022年了--你還不會手寫promise? --_-- promise的實現 第一版
- 2022-12-29 react如何將字符串轉義成html語句_React
- 2022-12-07 C++?如何將Lambda轉換成函數指針_C 語言
- 2022-12-10 Python對XML文件實現增刪改查操作_python
- 2022-04-28 詳解Pandas的三大利器(map,apply,applymap)_python
- 2021-12-12 Docker?Consul概述以及集群環境搭建步驟(圖文詳解)_docker
- 2023-03-19 一文掌握匯編語言?halt?命令_匯編語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支