欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++?分割字符串數(shù)據(jù)的實現(xiàn)方法

 更新時間:2023年09月01日 16:25:32   作者:^@^lemon?tea^@^  
這篇文章主要介紹了C++?分割字符串數(shù)據(jù)的實現(xiàn)方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

C++ 分割字符串數(shù)據(jù)的實現(xiàn)方法

使用stringstream流(已驗證)

引入頭文件

#include <sstream>

功能實現(xiàn):

string keyValue = "WTH01#WTH02#WTH03#WTH04#WTH05"
string keys[6] = { "" };
stringstream is(keyValue);
string temp;
int index = 0;
while (getline(is, temp, '#'))
{
    keys[index] = temp;
    index++;
    cout << temp << endl;
}

結(jié)果圖

C++中string如何實現(xiàn)字符串分割函數(shù)split()——4種方法

如:

string str1 = "This is a test";
string str2 = "This-is-a-test";
string str2 = "This+is+a+test";

我們?nèi)绾螌⒁陨献址凑漳撤N分隔符( , - , + ),將其分割成四個子串,其值分別為 “This” “is” “a” “test” 。

一、使用stringstream流

這里我們只需要用到 istringstream (字符串輸入流) 構(gòu)造字符串流,然后從字符串流中按照一定的格式讀取數(shù)據(jù)即可。

通常我們使用 cin 從流中讀取數(shù)據(jù),而我們也可以使用 getline 讀取,而后者在讀取時可以選擇接受的數(shù)據(jù)格式,其函數(shù)原型如下:

// istream & getline(char* buf, int bufSize);	// 讀到 \n 為止
istream & getline(char* buf, int bufSize, char delim); //讀到 delim 字符為止
				// \n 或 delim 都不會被讀入 buf,但會被從文件輸入流緩沖區(qū)中取走

 因此,我們可以按照此方式設(shè)計一個C++中的string split函數(shù)。

void Stringsplit(string str,const const char split)
{
	istringstream iss(str);	// 輸入流
	string token;			// 接收緩沖區(qū)
	while (getline(iss, token, split))	// 以split為分隔符
	{
		cout << token << endl; // 輸出
	}
}

如此,我們就設(shè)計出了我們的Stringsplit() 函數(shù)。該函數(shù)有以下 2 種語法格式

void Stringsplit(string str,const const char split);
// 默認將傳入的字符串str以split為分隔符進行分割,并將得到的子串打印在屏幕上,無返回值
void Stringsplit(string str, const const char split,vector<string>& rst);
// 默認將傳入的字符串str以split為分隔符進行分割,    不會將子串打印在屏幕上,無返回值
// 					分割的子串將會保存在rst數(shù)組中被帶出函數(shù)。

 以上,我們簡單的設(shè)計了一種C++中的分割字符串的函數(shù),下面來看一個測試用例:

int main() {
	string str("This is a test");
	Stringsplit(str, ' ');		// 打印子串
	vector<string> strList;
	string str2("This-is-a-test");
	Stringsplit(str2, '-', strList);	// 將子串存放到strList中
	for (auto s : strList)
		cout << s << " ";
	cout << endl;
	return 0;
}

# 輸出
This
is
a
test
This is a test

二、使用string類提供的find方法與substr方法

find函數(shù)原型: 

size_type find( const basic_string& str, size_type pos = 0 ) const;

參數(shù):str - 要搜索的 string , pos - 開始搜索的位置
返回值
找到的子串的首字符位置,或若找不到這種子串則為 npos 。

substr函數(shù)原型: 

basic_string substr( size_type pos = 0, size_type count = npos ) const;

參數(shù):pos - 要包含的首個字符的位置 ,count - 子串的長度
返回值
含子串 [pos, pos+count) 的 string 。

由以上兩個函數(shù)我們便可以設(shè)計出我們的Stringsplit()來。同時,因為find()函數(shù)查找的可以是字符串,因此我們的分隔符可以是單個的字符,也可以是一個字符串。 

