關於string類的建構函式及部分方法
1、建構函式
string(const char* s); 將string物件初始化為s字串
string(size_type n,char c); 建立一個包含N個元素的string物件,其中每個元素都為c
string(const string & str);將一個string物件初始化為string物件str(複製構造)
string()建立一個預設的string物件,長度為0(預設構造)
string(const char *s ,size_type n)將string物件初始化為S指向的前n個字元
template<class Iter> string(Iter begin,Iter end) 將string物件初始化為區間 begin end內的字元,其中begin和end類似指標,用於指定位置,包含begin不包含end
string(const string &str,string size_type pos=0,size_type n=npos)將一個string物件初始化為物件sr中從位置pos開始到結尾的字元 npos為最大位置,或從位置位置開始的n個字元
#include<iostream>
using namespace std;
int main(void)
{
char tests[]="teststr";
string a("test1");
string b(10,'a');
string c(a);
string d;
string e("test5",4);
string f(tests+0,tests+5);
string g(a,1,3);
cout<<a<<endl<<b<<endl<<c<<endl<<d<<endl<<e<<endl<<f<<endl<<g<<endl;
}
2、讀取getline string的getline是獨立的函式(友元?)
24 string name1;
25 string name2;
26 getline(cin,name1,':');
27 getline(cin,name2);
28 cout<<name1<<endl<<name2<<endl;
其特點
1、到達檔案尾,eofbit設定,fail()和eof()設定為ture;
2、遇到分界符(預設為\n),在輸入流中刪除分解字元,不儲存它
3、讀取字元數達到最大值(string::npos和克分配記憶體位元組數中較小的一個),在這種情況下
將設定輸入流的failbit,這說明fail()返回為ture;
string類過載了>>,當遇到空白字元就停止如空格、換行、製表符。
3、比較
string類過載了 < == != > 等比較運算子,可以實現
C字串和string之間的比較,當然也可以string和sring進行比較
如:
int main(void)
{
string a("abc");
string b("abc");
char c[20]="abm";
char d[20]="abc";
if(a == b)
{
cout<<"a b Y"<<endl;
}
if(a != c)
{
cout<< "a c N"<<endl;
}
if(b == d)
{
cout<<"b d Y"<<endl;
}
}
4、find方法
string::npos 是字串可以儲存的最大字元,通常是無符號的int或者無符號的long的最大值
--size_type find(const string& str,size_type pos=0)const
從字串的pos位置開始,查詢子字串str。如果找到,則返回字串首次出現時其首字元的索引,否則返回string::npos
--size_type find(const char* s,size_type pos = 0) const
從字串的pos位置開始,查詢子字串s,如果找到,則返回該字串首次出現時其首字元的索引,否則返回string::npos
--size_type find(const char* s,size_type pos=0,size_type n)
從字串的pos位置開始,查詢s的前N個字元組成的子字串,如果找到,則返回該子字串首次出現是其字元的索引,否則返回sting::npos
--size_type find(char ch,size_type pos=0) const
從字串的pos位置開始,查詢字元ch,如果找到則返回該字元首次出現的位置,否則返回string::npos
如:
string b = "test";
int n = b.find('s',1);
cout<<n<<endl;
5、capacity(),size(),reserve(n)
capacity() 檢視string的記憶體容量位元組
size() 檢視string實際資料大小位元組
reserve(n) 將string的記憶體容量更改為指定的n位元組
如:
string b = "test";
string c;
string d = "ttttttttttttttttttttttttttt";
cout<<b.capacity()<<endl; //輸出4
cout<<b.size()<<endl; //輸出4
cout<<c.capacity()<<endl; //輸出0
cout<<c.size()<<endl; //輸出0
cout<<d.capacity()<<endl; //輸出27
cout<<d.size()<<endl; //輸出27
c.reserve(50); //設定string c記憶體為50位元組
cout<<c.capacity()<<endl; //輸出50
cout<<c.size()<<endl; //輸出0
6、c_str()
c_str() 返回一個c const char*型別指標,這一點非常有用
如
#include<iostream>
using namespace std;
int main(void)
{
string c ="test";
const char* b;
b = c.c_str();
cout<<b<<endl;
}
string(const char* s); 將string物件初始化為s字串
string(size_type n,char c); 建立一個包含N個元素的string物件,其中每個元素都為c
string(const string & str);將一個string物件初始化為string物件str(複製構造)
string()建立一個預設的string物件,長度為0(預設構造)
string(const char *s ,size_type n)將string物件初始化為S指向的前n個字元
template<class Iter> string(Iter begin,Iter end) 將string物件初始化為區間 begin end內的字元,其中begin和end類似指標,用於指定位置,包含begin不包含end
string(const string &str,string size_type pos=0,size_type n=npos)將一個string物件初始化為物件sr中從位置pos開始到結尾的字元 npos為最大位置,或從位置位置開始的n個字元
#include<iostream>
using namespace std;
int main(void)
{
char tests[]="teststr";
string a("test1");
string b(10,'a');
string c(a);
string d;
string e("test5",4);
string f(tests+0,tests+5);
string g(a,1,3);
cout<<a<<endl<<b<<endl<<c<<endl<<d<<endl<<e<<endl<<f<<endl<<g<<endl;
}
2、讀取getline string的getline是獨立的函式(友元?)
24 string name1;
25 string name2;
26 getline(cin,name1,':');
27 getline(cin,name2);
28 cout<<name1<<endl<<name2<<endl;
其特點
1、到達檔案尾,eofbit設定,fail()和eof()設定為ture;
2、遇到分界符(預設為\n),在輸入流中刪除分解字元,不儲存它
3、讀取字元數達到最大值(string::npos和克分配記憶體位元組數中較小的一個),在這種情況下
將設定輸入流的failbit,這說明fail()返回為ture;
string類過載了>>,當遇到空白字元就停止如空格、換行、製表符。
3、比較
string類過載了 < == != > 等比較運算子,可以實現
C字串和string之間的比較,當然也可以string和sring進行比較
如:
int main(void)
{
string a("abc");
string b("abc");
char c[20]="abm";
char d[20]="abc";
if(a == b)
{
cout<<"a b Y"<<endl;
}
if(a != c)
{
cout<< "a c N"<<endl;
}
if(b == d)
{
cout<<"b d Y"<<endl;
}
}
4、find方法
string::npos 是字串可以儲存的最大字元,通常是無符號的int或者無符號的long的最大值
--size_type find(const string& str,size_type pos=0)const
從字串的pos位置開始,查詢子字串str。如果找到,則返回字串首次出現時其首字元的索引,否則返回string::npos
--size_type find(const char* s,size_type pos = 0) const
從字串的pos位置開始,查詢子字串s,如果找到,則返回該字串首次出現時其首字元的索引,否則返回string::npos
--size_type find(const char* s,size_type pos=0,size_type n)
從字串的pos位置開始,查詢s的前N個字元組成的子字串,如果找到,則返回該子字串首次出現是其字元的索引,否則返回sting::npos
--size_type find(char ch,size_type pos=0) const
從字串的pos位置開始,查詢字元ch,如果找到則返回該字元首次出現的位置,否則返回string::npos
如:
string b = "test";
int n = b.find('s',1);
cout<<n<<endl;
5、capacity(),size(),reserve(n)
capacity() 檢視string的記憶體容量位元組
size() 檢視string實際資料大小位元組
reserve(n) 將string的記憶體容量更改為指定的n位元組
如:
string b = "test";
string c;
string d = "ttttttttttttttttttttttttttt";
cout<<b.capacity()<<endl; //輸出4
cout<<b.size()<<endl; //輸出4
cout<<c.capacity()<<endl; //輸出0
cout<<c.size()<<endl; //輸出0
cout<<d.capacity()<<endl; //輸出27
cout<<d.size()<<endl; //輸出27
c.reserve(50); //設定string c記憶體為50位元組
cout<<c.capacity()<<endl; //輸出50
cout<<c.size()<<endl; //輸出0
6、c_str()
c_str() 返回一個c const char*型別指標,這一點非常有用
如
#include<iostream>
using namespace std;
int main(void)
{
string c ="test";
const char* b;
b = c.c_str();
cout<<b<<endl;
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7728585/viewspace-2122602/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 關於建構函式與解構函式的分享函式
- 關於類的建構函式不能為虛擬函式的幾點理由!函式
- 類的建構函式和解構函式函式
- 關於scala中的主建構函式函式
- 關於建構函式的一點理解函式
- 子類繼承父類的建構函式(方法)嗎?繼承函式
- C++ 類建構函式和解構函式C++函式
- 關於python建構函式的過載Python函式
- 關於function建構函式特別注意的Function函式
- JS 建構函式與類JS函式
- 19-父類的建構函式函式
- flutter-dart 類的建構函式FlutterDart函式
- C++語言之結構體、類、建構函式、拷貝建構函式C++結構體函式
- Python中,類的特殊方法與內建函式的關聯Python函式
- 在 C++ 中子類繼承和呼叫父類的建構函式方法C++繼承函式
- 在C++中子類繼承和呼叫父類的建構函式方法C++繼承函式
- 父類和子類的建構函式問題函式
- javascript關於Array()建構函式引數注意點JavaScript函式
- 構建一個類jq的函式庫函式
- 關於oracle內建函式的使用Oracle函式
- 關於C++中建構函式的常見疑問C++函式
- 建構函式、原型及原型鏈函式原型
- 關於C#中建構函式的繼承的問題C#函式繼承
- C++再議建構函式及複製建構函式深度複製C++函式
- 談談Fragment的建構函式、重新建立(recreate)及相關Fragment函式
- python的部分內建函式Python函式
- C++:建構函式的分類和呼叫C++函式
- dart系列之:dart類中的建構函式Dart函式
- Golang建立建構函式的方法詳解Golang函式
- c# 通過建構函式及傳入類物件方法引數來複制物件C#函式物件
- 建構函式顯式返回 this 在 new 運算及 call 方法中的比較函式
- 建構函式與解構函式函式
- python中關於列表的一些內建方法(函式)和操作(部分需要理解的會給出例子)Python函式
- 關於Java建構函式(Constructor)的常見問題總結Java函式Struct
- ## 建構函式函式
- 建構函式函式
- 關於C++類方法函式指標的定義和使用方法C++函式指標
- Java的建構函式Java函式