欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當前位置:主頁 > 軟件編程 > C語言 >

如何在c++中實現(xiàn)字符串分割函數(shù)split詳解

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊: 次

前言

在學(xué)習(xí)c++中string相關(guān)基本用法的時候,發(fā)現(xiàn)了sstream的istringstream[1]可以將字符串類似于控制臺的方式進行輸入,而實質(zhì)上這個行為等同于利用空格將一個字符串進行了分割,于是考慮到可以利用這個特性來實現(xiàn)c++庫函數(shù)中沒有的字符串分割函數(shù)split

string src("Avatar 123 5.2 Titanic K");
istringstream istrStream(src); //建立src到istrStream的聯(lián)系
string s1, s2;
int n; double d; char c;
istrStream >> s1 >> n >> d >> s2 >> c;
//以空格為分界的各數(shù)值則輸入到了對應(yīng)變量上

實現(xiàn)細節(jié)

目的是可以像js中一樣,調(diào)用一個函數(shù)即可以方便地獲取到處理完畢后的字符串數(shù)組,根據(jù)c++的實際情況再進行參數(shù)調(diào)整。

1. 輸入輸出:

string* split(int& length, string str, const char token = ' ')

返回:處理完的字符串數(shù)組的首地址

傳入:字符串str、分隔符token(默認參數(shù)為空格)、以及引用參數(shù)length,指明處理完畢后動態(tài)分配的數(shù)組長度

2. 數(shù)據(jù)透明處理:

由于istringstream會像cin一樣,把空格視為數(shù)據(jù)間的界限,所以當分隔符不是空格時,需要將傳入的分隔符換為空格,并且要提前對原有空格進行數(shù)據(jù)透明處理

字符替換利用了庫algorithm中的replace() [2]

 const char SPACE = 0;
 if(token!=' ') {
 // 先把原有的空格替換為ASCII中的不可見字符
 replace(str.begin(), str.end(), ' ', SPACE); 
 // 再把分隔符換位空格,交給字符串流處理
 replace(str.begin(), str.end(), token, ' ');
 }

  假設(shè)輸入字符串為:"a b,c,d,e,f g"
  分隔符為非空格:','
  則被替換為:"aSPACEb c d e fSPACEg"

3. 數(shù)據(jù)分割:

 //實例化一個字符串輸入流,輸入?yún)?shù)即待處理字符串
 istringstream i_stream(str); 
 //將length置零
 length = 0; 
 queue<string> q;
 //用一個string實例s接收輸入流傳入的數(shù)據(jù),入隊并計數(shù)
 string s;
 while (i_stream>>s) {
 q.push(s);
 length++;
 }

4. 數(shù)組生成:

 //根據(jù)計數(shù)結(jié)果動態(tài)開辟一個字符串數(shù)組空間
 string* results = new string[length]; 
 //將隊列中的數(shù)據(jù)轉(zhuǎn)入數(shù)組中
 for (int i = 0; i < length; i++) {
 results[i] = q.front();
 //將替換掉的空格進行還原
 if(token!=' ') replace(results[i].begin(), results[i].end(), SPACE, ' ');
 q.pop();
 }

完整代碼

#include <iostream>
#include <string>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;

string* split(int& length, string str,const char token = ' ') {
 const char SPACE = 0;
 if(token!=' ') {
 replace(str.begin(), str.end(), ' ', SPACE);
 replace(str.begin(), str.end(), token, ' ');
 }
 istringstream i_stream(str);
 queue<string> q;
 length = 0;
 string s;
 while (i_stream>>s) {
 q.push(s);
 length++;
 }
 string* results = new string[length];
 for (int i = 0; i < length; i++) {
 results[i] = q.front();
 q.pop();
 if(token!=' ') replace(results[i].begin(), results[i].end(), SPACE, ' ');
 }
 return results;
}

//測試:
int main() {
 int length;
 string* results = split(length, "a b,c,d,e,f g", ',');
 for (int i = 0; i < length; i++) cout<<results[i]<<endl;
 return 0;
}

參考

[1] C++ string類(C++字符串)完全攻略

[2] C++ string 替換指定字符

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對我們的支持。

上一篇:C語言實現(xiàn)萬年歷程序

欄    目:C語言

下一篇:C語言仿QQ聊天界面抖動功能

本文標題:如何在c++中實現(xiàn)字符串分割函數(shù)split詳解

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/129.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有