C++標準庫之String

C++中支持的字符串處理的函數庫叫String,但它不是STL,卻與STL操作十分相似。

1.聲明:

使用String之前要有以下頭文件

#include<string>
using namespace std;

聲明方法

string s;   //聲明一個string對象 s
string s[10]; //聲明一個string對象數組s 

初始化string對象

直接初始化:利用小括號完成;

拷貝初始化:利用等號完成拷貝過程(減少使用);

string s1;            //定義一個空字符 
string s2 = s1;       //拷貝初始化,將s1賦值給s2 
string s3 = "Hello";  //拷貝初始化,用字符串字面值初始化s3 
string s4(10,'c');    //直接初始化,s4內容是cccccccccc 
string s5(s3);        //直接初始化,這是新的初始化方法,等價于s5 = s3 
string s6("World");      //直接初始化,這是新的初始化方法,等價于s6 = “World”

以下是運行結果

//
//
//Hello
//cccccccccc
//Hello
//World

輸入字符串方法

cin >> s7;          // 讀取有效字符串直到遇到空格 
getline(cin,s8);    // 讀取字符串直到遇到換行結束,可讀入空格 
getline(cin,s9,'a') // 讀取除了'a'以外的所有字符串,包括'\n'和空格 

如果想反復讀入字符

string s10;
while(cin >> s10){//其他操作 
}

2.string對象的操作

s.empty();                  //   判斷是否為空,返回bool型 
s.size(),s.length();        //   返回字符串的個數 
s[n];                       //   字符串第n-1個字符,從0開始計數 
s1+s2;                      //   將s2字符串連接在s1后面 s = s + '0';            //   在s字符串后面加個字符0 s = s + 'a'             //   在s字符串后面加個字符a string s2 = s1 + "adc"  //   s1字符串后加個字符串"abc"賦值給s2 
s1 = s2;                    //   將s2字符串賦值給s1 
s1 == s2;                   //   判斷s2字符串和s1字符串是否相等
!=,<,<=,>,>=                //   兩個字符串的比較

3.string對象中字符的處理(頭文件cctype

isalnum(c);   //如果c是字母或者數字,返回true 
isalpha(c);   //如果c是字母,返回true 
iscntrl(c);   //如果c是控制字符,返回true 
isdigit(c);   //如果c是數字,返回true 
isgraph(c);   //如果c不是空格,可打印,返回true 
islower(c);   //如果c是小寫字母,返回true 
isupper(c);   //如果c是大寫字母,返回true
isprint(c);   //如果c是可打印字母,返回true
ispunct(c);   //如果c是標點符號,返回true
isspace(c);   //如果c是空白字符,返回true
isxdigit(c);  //如果c是十六進制數,返回true
tolower(c) ;  //如果c是大寫字母,返回小寫字母 
toupper(c);   //如果c是小寫字母,返回大寫字母

4.函數操作

1)assign():

s.assign(base);                           //將base賦值給s 
s.assign(base,10,9);                      //將base第10字符已經后9個字符賦值給s 
s.assign("you are so beautiful",7);       //將字符串中前0-6個字符賦值給s 
s.assign("you are so beautiful");         //將字符賦值給s 
s.assign(10,'*');                         //將10個*賦值給s 
s.assign<int>(10,0x2D);                   //賦值10個-給s 
s.assign(base.begin()+16,base.end()-12);  //將base迭代器中指定位置的字符賦給s 

2)insert():? ? ?

  string str="to be question";string str2="the ";string str3="or not to be";string::iterator it;// used in the same order as described above://從0開始計數 str.insert(6,str2);                            // 將字符串str2插入到str的第六個位置(str[5]) str.insert(6,str3,3,4);                        // 將str3第3,4,5,6四個字符插入到str第六個位置處 str.insert(10,"that is cool",8);               // 將"that is cool"字符串中前八個字符插入到str第10個位置處 str.insert(10,"to be ");                       // 將"to be "插入到str第10個位置處 str.insert(15,1,':');                          //  插入一個,到str的第15個位置處 it = str.insert(str.begin()+5,',');            //  將,插入到字符串開頭的第五個位置  cout << *it << endl;                           //it指向的是, str.insert (str.end(),3,'.');                  //  在字符串末尾插入3個. str.insert (it+2,str3.begin(),str3.begin()+3); //將str3前3個字符插入到,后面的第2個位置 

