C++按照特殊字符分割字符串--日期排序

一、string中的find()函數

原型:size_t find(const string& str,size_t pos = 0)const;

功能:返回子字符串第一次出現的位置

返回值:找到返回第一次出現的位置,否則返回string::npos

例子:

輸入:stringing

輸出:3

int main() {
	string str;
	string substr;
	while (cin>>str)
	{
		cin >> substr;
		size_t  n = str.find(substr);
		if (n != string::npos) {
			cout << n << endl;
		}
		else {
			cout << -1 << endl;
		}
	}
}

二、string中的substr()函數

原型:string substr(size_t pos = 0,size_t n = nops)const;

功能:獲取子串

參數說明:pos爲起始位置(默認爲0),n爲結束位置(默認爲npos)

返回值:子串

例子:

輸入:test

輸出:test

           est

           est

           est

int main() {
	string str;
	while (cin>>str)
	{
		cout << str.substr()<<endl;
		cout << str.substr(1,string::npos) << endl;
		cout << str.substr(1, str.size()-1) << endl;
		cout << str.substr(1, str.size()) << endl;
	}
}

三、strtok_s()函數

原型:char *strtok_s( char *strToken, const char *strDelimit, char **buf);

功能:分割字符串,將剩餘的字符串存儲在buf變量中,而不是靜態變量中,從而保證了安全性。

參數說明:strToken爲待分割的字符串,char爲分割標誌,buf用於存放剩餘字符串

返回值:返回分割出來的子串的首地址

例子:

#include<iostream>
#include<cstring>
#include<string>
using namespace std;

int main() {
	char str[] = "now # is the time for all # good men to come to the # aid of their country"; //緩衝塊首地址
	char delims[] = "#";
	char *strToken = nullptr;
	char *next_token = nullptr;

	strToken = strtok_s(str, delims, &next_token);

	while (strToken != NULL) {
		printf("result is \"%s\"\n", strToken);
		strToken = strtok_s(next_token, delims, &next_token);
	}
	for (;;);
}

三、C++中使用string的find()和substr()函數實現字符串分割

需要注意一個技巧就是將分割符號添加到string的末尾,方便分割最後一個字符。

例子:

#include<iostream>
#include<string>
#include<vector>
using namespace std;

void split(string &str, string delimit, vector<string>&result) {
	size_t pos = str.find(delimit);
	str += delimit;//將分隔符加入到最後一個位置,方便分割最後一位
	while (pos != string::npos) {
		result.push_back(str.substr(0, pos));
		str = str.substr(pos + 1);//substr的第一個參數爲起始位置,第二個參數爲複製長度,默認爲string::npos到最後一個位置
		pos = str.find(delimit);
	}
}
int main() {
	string str;
	string delimit;
	while (cin>>str)
	{
		cin >> delimit;
		vector<string> result;
		split(str, delimit, result);
		for (int i = 0; i < result.size(); ++i) {
			cout << result[i] << ' ';
		}
	}
}

 

綜合案例:

version-1:

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;

class Data {
public:
	int year, month, day;
	Data(int y, int m, int d) :year(y), month(m), day(d) {}
};
//將字符串數字按照特殊字符分割
void split_string(string str, string delimit, vector<string>&result) {
	size_t pos = str.find(delimit);
	str += delimit;
	while (pos != string::npos) {
		result.push_back(str.substr(0, pos));
		str = str.substr(pos + 1);
		pos = str.find(delimit);
	}
}
//將字符串按照特殊字符分割並轉換成數字
void split_number(string str, string delimit, vector<int>&result) {
	size_t pos = str.find(delimit);
	str += delimit;
	while (pos != string::npos) {
		string temp = str.substr(0, pos);
		int n = atoi(temp.c_str());//atoi可以將字符串"1234"轉換成int類型1234
		result.push_back(n);
		str = str.substr(pos + 1);
		pos = str.find(delimit);
	}
}
bool comparer(Data* object_a, Data* object_b) {
	if (object_a->year == object_b->year)
		if (object_a->month == object_b->month)
			return object_a->day < object_b->day;
		else
			return object_a->month < object_b->month;
	else
		return object_a->year < object_b->year;
}
int main() {
	string str;
	while (cin >> str)
	{
		/************************先將日期按照','分開存放在data_split_str數組中******************************/
		vector<string> data_split_str;
		split_string(str, ",", data_split_str);//得到分割的日期

		/************************再將data_split_str中的每個數據分割成數字存放在all中************************/
		vector<vector<int>> all;
		for (int i = 0; i < data_split_str.size(); ++i) {
			vector<int> data_split_num;
			split_number(data_split_str[i], ".", data_split_num);
			all.push_back(data_split_num);
		}

		/**************構建Data對象數組,每個對象包含三個數據屬性year,month,day**************************/
		vector<Data*> v_data;
		for (int i = 0; i < all.size(); i++) {
			Data* temp = new Data(all[i][0], all[i][1], all[i][2]);
			v_data.push_back(temp);
		}

		/**************將對象數組按照年月日進行排序即可***********/
		sort(v_data.begin(), v_data.end(), comparer);
		for (int i = 0; i < v_data.size(); ++i) {
			cout << v_data[i]->year << '.' << v_data[i]->month << '.' << v_data[i]->day;
			if (i != v_data.size() - 1) cout << ',';
		}
	}
}

 

version-2【試錯--記錄】:日期分割後 不轉換成int類型比較,直接使用字符串比較.

參考鏈接:https://www.php.cn/faq/415854.html

因此,上述代碼並不需要轉換成int類型再比較,可以直接比較字符串了。

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;

//將字符串數字按照特殊字符分割
void split_string(string &str, string delimit, vector<string>&result) {
	size_t pos = str.find(delimit);
	str += delimit;
	while (pos != string::npos) {
		result.push_back(str.substr(0, pos));
		str = str.substr(pos + 1);
		pos = str.find(delimit);
	}
}
int main() {
	string str;
	string delimit;
	while (cin>>str)
	{
		/************************先將日期按照','分開存放在all數組中************************/
		string delimit = ",";
		vector<string> data_split_str;
		split_string(str, delimit, data_split_str);//得到分割的日期
		/************************直接對字符串進行比較************************/
		sort(data_split_str.begin(), data_split_str.end());
		for (int i = 0; i < data_split_str.size(); ++i) {
			cout << data_split_str[i];
			if (i != data_split_str.size() - 1) cout << ',';
		}
	}
}

但是:發現自己還是犯錯了,因爲字符串比較是完全按照單個字符進行比較的,比如1994.1.12和1994.1.3,進行比較的時候比較到12和3的時候,12拆分爲字符1和2,所以1肯定比3小,導致錯誤的結果。

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章