C 標準型別庫string

2021-07-06 04:41:52 字數 680 閱讀 7857

string初始化方式:

int main()

getline輸出一整行

getline:引數(輸入流,string物件),讀取給定內容,遇到換行符停止(換行符也被讀取進去了),它返回乙個流的引數。

#include#includeusing namespace std;

int main()

{ string s;

while(cin>>s)

{cout《當輸入string字元時,遇到空格就停止了,得到一整行(包含空格的)就要用到getline函式,**中注釋部分就是。

string的size和empty函式,乙個返回個數,乙個返回是否為空。呼叫形式:s.size()   ,  s.empty() ,其中size函式返回乙個string::size_type型別的無符號數。

處理string物件中的每個字元,使用基於範圍的for語句來遍歷每個元素,並對其進行操作(改變要定義引用),處理一部分字元則要用到下標。

#include#includeusing namespace std;

int main()

{ string s="pan";

for(auto &c : s)//c的型別不確定,則用auto

{c='a';//使每個字元都變為a

cout《列印:aaa

hbha

string標準庫型別 C

c 中string的學習體會 string 1 不允許把兩個字串字面值連線起來,乙個string物件 字串字面值返回的是string物件.string size type只是string裡方便移植性的定義的一種型別 2 cout include using namespace std int mai...

C 標準庫string型別

c 組成 基本資料型別和抽象資料型別標準庫 只需知道抽象資料型別支援的操作而不需關心內部表示 命名空間兩種使用方法 using std name 和 using namespace std 標準庫string型別和字串字面值不是同一型別 具體區別?getline 函式 string line get...

C 標準庫string型別

標準庫的string型別 include include using namespace std 或者可以這樣 using std string using std cin using std cout int main 12.下標操作可用作左值 string str some string for...