?

3)find():? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //字符串查找,若存在返回下標。

4)replace():? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //替換字符

5)erase():? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //刪除字符

6)append(),+=,push_back():? ? ? ? ? ? ? ? ? ? ? ?//在尾部添加字符

7)compare(),==,!=,<,<=,>,>=:? ? ? ? //字符串的比較

8)reverse():? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //保留一定量內存以容納一定數量的字符

9)substr():? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //返回某個字符串a)?

10) swap()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //交換兩個字符串的內容

11) clear()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//刪除全部字符?

12) +? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//串聯字符串

13) size(),length()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //返回字符數量

14) max_size()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //返回字符的可能最大個數

15) empty()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//判斷字符串是否為空

16) capacity()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//返回重新分配之前的字符容量

17) [ ], at()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //存取單一字符

18) >>,getline()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //從stream讀取某值

19) <<? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //將值寫入stream

20) copy()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//將某值賦值為一個C_string

21) c_str()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //將內容以C_string返回

22) data()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //將內容以字符數組形式返回

23) substr()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//返回某個子字符串

24)begin() end()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //提供類似STL的迭代器支持

25) rbegin() rend()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //逆向迭代器

26) get_allocator()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //返回配置器

(未完待續)(以后需要什么再查官方文檔)
http://www.cplusplus.com/reference/string/string/

?

轉載于:https://www.cnblogs.com/wanghao-boke/p/10440836.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/385136.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/385136.shtml
英文地址,請注明出處:http://en.pswp.cn/news/385136.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

652. 尋找重復的子樹

給定一棵二叉樹&#xff0c;返回所有重復的子樹。對于同一類的重復子樹&#xff0c;你只需要返回其中任意一棵的根結點即可。 兩棵樹重復是指它們具有相同的結構以及相同的結點值。 示例 1&#xff1a; 1 / \ 2 3 / / \ 4 2 4 / 4 …

817. 鏈表組件

給定一個鏈表&#xff08;鏈表結點包含一個整型值&#xff09;的頭結點 head。 同時給定列表 G&#xff0c;該列表是上述鏈表中整型值的一個子集。 返回列表 G 中組件的個數&#xff0c;這里對組件的定義為&#xff1a;鏈表中一段最長連續結點的值&#xff08;該值必須在列表 G…

1121 Damn Single (25 分)

"Damn Single (單身狗)" is the Chinese nickname for someone who is being single. You are supposed to find those who are alone in a big party, so they can be taken care of. Input Specification: Each input file contains one test case. For each case,…

1124 Raffle for Weibo Followers (20 分)

John got a full mark on PAT. He was so happy that he decided to hold a raffle&#xff08;抽獎&#xff09; for his followers on Weibo -- that is, he would select winners from every N followers who forwarded his post, and give away gifts. Now you are suppose…

987. 二叉樹的垂序遍歷

給定二叉樹&#xff0c;按垂序遍歷返回其結點值。 對位于 (X, Y) 的每個結點而言&#xff0c;其左右子結點分別位于 (X-1, Y-1) 和 (X1, Y-1)。 把一條垂線從 X -infinity 移動到 X infinity &#xff0c;每當該垂線與結點接觸時&#xff0c;我們按從上到下的順序報告結點的值…

28. 實現 strStr()

實現 strStr() 函數。 給定一個 haystack 字符串和一個 needle 字符串&#xff0c;在 haystack 字符串中找出 needle 字符串出現的第一個位置 (從0開始)。如果不存在&#xff0c;則返回 -1。 示例 1: 輸入: haystack "hello", needle "ll" 輸出: 2 示例…

1136 A Delayed Palindrome (20 分)

Consider a positive integer N written in standard notation with k1 digits a?i?? as a?k???a?1??a?0?? with 0 for all i and a?k??>0. Then N is palindromic if and only if a?i??a?k?i?? for all i. Zero is written 0 and is also palindrom…

1044 火星數字 (20 分)

火星人是以 13 進制計數的&#xff1a; 地球人的 0 被火星人稱為 tret。地球人數字 1 到 12 的火星文分別為&#xff1a;jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec。火星人將進位以后的 12 個高位數字分別稱為&#xff1a;tam, hel, maa, huh, tou, kes, he…

