目錄
?
1. 字符與整數的聯系——ASCII碼每個常用字符都對應一個-128 ~ 127的數字,二者之間可以相互轉化。注意:目前負數沒有與之對應的字符。
2.字符數組
2.2 字符數組的常用操作下面幾個函數需要引入頭文件:
2.3 遍歷字符數組中的字符:
3. 標準庫類型string可變長的字符序列,比字符數組更加好用。需要引入頭文件:
3.1 定義和初始化
3.2 string上的操作
(2) 使用getline讀取一整行
(3) string的empty和size操作(注意size是無符號整數,因此 s.size() <= -1一定成立):
(4) string的比較:
(5) 為string對象賦值:
(6) 兩個string對象相加:
(7) 字面值和string對象相加:
3.3 處理string對象中的字符可以將string對象當成字符數組來處理:
4.string中元素的訪問
1、[ ]+下標?因為string類對[ ]運算符進行了重載,所以我們可以直接使用[ ]+下標訪問對象中的元素。并且該重載使用的是引用返回,所以我們可以通過[ ]+下標修改對應位置的元素。
2、使用at訪問對象中的元素?因為at函數也是使用的引用返回,所以我們也可以通過at函數修改對應位置的元素。
3、使用范圍for訪問對象中的元素
5.string中子字符串的提取
1、使用substr函數提取string中的子字符串
?
1. 字符與整數的聯系——ASCII碼
每個常用字符都對應一個-128 ~ 127的數字,二者之間可以相互轉化。注意:目前負數沒有與之對應的字符。
#include <iostream>using namespace std;int main()
{char c = 'a';cout << (int)c << endl;int a = 66;cout << (char)a << endl;return 0;
}
常用ASCII值:'A'- 'Z'是65 ~ 90,'a' - 'z'是97 - 122,0 - 9是 48 - 57。
字符可以參與運算,運算時會將其當做整數:
#include <iostream>using namespace std;int main()
{int a = 'B' - 'A';int b = 'A' * 'B';char c = 'A' + 2;cout << a << endl;cout << b << endl;cout << c << endl;return 0;
}
2.字符數組
字符串就是字符數組加上結束符'\0'。
可以使用字符串來初始化字符數組,但此時要注意,每個字符串結尾會暗含一個'\0'字符,因此字符數組的長度至少要比字符串的長度多 1 !
?
#include <iostream>using namespace std;int main()
{char str[100];cin >> str; // 輸入字符串時,遇到空格或者回車就會停止cout << str << endl; // 輸出字符串時,遇到空格或者回車不會停止,遇到'\0'時停止printf("%s\n", str);return 0;
}
讀入一行字符串,包括空格:
#include <iostream>using namespace std;int main()
{char str[100];fgets(str, 100, stdin); // gets函數在新版C++中被移除了,因為不安全。// 可以用fgets代替,但注意fgets不會刪除行末的回車字符cout << str << endl;return 0;
}
2.2 字符數組的常用操作
下面幾個函數需要引入頭文件:
#include <string.h>
(1) strlen(str),求字符串的長度
(2) strcmp(a, b),比較兩個字符串的大小,a < b返回-1,a == b返回0,a > b返回1。這里的比較方式是字典序!
(3) strcpy(a, b),將字符串b復制給從a開始的字符數組。
#include <iostream>
#include <string.h>using namespace std;int main()
{char a[100] = "hello world!", b[100];cout << strlen(a) << endl;strcpy(b, a);cout << strcmp(a, b) << endl;return 0;
}
2.3 遍歷字符數組中的字符:
?
#include <iostream>
#include <string.h>using namespace std;int main()
{char a[100] = "hello world!";// 注意:下述for循環每次均會執行strlen(a),運行效率較低,最好將strlen(a)用一個變量存下來for (int i = 0; i < strlen(a); i ++ )cout << a[i] << endl;return 0;
}
3. 標準庫類型string
可變長的字符序列,比字符數組更加好用。需要引入頭文件:
#include <string>
3.1 定義和初始化
#include <iostream>
#include <string>using namespace std;int main()
{string s1; // 默認初始化,s1是一個空字符串string s2 = s1; // s2是s1的副本,注意s2只是與s1的值相同,并不指向同一段地址string s3 = "hiya"; // s3是該字符串字面值的副本string s4(10, 'c'); // s4的內容是 "cccccccccc"return 0;
}
3.2 string上的操作
(1) string的讀寫:
#include <iostream>
#include <string>using namespace std;int main()
{string s1, s2;cin >> s1 >> s2;cout << s1 << s2 << endl;return 0;
}
注意:不能用printf直接輸出string,需要寫成:printf(“%s”, s.c_str());
(2) 使用getline讀取一整行
#include <iostream>
#include <string>using namespace std;int main()
{string s;getline(cin, s);cout << s << endl;return 0;
}
(3) string的empty和size操作(注意size是無符號整數,因此 s.size() <= -1一定成立):
#include <iostream>
#include <string>using namespace std;int main()
{string s1, s2 = "abc";cout << s1.empty() << endl;cout << s2.empty() << endl;cout << s2.size() << endl;return 0;
}
(4) string的比較:
支持 >, <, >=, <=, ==, !=等所有比較操作,按字典序進行比較。
(5) 為string對象賦值:
string s1(10, 'c'), s2; // s1的內容是 cccccccccc;s2是一個空字符串
s1 = s2; // 賦值:用s2的副本替換s1的副本// 此時s1和s2都是空字符串
(6) 兩個string對象相加:
做加法運算時,字面值和字符都會被轉化成string對象,因此直接相加就是將這些字面值串聯起來:string s1 = "hello", s2 = "world"; // 在s1和s2中都沒有標點符號
string s3 = s1 + ", " + s2 + '\n';
(7) 字面值和string對象相加:
做加法運算時,字面值和字符都會被轉化成string對象,因此直接相加就是將這些字面值串聯起來:
string s1 = "hello", s2 = "world"; // 在s1和s2中都沒有標點符號
string s3 = s1 + ", " + s2 + '\n';
當把string對象和字符字面值及字符串字面值混在一條語句中使用時,必須確保每個加法運算符的兩側的運算對象至少有一個是string:
string s4 = s1 + ", "; // 正確:把一個string對象和有一個字面值相加
string s5 = "hello" + ", "; // 錯誤:兩個運算對象都不是stringstring s6 = s1 + ", " + "world"; // 正確,每個加法運算都有一個運算符是string
string s7 = "hello" + ", " + s2; // 錯誤:不能把字面值直接相加,運算是從左到右進行的
3.3 處理string對象中的字符
可以將string對象當成字符數組來處理:
#include <iostream>
#include <string>using namespace std;int main()
{string s = "hello world";for (int i = 0; i < s.size(); i ++ )cout << s[i] << endl;return 0;
}
或者使用基于范圍的for語句:
#include <iostream>
#include <string>using namespace std;int main()
{string s = "hello world";for (char c: s) cout << c << endl;for (char& c: s) c = 'a';cout << s << endl;return 0;
}
4.string中元素的訪問
1、[ ]+下標
?因為string類對[ ]運算符進行了重載,所以我們可以直接使用[ ]+下標訪問對象中的元素。并且該重載使用的是引用返回,所以我們可以通過[ ]+下標修改對應位置的元素。
#include <iostream>
#include <string>
using namespace std;
int main()
{string s("CSDN");//[]+下標訪問對象元素for (size_t i = 0; i < s.size(); i++){cout << s[i];}cout << endl;//[]+下標修改對象元素內容for (size_t i = 0; i < s.size(); i++){s[i] = 'x';}cout << s << endl; //xxxxreturn 0;
}
2、使用at訪問對象中的元素
?因為at函數也是使用的引用返回,所以我們也可以通過at函數修改對應位置的元素。
#include <iostream>
#include <string>
using namespace std;
int main()
{string s("CSDN");for (size_t i = 0; i < s.size(); i++){//at(pos)訪問pos位置的元素cout << s.at(i);}cout << endl;for (size_t i = 0; i < s.size(); i++){//at(pos)訪問pos位置的元素,并對其進行修改s.at(i) = 'x';}cout << s << endl; //xxxxreturn 0;
}
3、使用范圍for訪問對象中的元素
需要特別注意的是:若是需要通過范圍for修改對象的元素,則用于接收元素的變量e的類型必須是引用類型,否則e只是對象元素的拷貝,對e的修改不會影響到對象的元素。
#include <iostream>
#include <string>
using namespace std;
int main()
{string s("CSDN");//使用范圍for訪問對象元素for (auto e : s){cout << e;}cout << endl; //CSDN//使用范圍for訪問對象元素,并對其進行修改for (auto& e : s) //需要修改對象的元素,e必須是引用類型{e = 'x';}cout << s << endl; //xxxxreturn 0;
}
5.string中子字符串的提取
1、使用substr函數提取string中的子字符串
#include <iostream>
#include <string>
using namespace std;
int main()
{string s1("abcdef");string s2;//substr(pos, n)提取pos位置開始的n個字符序列作為返回值s2 = s1.substr(2, 4);cout << s2 << endl; //cdefreturn 0;
}
#include <iostream>
#include <string>
using namespace std;
int main()
{string s("abcdef");char str[20];//copy(str, n, pos)復制pos位置開始的n個字符到str字符串size_t length = s.copy(str, 4, 2);//copy函數不會在復制內容的末尾附加'\0',需要手動加str[length] = '\0';cout << str << endl; //cdefreturn 0;
}
?
?
?