網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
C++11 字符串分割代碼示例如下,很顯然, 使用了C++11 特性,代碼簡(jiǎn)潔好多
#include <iostream> #include <string> #include <vector> #include <regex> ? using namespace std; ? //沒(méi)有使用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++ 實(shí)現(xiàn)字符串分割函數(shù) 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
相關(guān)推薦
- 2022-05-01 pandas中關(guān)于apply+lambda的應(yīng)用_python
- 2022-12-19 QT實(shí)現(xiàn)自定義Http客戶(hù)端的示例代碼_C 語(yǔ)言
- 2022-08-20 Redis實(shí)現(xiàn)庫(kù)存扣減的解決方案防止商品超賣(mài)_Redis
- 2022-10-03 docker修改容器的端口、容器名、映射地址以及注意事項(xiàng)_docker
- 2023-03-03 Oracle中trunc()函數(shù)實(shí)例詳解_oracle
- 2022-03-26 Linux快速部署Redis_Redis
- 2022-07-16 BeanFactory和FactoryBean的區(qū)別
- 2022-07-14 Android實(shí)現(xiàn)手機(jī)多點(diǎn)觸摸畫(huà)圓_Android
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支