43. 字符串相乘

給定兩個以字符串形式表示的非負整數 num1 和 num2&#xff0c;返回 num1 和 num2 的乘積&#xff0c;它們的乘積也表示為字符串形式。 示例 1: 輸入: num1 "2", num2 "3" 輸出: "6" 示例 2: 輸入: num1 "123", num2 "456&qu…

1045 快速排序 (25 分)

著名的快速排序算法里有一個經典的劃分過程&#xff1a;我們通常采用某種方法取一個元素作為主元&#xff0c;通過交換&#xff0c;把比主元小的元素放到它的左邊&#xff0c;比主元大的元素放到它的右邊。 給定劃分后的 N 個互不相同的正整數的排列&#xff0c;請問有多少個元…

1049 數列的片段和 (20 分)

給定一個正數數列&#xff0c;我們可以從中截取任意的連續的幾個數&#xff0c;稱為片段。例如&#xff0c;給定數列 { 0.1, 0.2, 0.3, 0.4 }&#xff0c;我們有 (0.1) (0.1, 0.2) (0.1, 0.2, 0.3) (0.1, 0.2, 0.3, 0.4) (0.2) (0.2, 0.3) (0.2, 0.3, 0.4) (0.3) (0.3, 0.4) (0…

C++ Priemer目錄索引

序號內容1 【C Primer | 15】虛函數表剖析&#xff08;一&#xff09; 2 【C Priemr | 15】虛函數表剖析&#xff08;二&#xff09; 3 【C Priemr | 15】虛函數表剖析&#xff08;三&#xff09; 4一個C程序執行main函數前和執行完main函數后會發生什么。1 【C Priemr | 15】虛…

1046 劃拳 (15 分)

劃拳是古老中國酒文化的一個有趣的組成部分。酒桌上兩人劃拳的方法為&#xff1a;每人口中喊出一個數字&#xff0c;同時用手比劃出一個數字。如果誰比劃出的數字正好等于兩人喊出的數字之和&#xff0c;誰就贏了&#xff0c;輸家罰一杯酒。兩人同贏或兩人同輸則繼續下一輪&…

多線程順序交替打印ABCD

題目&#xff1a;按照 ABCD的順序交替打印。 1. 測試代碼&#xff1a; #include <iostream> #include <unistd.h> #include <stdlib.h> #include <pthread.h> using namespace std;struct {int t;pthread_mutex_t mutex;pthread_cond_t cond; } tes…

第一個只出現一次的字符

在一個字符串(0<字符串長度<10000&#xff0c;全部由字母組成)中找到第一個只出現一次的字符,并返回它的位置, 如果沒有則返回 -1&#xff08;需要區分大小寫&#xff09;. 解法&#xff1a; class Solution { public:int FirstNotRepeatingChar(string str) {unordered…

《Leetcode》目錄

序號題目題解標記1 43. 字符串相乘 字符串2513. 找樹左下角的值二叉樹3 450. 刪除二叉搜索樹中的節點 二叉樹486. 分隔鏈表鏈表155 155. 最小棧 C題解棧77. 組合C題解回溯算法15.三數之和C題解

一個C++程序執行main函數前和執行完main函數后會發生什么。

總結&#xff1a; main函數執行之前&#xff0c;主要就是初始化系統相關資源&#xff1a; 設置棧指針初始化static靜態和global全局變量&#xff0c;即data段的內容將未初始化部分的賦初值&#xff1a;數值型short&#xff0c;int&#xff0c;long等為0&#xff0c;bool為FALS…

1144 The Missing Number (20 分)

Given N integers, you are supposed to find the smallest positive integer that is NOT in the given list. Input Specification: Each input file contains one test case. For each case, the first line gives a positive integer N (≤10?5??). Then N integers are…

【面試寶典 | 01】面經

字節跳動提前批后端第三面涼經該來的終究會來的

1148 Werewolf - Simple Version (20 分)

Werewolf&#xff08;狼人殺&#xff09; is a game in which the players are partitioned into two parties: the werewolves and the human beings. Suppose that in a game, player #1 said: "Player #2 is a werewolf.";player #2 said: "Player #3 is a h…