// 使用字符分割
void Stringsplit(const string& str, const char split, vector<string>& res)
{
	if (str == "")		return;
	//在字符串末尾也加入分隔符,方便截取最后一段
	string strs = str + split;
	size_t pos = strs.find(split);
	// 若找不到內(nèi)容則字符串搜索函數(shù)返回 npos
	while (pos != strs.npos)
	{
		string temp = strs.substr(0, pos);
		res.push_back(temp);
		//去掉已分割的字符串,在剩下的字符串中進行分割
		strs = strs.substr(pos + 1, strs.size());
		pos = strs.find(split);
	}
}
// 使用字符串分割
void Stringsplit(const string& str, const string& splits, vector<string>& res)
{
	if (str == "")		return;
	//在字符串末尾也加入分隔符,方便截取最后一段
	string strs = str + splits;
	size_t pos = strs.find(splits);
	int step = splits.size();
	// 若找不到內(nèi)容則字符串搜索函數(shù)返回 npos
	while (pos != strs.npos)
	{
		string temp = strs.substr(0, pos);
		res.push_back(temp);
		//去掉已分割的字符串,在剩下的字符串中進行分割
		strs = strs.substr(pos + step, strs.size());
		pos = strs.find(splits);
	}
}

下面是一個測試用例:

int main()
{
	vector<string> strList;
	string str("This-is-a-test");
	Stringsplit(str, '-', strList);
	for (auto s : strList)
		cout << s << " ";
	cout << endl;
	vector<string> strList2;
	string str2("This%20is%20a%20test");
	Stringsplit(str2, "%20", strList2);
	for (auto s : strList2)
		cout << s << " ";
	cout << endl;
	return 0;
}

# 輸出
This is a test
This is a test

三、使用C庫函數(shù)strtok

char* strtok( char* str, const char* delim );

參數(shù):str - 指向要記號化的空終止字節(jié)字符串的指針
delim - 指向標識分隔符的空終止字節(jié)字符串的指針
返回值:指向下個記號起始的指針,或若無更多記號則為空指針。

需要注意的是,該函數(shù)使用一個全局的靜態(tài)變量來保存每次分割后的位置,因此在多線程中是不安全的,這里我們也可以選擇使用它的線程安全版本

 char *strtok_r(char *str, const char *delim, char **saveptr); 。
void Stringsplit(const string& str, const string& split, vector<string>& res)
{
	char* strc = new char[str.size() + 1];
	strcpy(strc, str.c_str());   // 將str拷貝到 char類型的strc中
	char* temp = strtok(strc, split.c_str());
	while (temp != NULL)
	{
		res.push_back(string(temp));		
		temp = strtok(NULL, split.c_str());	// 下一個被分割的串
	}
	delete[] strc;
}

如此,我們的使用 strtok 版本的Stringsplit() 就完成了。不過,我們使用這種方法實現(xiàn)的字符串分割函數(shù)只能根據(jù)字符來分割,而我們傳入的參數(shù)是字符串類型,這樣可能會對函數(shù)的使用這造成誤導(dǎo)(注:參數(shù)傳入字符串用的雙引號,傳入字符用的單引號),因此我們也可以使用下面的方法封裝一個參數(shù)是字符類型的函數(shù)。

void Stringsplit(const string& str, const char split, vector<string>& res)
{
	Stringsplit(str, string(1,split), res);	// 調(diào)用上一個版本的Stringsplit()
}

 下面給出一個測試用例,我們分別使用單/雙引號傳入分割的限定字符。

int main()
{
	vector<string> strList;
	string str("This+is+a+test");
	Stringsplit(str, '+', strList);
	for (auto s : strList)
		cout << s << " ";
	cout << endl;
	vector<string> strList2;
	string str2("This-is-a-test");
	Stringsplit(str2, "-", strList2);
	for (auto s : strList2)
		cout << s << " ";
	cout << endl;
	return 0;
}

四、使用regex_token_iterator(正則表達式)

正則表達式(regular expression)描述了一種字符串匹配的模式(pattern),可以用來檢查一個串是否含有某種子串、將匹配的子串替換或者從某個串中取出符合某個條件的子串等。 

而在C++的正則中,把這種操作稱為Tokenize分詞(或者叫切割)。這種操作剛好可以滿足我們的需求,用模板類regex_token_iterator<>提供分詞迭代器,可以完成字符串的分割。

