C++:字符串string

標準庫類型string表示可變長的字符序列,其定義在頭文件<string>中。作爲標準庫的一部分,string定義在命名空間std中。要使用string類型,需在程序中包含如下代碼:1)using namespace std;,或2)using std::string;

定義和初始化string對象

如何初始化類的對象是由類本身決定的。一個類可以定義很多初始化對象的方式,只不過這些方式之間必須有所區別:或者是初始值的數量不同,或者是初始值的類型不同。

初始化string對象的方式:

string s1;         //默認初始化一個string對象,s1是一個空字符串
string s2(s1);     //s2是s1的副本
string s2=s1;      //等價於s2(s1),s2是s1的副本

string s3("hello");//s3是字符串字面值"hello"的副本,除了字面值最後的那個空字符外
string s3="hello"; //等價於s3("hello"),s3是字面值"hello"的副本

string s4(n,'c');  //把s4初始化爲由連續n個字符c組成的串
string s4(10,'c'); //s4的內容是10個c:cccccccccc

string對象上的操作

一個類除了要規定初始化其對象的方式外,還要定義對象上所能執行的操作。其中,類既能定義通過函數名調用的操作,就像Sales_item類的isbn函數那樣,也能定義"<<、+"等各種運算符在該對象上的新含義。

string的操作

os<<s         //將s寫到輸出流os當中,返回os
is>>s         //從is中讀取字符串賦給s,字符串以空白分隔,返回is
getline(is,s) //從is中讀取一行賦給s,返回false
s.empty()     //s爲空返回true,否則返回false
s.size()      //返回s中字符的個數
s[n]          //返回s中第n個字符的引用,位置n從0計起
s1+s2         //返回s1和s2連接後的結果
s1=s2         //用s2的副本代替s1中原來的字符
s1==s2        //如果s1和s2中所含的字符完全一樣,則它們相等;
              //string對象的相等性判斷對字母的大小寫敏感
s1!=s2
<,<=,>,>=     //利用字符在字典中的順序進行比較,且對字母的大小寫敏感

兩個string對象相加

兩個string對象相加得到一個新的string對象,其內容是把左側的運算對象與右側的運算對象串接而成。也就是說,對string對象使用加法運算符(+)的結果是一個新的string對象,它所包含的字符由兩部分組成:前半部分是加號左側string對象所含的字符、後半部分是加號右側string對象所含的字符。另外,複合賦值運算符(+=)負責把右側string對象的內容追加到左側string對象的後面。

string s1="hello,",s2="world!\n";
string s3=s1+s2; //s3的內容是hello,world\n
s1+=s2;          //等價於s3=s1+s2

cout<<"s3:"<<s3<<endl;
cout<<"s1:"<<s1<<endl;

爲string對象賦值

一般來說,在設計標準庫類型時都力求在易用性上向內置類型看齊,因此大多數庫類型都支持賦值操作。對於string類而言,允許把一個對象的值賦給另外一個對象。

//S1的內容是10個c:cccccccccc,s2是一個空字符串
string s1(10,'c'),s2;

cout<<"s1:"<<s1<<endl;
cout<<"s2:"<<s2<<endl;

//賦值:用s2的副本替換s1的內容,此時s1和s2都是空字符串
s1=s2;

cout<<"s1:"<<s1<<endl;
cout<<"s2:"<<s2<<endl;

字符串字面值和string對象相加

把數值類型轉換爲字符串string類型

(1)使用stringstream

要使用stringstream類型,需在程序中包含如下代碼:

#include <sstream>
using namespace std;

代碼清單

double a = 123.32;
string res;
stringstream ss; //定義流ss
ss << a;         //將數字a轉化成流ss
ss >> res;       //將流ss轉化成字符串

cout<<res<<endl;

(2)使用函數模板+ostringstream

使用函數模板將內置數據類型(整型、字符型、實型、布爾型)轉換成string。

代碼清單

//ostringstream對象用來進行格式化的輸出,常用於將各種類型轉換爲string類型
//ostringstream只支持<<操作符
template<typename Type> 
string toString(const Type& t){
    ostringstream oss;  //創建一個格式化輸出流
    oss<<t;             //把值傳遞如流中
    return oss.str();  
}
 
cout<<toString(14.2)<<endl;  //實型->string:輸出14.2
cout<<toString(12301)<<endl; //整型->string:輸出12301
cout<<toString(123456789785)<<endl;//長整型->string:輸出123456789785
cout<<toString(true)<<endl;  //布爾型->string:輸出1

(3)使用標準庫函數std::to_string()

std命令空間下有一個C++標準庫函數std::to_string(),可用於將數值類型轉換爲string。使用時需在程序中包含如下代碼:using namespace std;

to_string()函數的原型如下:

string to_string (int val);
string to_string (long val);
string to_string (long long val);
string to_string (unsigned val);
string to_string (unsigned long val);
string to_string (unsigned long long val);
string to_string (float val);
string to_string (double val);
string to_string (long double val);

代碼清單

  string s;int val=100;
  s=to_string (val);

  cout<<s<<endl;

把string類型字符串轉換爲數值類型

(1)使用函數模板+istringstream

代碼清單

#include <iostream>
#include <sstream> //使用stringstream需要引入這個頭文件
using namespace std;

//模板函數:將string類型變量轉換爲常用的數值類型(此方法具有普遍適用性)
template <class Type>
Type stringToNum(const string& str){
    istringstream iss(str);
    Type num;
    iss >> num;
    return num;
}

int main(int argc, char* argv[]){
    string str("00801");
    int a=stringToNum<int>(str);
    cout << a << endl;

    return 0;
}

(2)使用C++標準庫函數

使用C++11引入的C++庫函數將string轉換爲數值類型,相應的庫函數申明於頭文件<string>中。

名稱函數原型描述
stoi int stoi (const string& str, size_t* idx = 0, int base = 10);
int stoi (const wstring& str, size_t* idx = 0, int base = 10);
Convert string to integer (function template )
stol long stol (const string& str, size_t* idx = 0, int base = 10);
long stol (const wstring& str, size_t* idx = 0, int base = 10);
Convert string to long int (function template)
stoul unsigned long stoul (const string& str, size_t* idx = 0, int base = 10);
unsigned long stoul (const wstring& str, size_t* idx = 0, int base = 10);
Convert string to unsigned integer (function template)
stoll long long stoll (const string& str, size_t* idx = 0, int base = 10);
long long stoll (const wstring& str, size_t* idx = 0, int base = 10);
Convert string to long long (function template)
stoull unsigned long long stoull (const string& str, size_t* idx = 0, int base = 10);
unsigned long long stoull (const wstring& str, size_t* idx = 0, int base = 10);
Convert string to unsigned long long (function template)
stof float stof (const string& str, size_t* idx = 0);
float stof (const wstring& str, size_t* idx = 0);
Convert string to float (function template )
stod double stod (const string& str, size_t* idx = 0);
double stod (const wstring& str, size_t* idx = 0);
Convert string to double (function template )
stold long double stold (const string& str, size_t* idx = 0);
long double stold (const wstring& str, size_t* idx = 0);
Convert string to long double (function template)

形參說明:
str:重載了string和wstring版本,表示被轉換的字符串。

idx:表示一個size_t*的指針類型,默認爲空值。不爲空時,轉換成功時獲取第一個非數值字符的下標。一般情況下,因爲它是直接char型指針把最後非數值字符的地址值和起始地址值相減,所以也表示成功轉換的字符數量,如”10”轉成功爲數值10時,*idx的值爲2。

base:表示轉換基準,默認是10進制。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章