1. 程式人生 > >c++之標準庫型別string

c++之標準庫型別string

標準庫型別string 表示可變長的字元序列,使用string型別必須首先包含string標頭檔案。

#include <string>
using namesapce std;
  • 讀取未知數量的物件
int main()
{
    string word;
    while(cin << word)//使用一個istream物件作為條件,其效果是檢查流的狀態。當遇到檔案結束符或無效輸入時,條件變為假。
        cout << word << endl;
    return 0;
}
  • 使用getline讀取一整行
int
main() { string line; //函式從給定的的輸入流中讀入內容,直到遇到換行符為止 while (getline(cin, line)) //每次讀入一整行,遇到空行跳過 if(!line.empty()) //只輸出長度超過80個字元的行 //if(line.size() > 80) cout << line << endl; return 0; }
  • 處理每個字元?使用基於範圍的for迴圈
for(定義一個變數 : 一個string物件)//該變數被用於訪問序列中的基礎元素
語句;
//使用for語句和ispunct()來統計string物件中標點符號的個數
string s("Hllo World!!!");
//關鍵字decltype宣告計數變數punct_num的型別為s.size()返回的型別(也就是string::size_type)
decltype(s.size()) punct_num = 0;
for(auto c : s)
    if (ispunct(c))
        ++punct_num;
cout << punct_num << endl;
  • 使用for語句改變字串中的字元
string s("Hello World!!!"
) for(auto &c : s) //c是引用將改變s中字元的值 c = toupper(c); cout << s << endl;
  • 只處理一部分字元?
//將字串首字母改成大寫形式
string s("some string");
if (!s.empty())
    s[0] = toupper(s[0])
//將第一個詞改成大寫形式
for (decltype(s.size()) index = 0;
    index !=s.size() && !isspace(s[index]);++index)
        //將當前字元改成大寫
        s[index] = toupper(s[index]);

相關推薦

c++標準型別string

標準庫型別string 表示可變長的字元序列,使用string型別必須首先包含string標頭檔案。 #include <string> using namesapce std; 讀取未知數量的物件 int main() {

c++_標準型別string

一  標準庫型別 string 是可變長的字元序列,使用string型別需要: #include<string> using namespace std;  or   using std::string; 二 定義與初始化             直接初始化

C++標準型別-string

1:首先區分開C和C++中的string 解釋:C語言中的並沒有string型別(C語言連類都沒,當然沒有string),C中的有個string.h標頭檔案,這個標頭檔案只是包含對字串的處理,比如說strcat等函式。而C++中的string是一個標準庫型別,表示可變長的字元序列,而由於是標準庫

《隨筆十八》—— C++中的 “ 標準型別string

  目錄 初始化 string 物件的方式 string 物件的操作 cin 和 getline  讀寫 string物件 string 的 size ()操作 和  string::size_type 型別 比較string物件

C++primer筆記-第3章 字串、變數和陣列(標準型別string

 標準庫string、vector都是對內建陣列型別的抽象,string支援變長的字串,vector表示可變長的集合。迭代器是string和vector的配套型別,常用於訪問string或者vector中的元素。 string作為標準庫的一部分,定義在名稱空間s

C++中的標準型別——string

string  標準庫型別string是一種可變長的字元序列。string定義在std名稱空間中。在使用string前,需要: #include<string> using std

C++基礎程式設計----2.2標準型別string

                                   標準庫型別string 標準庫型別string是一個可變長的字元序列。 使用string型別必須包含string標頭檔案,stri

標準型別String用法總結(1)

標頭檔案及名稱空間 #include<string> using std::string 定義及初始化string物件 string s1; //預設初始化,s1是一個空串 string s2(s1);//s2是s1的副本 string s2=s1;//等價於s2(

(12)標準型別string

標準庫型別string表示可變長的字元序列,使用string型別必須首先包含string標頭檔案。作為標準庫的一部分,string定義在名稱空間std中。如: #include<string> using std::string; 定義和初始化string物件

11 標準型別 string 初識

1. 定義和初始化 string 物件 #include <iostream> using namespace std; using std::string; /** * 標準庫型別 string */ int main() {

筆記2 (標準型別string、vector和bitset)

抽象資料型別(abstract data type) 通常,標頭檔案中應該只定義確定必要的東西 標準庫string 型別 #include<string> using  std::string; string的定義和初始化 string型別的輸入操作符:

c++基礎五---標準型別string ,vector)

string型別 支援長度可變的字串 因為歷史原因以及為了與C語言相容,字串字面值與標準庫string型別不是同一種類型。這一點很容易引起混亂。 s.empty ()    如果s為空串,則返回ture s.size() s[n]    返回s中位置為n的字元 size(

C++ Primer學習筆記- 第三章:標準型別

四、標準庫bitset型別 標準庫中bitset型別用來處理二進位制位的有序集,bitset型別簡化了位集的處理,使用bitset時需要包含標頭檔案#include<bitset>     bitset物件的定義和初始化 bitset也是類模板,不過bits

C++ Primer】【學習筆記】【第三章】標準型別:bitset型別

#include <iostream> #include <bitset> using namespace std; int main() { bitset<32> bitvec; int a = 0, b = 1; i

C++ PRIMER3 標準型別

1 using 宣告 一次只能用於一個名稱空間 儘量不要在標頭檔案中使用 2 string 型別 #include<string> 構造: string s1("test") s2(n, 'c') getline 獲取一行 直到換行符 重要操作: empty

C++Primer_Chap17_標準特殊設施_List02_bitset型別_筆記

  定義在標頭檔案bitset中的bitset類使位運算的使用更為容器,並且能夠處理超過最長整型型別大小的位集合。 #include <bitset> 定義和初始化bitset   當我們定義一個bitset時,需要宣告它包含多少個二進位制位。 bit

C++Primer_Chap17_標準特殊設施_List01_tuple型別_筆記

  tuple是類似pair的模板。不同tuple型別的成員型別可以不同,但一個tuple可以有任一數量的成員。每個確定的tuple型別的成員數目是固定的。但一個tuple型別的成員數目可以與另一個tuple型別不同。   當我們希望將一些資料組合成單一物件,但又不

C++標準程式

標準程式庫 C++中的標準程式庫是類庫和函式的集合,其使用核心語言寫成。標準程式庫提供若干泛型容器、函式物件、泛型字串和流(包含互動和檔案I/O),支援部分語言特性和常用的函式,如開平方根。C++標準程式庫也吸收了ISO C90 C標準程式庫。標準程式庫的特性聲明於std名稱空間之中。

C++ primer讀書筆記 chapter3 標準型別

除第二章介紹的是C++的基本型別,本章將大致介紹一下C++定義的內容豐富的抽象資料庫型別標準庫。著重介紹一下sting、vector和bitset。 3.2標準庫string型別   1.string型別支援幾個建構函式。建構函式是一個特殊成員函式,定義如何初始化該型別的物件,以下是stri

C/C++】標準 numeric

顧名思義, numeric 是一個用於數值計算的小庫. Generalized numeric operations This header describes a set of algorithms to perform certain operation