0. 歷史遺留問題
- C語言不支持真正意義上的字符串
- C語言用字符數組和一組函數是實現字符串操作
- C語言不支持自定義類型,因此無法獲得字符串類型
1. 解決方案
- 從C到C++的進化過程引入了自定義類型
- 在C++中可以通過類完成字符串類型的定義
2. 標準庫中的字符串類
- C++語言直接支持C語言的所有概念
- C++語言中沒有原生的字符串類型
C++標準庫提供了string類型
- string 直接支持字符串連接
- string 直接支持字符串的大小比較
- string 直接支持子串查找和提取
- string 直接支持字符串的插入和替換
編程說明:字符串類的使用
#include <iostream>
#include <string>
using namespace std;
void string_sort(string a[], int len)
{
for(int i=0; i<len; i++)
{
for(int j=i; j<len; j++)
{
if( a[i] > a[j] )
{
swap(a[i], a[j]);
}
}
}
}
string string_add(string a[], int len)
{
string ret = "";
for(int i=0; i<len; i++)
{
ret += a[i] + "; ";
}
return ret;
}
void print(string a[], int len)
{
for(int i=0; i<len; i++)
{
cout << a[i] << endl;
}
}
int main()
{
string sa[7] =
{
"Hello World",
"D.T.Software",
"C#",
"Java",
"C++",
"Python",
"TypeScript"
};
string_sort(sa, 7);
print(sa, 7);
cout << endl;
cout << string_add(sa, 7) << endl;
return 0;
}
輸出結果:
C#
C++
D.T.Software
Hello World
Java
Python
TypeScript
C#; C++; D.T.Software; Hello World; Java; Python; TypeScript;
3. 標準庫中的字符串類——字符串與數字的轉換:字符串流類(sstream)
- 字符串與數字的轉換
- 標準庫中提供了相關的類對字符串和數字進行轉換
- 字符串流類(sstream)用于string的轉換
<sstream> - 相關頭文件
istringstream - 字符串輸入流
ostringstream - 字符串輸出流
- 使用方法:
- string -> 數字:
istringstream iss("123.45"); double num; iss >> num;
- 數字 -> string
ostringstream oss; oss << 543.21; string s = oss.str();
編程說明:字符串和數字的轉換
#include <iostream>
#include <sstream>
#include <string>
// 使用宏來轉換,用到了臨時對象
#define TONUMBER(s, n) (istringstream(s)>>n)
#define TOSTRING(n) (((ostringstream&)(ostringstream() << n)).str())
using namespace std;
/*
bool to_number(const string& s, int& n) // 使用全局函數來將字符串轉化為數字(數字類型只能固定,需要用到后面的模板)
{
istringstream iss(s);
return iss >> n;
}
*/
/*
string to_string(int n) // 使用全局函數來將數字轉化為字符串(數字類型只能固定,需要用到后面的模板)
{
string s = "";
ostringstream oss;
oss << n;
s = oss.str();
return s;
}
*/
int main()
{
int num = 0;
if(TONUMBER("123", num))
{
cout << num << endl;
}
cout << TOSTRING(789) << endl;
return 0;
}
輸出結果:
123
789
4. 面試題分析:字符串循環右移
編程說明:字符串循環右移
#include <iostream>
#include <string>
using namespace std;
string operator >> (const string& s, unsigned int n)
{
string ret = "";
// abcdefd ==> 3 ==> efgabcd
unsigned int pos = 0;
n = n % s.length();
pos = s.length() - n;
ret = s.substr(pos);
ret += s.substr(0, pos);
return ret;
}
int main()
{
string str = "abcdefg";
cout << (str >> 3) << endl;
return 0;
}
輸出結果:
efgabcd
5. 小結
- 應用開發中大多數的情況都是在進行字符串處理
- C++中沒有直接支持原生的字符串類型
- 標準庫中string類支持字符串的概念
- string類支持字符串和數字的相互轉換
- string類的應用使得問題的求解變得簡單
6. 課后練習
字符串反轉
自己測試通過的代碼,希望有朋友看到后提出不同的測試代碼,一起討論。測試過程中,由于作業要求必須傳入const string& s
,只讀字符串的轉換困擾我許久,后來通過強制轉換后實現并測試通過
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string str_reverse(const string& s, const char c)
{
string ret = "";
string* ss = const_cast<string*> (&s);
int n = (*ss).find(c);
while( n != string::npos)
{
string sub_str = "";
int pos = 0;
sub_str = (*ss).substr(pos, n);
reverse(sub_str.begin(), sub_str.end());
ret += sub_str + c;
(*ss) = (*ss).substr(n+1);
n = (*ss).find(c);
}
reverse((*ss).begin(), (*ss).end());
ret += (*ss);
return ret;
}
int main()
{
cout << str_reverse("", ';') << endl; // 輸出:空字符串
cout << str_reverse(";", ';') << endl; // 輸出:;
cout << str_reverse("abcde;", ';') << endl; // 輸出:edcba;
cout << str_reverse("we;tonight;you", ';') << endl; // 輸出:ew;thginot;uoy
return 0;
}
輸出結果:
;
edcba;
ew;thginot;uoy