C++
字串
Index
char
char
字元:用單引號 ' '
#include <iostream>
using namespace std;
int main(void){
char c = 'a';
char a[5] = {'a', 'b', 'c', 'd', 'e'};
char text[] = "hello";
cout << c << '\n'; //a
cout << a[1] << '\n'; //b
char e = "e"; //錯誤
char b = "abc"; //錯誤
}
變數:只能存取一個字元
陣列:可以存取指定數量的字元
char
讀入字元
#include <iostream>
using namespace std;
int main(void){
char c[100];
for(int i = 0; i < 5; i++){
cin >> c[i]; //cin >> apple
}
cout << c[2]; //p
}
用陣列的方式讀入字元
char
以 '\0'
當結尾
#include <iostream>
using namespace std;
int main(void){
char c[] = "hello";
for(int i = 0; i < 10; i++){
if(c[i] == '\0'){
cout << "NULL";
break;
}
cout << c[i];
}
//helloNULL
}
c
h
e
l
l
o
\0
0
1
2
3
4
5
跳脫字元
跳脫字元 ( Escape Character ) 是用來在字串中插入一些無法直接輸入或有特殊意義的字符
在 C++ 中會以反斜線 ( \ ) 開頭
Escape Sequence | Effect |
---|---|
\a | 發出嗶聲 |
\b | 退格符(backspace) |
\n | 換行符(newline) |
\r | 回車符(carriage return) |
\t | 制表符(tab) |
\0 | 結束字元,後面的字元不印出 |
\\ | 反斜線本身 |
\' | 單引號 |
\" | 雙引號 |
what is string
string

string 的中文是字串
↑ 上面那句話就是一個 string
string
char v.s. string
使用 string 更方便輸入、輸出
不用事先訂定大小
//string
#include <iostream>
using namespace std;
int main(void){
string s;
cin >> s;
cout << s;
}
//char
#include <iostream>
using namespace std;
int main(void){
char c[5];
for(int i = 0; i < 5; i++){
cin >> c[i];
cout << c[i];
}
}
char 要用單引號 ' '
string 要用雙引號 " "
「單一」字元
declare
string 的宣告方式有以下幾種
#include <iostream>
using namespace std;
int main() {
string s1;
string s2 = s1;
string s3 = "Hello";
string s4("Hi");
cout << s4; //Hi
}
input
讀入字串
#include <iostream>
using namespace std;
int main() {
string s;
while(cin >> s){
cout << s << '\n';
}
}
如果讀入 "Hi my name is pomero"
上面的程式會怎麼輸出?
input
如果讀入 "Hi my name is pomero"
上面的程式會怎麼輸出?

遇到句子裡的空格時會換行輸出
getline
會有上一頁的問題是因為 cin
會讀取輸入直到有空格或換行
如果想要讀入一整行輸入的話,可以使用 getline()
#include <iostream>
using namespace std;
int main() {
string s;
getline(cin, s);
cout << s; //Hi my name is pomero
}
get value
如果想要得到字串某個位置的值時,可以用陣列的讀取方式拿到或修改
#include <iostream>
using namespace std;
int main() {
string s = "Hello";
cout << s[0] << '\n'; //H
s[1] = 'a';
cout << s; //Hallo
}
connect string
字串可以彼此相加
#include <iostream>
using namespace std;
int main() {
string s1 = "hello";
string s2 = "world";
string s3 = s1 + ' ' + s2 + "!\n"; //OK
string s4 = "123" + "567"; //錯誤,不能直接相加兩個字面值
string s5 = s1 + "aaa" + "bbb";
//OK,等同 s1 + "aaa" 產生一個新 string,新 string 與 "bbb" 相加
string s6 = "aaa" + "bbb" + s1;
//錯誤,"aaa" + "bbb" 會先運算,兩個字面值不能相加
string s7 = s1 + "aaa"; //OK
string s8 = "aaa" + s1; //OK
}
compare string
字串可以互相比較
#include <iostream>
using namespace std;
int main() {
// 以下皆為 true
cout << ("aaa" == "aaa"); // 相同
cout << ("aaa" != "bbb"); // 不相同
cout << ("abcd" < "abcde"); // 規則 1
cout << ("abcd" > "abcc"); // 規則 2,d > c
cout << ("abcd" > "abcceeeeee"); // 規則 2,d > c,即使右邊比較長
}
規則 1:字串長度不同,若前面字元皆相同,則較長的字串較大
規則 2:字串長度不同,從前面數來第一個不同的字元若較大,則該字串較大 ( 字典序 ASCII )
ascii
將字符轉換為對應的數字表示,以便計算機和通信設備之間進行信息交換

method
string 有許多方法可以使用
要使用這些方法需要 #include <string>
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "Hello";
cout << s.empty() << '\n'; //0 (false的意思)
//檢查字串是否為空
cout << s.length() << '\n'; //5
//字串的大小
cout << s.front() << '\n'; //H
//取得字串最前面的字元
cout << s.back(); //o
//取得字串最尾端的字元
}
method
s.find(sub_string)
查詢,回傳第一個發現的子字串的位置
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "Hello", sub_string = "el", new_string = "Hi";
int pos = 2, length = 3;
cout << s.find(sub_string) << '\n'; //1
s.replace(pos, length, new_string);
cout << s << '\n'; //HeHi
cout << s.substr(pos, length) << '\n'; //Hi
s.insert(pos, new_string);
cout << s << '\n'; //HeHiHi
}
s.substr(pos, length)
擷取子字串,從 pos 位置,擷取 length 長度
s.replace(pos, length, new_string)
從
pos 取代 length 長度,變為 new_string
s.insert(pos, new_string)
插入,從 pos 位置,插入一個 new_string
transform
stoi // 轉 int
stol // 轉 long int
stoll // 轉 long long int
stoul // 轉 unsigned long int
stoull // 轉 unsigned long long int
stof // 轉 float
stod // 轉 double
stold // 轉 long double
可以將字串轉換為各種數字型態
需要 #include <string>
#include <iostream>
#include <string>
using namespace std;
int main() {
cout << stoi("2") + stoi("5"); //7
cout << stof("5.5") + stof("6.4"); //11.9
}
transform
也可以用字元處理將字串轉換成整數
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "123456";
int num = 0;
for(int i = 0; i < s.length(); i++){
num += (s[i] - '0'); //'0'的編碼是48,利用數字編碼差值
}
cout << num; //21
}
s[0] = '1'
'1' - '0' = 49 - 48 ( ASCII ) = 1
practice
zerojudge
按照難度排ㄉ ( 大概
下課!
C++ - 5 - string
By pomer0
C++ - 5 - string
- 104