void Stringsplit(const string& str, const string& split, vector<string>& res)
{
	//std::regex ws_re("\\s+"); // 正則表達式,匹配空格 
	std::regex reg(split);		// 匹配split
	std::sregex_token_iterator pos(str.begin(), str.end(), reg, -1);
	decltype(pos) end;              // 自動推導(dǎo)類型 
	for (; pos != end; ++pos)
	{
		res.push_back(pos->str());
	}
}

測試用例:

int main()
{
	// 單個字符分詞
	vector<string> strList;
	string str("This is a test");
	Stringsplit(str," ", strList);
	for (auto s : strList)
		cout << s << " ";
	cout << endl;
	// 使用字符串分詞
	vector<string> strList2;
	string str2("ThisABCisABCaABCtest");
	Stringsplit(str2, "ABC", strList2);
	for (auto s : strList2)
		cout << s << " ";
	cout << endl;
}

# 輸出
This is a test
This is a test

到此這篇關(guān)于C++ 分割字符串數(shù)據(jù)的文章就介紹到這了,更多相關(guān)C++ 分割字符串內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C++靜態(tài)庫與動態(tài)庫文件的生成和使用教程

    C++靜態(tài)庫與動態(tài)庫文件的生成和使用教程

    庫文件是計算機上的一類文件,可以簡單的把庫文件看成一種代碼倉庫,它提供給使用者一些可以直接拿來用的變量、函數(shù)和類,下面這篇文章主要給大家介紹了關(guān)于C++靜態(tài)庫與動態(tài)庫文件的生成和使用的相關(guān)資料,需要的朋友可以參考下
    2023-03-03
  • 詳解C++編程中表達式的語義與計算順序

    詳解C++編程中表達式的語義與計算順序

    這篇文章主要介紹了C++編程中表達式的語義與計算順序,是C++入門學習中的基礎(chǔ)知識,需要的朋友可以參考下
    2016-01-01
  • C++中const與#define的利弊分析

    C++中const與#define的利弊分析

    C++中不但可以用define定義常量還可以用const定義常量,下面這篇文章主要給大家分析介紹了關(guān)于C++中const與#define的利弊,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2018-05-05
  • C語言之實現(xiàn)字符串小寫變大寫的實例

    C語言之實現(xiàn)字符串小寫變大寫的實例

    這篇文章主要介紹了C語言之實現(xiàn)字符串小寫變大寫的實例的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • c語言網(wǎng)絡(luò)編程-標準步驟(比較簡單)

    c語言網(wǎng)絡(luò)編程-標準步驟(比較簡單)

    這篇文章主要介紹了c語言網(wǎng)絡(luò)編程-標準步驟(比較簡單),需要的朋友可以參考下
    2014-01-01
  • C語言簡析指針用途

    C語言簡析指針用途

    C語言這門課程在計算機的基礎(chǔ)教學中一直占有比較重要的地位,然而要想突破C語言的學習,對指針的掌握是非常重要的,本文將具體針對指針的基礎(chǔ)做詳盡的介紹
    2022-07-07
  • C++中fork函數(shù)的使用及原理

    C++中fork函數(shù)的使用及原理

    這篇文章主要介紹了C++中fork函數(shù)的使用及原理,在C++中,fork函數(shù)用于創(chuàng)建一個新的進程稱為子進程,該進程與原始進程幾乎完全相同,需要的朋友可以參考下
    2023-05-05
  • C數(shù)據(jù)結(jié)構(gòu)中串簡單實例

    C數(shù)據(jù)結(jié)構(gòu)中串簡單實例

    這篇文章主要介紹了C數(shù)據(jù)結(jié)構(gòu)中串簡單實例的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Qt使用QChart實現(xiàn)靜態(tài)顯示溫度變化曲線

    Qt使用QChart實現(xiàn)靜態(tài)顯示溫度變化曲線

    QChart模塊是Qt?Charts庫的基礎(chǔ),提供了用于創(chuàng)建和顯示各種類型圖表的類和接口,本文主要介紹了如何使用QChart實現(xiàn)動態(tài)顯示3個設(shè)備的溫度變化曲線,感興趣的可以了解一下
    2023-06-06
  • C++中的異常實例詳解

    C++中的異常實例詳解

    異常處理是C++的一項語言機制,用于在程序中處理異常事件,下面這篇文章主要給大家介紹了關(guān)于C++中異常的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-02-02

最新評論