最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

一文搞懂C++中string容器的構(gòu)造及使用

 更新時(shí)間:2022年07月11日 09:21:26   作者:葉落秋白  
本文小編將帶大家學(xué)習(xí)最常見的一個(gè)引用類型——string容器,學(xué)習(xí)string容器的構(gòu)造、以及C++API中String類的常用方法,感興趣的可以了解一下

string容器

string基本概念

本質(zhì):

string是c++風(fēng)格的字符串,不同于c語(yǔ)言的 char*,他本質(zhì)是一個(gè)類

string 和 char*的區(qū)別:

char*是一個(gè)指針

string是一個(gè)類,類內(nèi)部封裝了char*來(lái)管理字符串,是一個(gè)char&型的容器

特點(diǎn):

strint類內(nèi)部封裝了很多成員方法

例如:查找find,拷貝copy,刪除delete,替換replace,插入insert

string管理char*所分配的內(nèi)存,不用考慮賦值越界和取值越界等問(wèn)題,由類內(nèi)部進(jìn)行負(fù)責(zé)

string構(gòu)造函數(shù)

四種函數(shù)原型

  • string()創(chuàng)建一個(gè)空的字符串
  • string(const char* s)使用字符串s初始化
  • string(const string& str)使用一個(gè)string對(duì)象初始化另一個(gè)string對(duì)象
  • string(int n,char c)使用n個(gè)字符c初始化

使用示例:

//string的構(gòu)造函數(shù)
void test1()
{
    string s1;//默認(rèn)構(gòu)造
    const char* str = "葉落 秋白";
    string s2(str);
    cout << "s2:"<<s2 << endl;
    string s3(s2);
    cout << "s3" << s3 << endl;
    string s4(6,'a');
}

上面就是四個(gè)構(gòu)造方法對(duì)應(yīng)的舉例了,第一種方式是我們頻繁使用的;第二種方式就是設(shè)置不可變字符數(shù)組傳入構(gòu)造來(lái)初始化;第三種方式理解為調(diào)用拷貝構(gòu)造即可;第四種方式就比較有意思了,在上面代碼里的意思就是用6個(gè)'a'來(lái)初始化字符串,輸入s4結(jié)果為:aaaaaa。

string賦值操作

給string字符串賦值

賦值的函數(shù)原型:

  • string& operator = (const char* s)char*類型字符串 賦值給當(dāng)前的字符串
  • string& operator = (const string &s)把字符串s賦給當(dāng)前的字符串
  • string& operator = (char c)把字符賦值給當(dāng)前的字符串
  • string& assign(const char* s)把字符串s賦值給當(dāng)前的字符串
  • string& assign(const char*s,int n)把字符串s的當(dāng)前n個(gè)字符賦給當(dāng)前的字符串
  • string& assign(const string &s)把字符串s賦給當(dāng)前字符串
  • string& assign(int n,char c)用n個(gè)字符c賦給當(dāng)前字符串

使用示例:

void test2()
{
	string str1;
	str1 = "葉落秋白";
	cout << "str1=" << str1 << endl;

	string str2;
	str2 = str1;
	cout << "str2=" << str2 << endl;
	
	string str3;
	str3 = 'c';
	cout << "str3=" << str3 << endl;

	string str4;
	str4.assign("hello c++");
	cout << "str4=" << str4 << endl;

	string str5;
	str5.assign("hello c#",5);
	cout << "str5=" << str5 << endl;
	
	string str6;
	str6.assign(str5);
	cout << "str6=" << str6 << endl;

	string str7;
	str7.assign(6, 'w');
	cout << "str7=" << str7 << endl;
}

tips:stirng賦值方法很多,但是重載的operator=的方式最為常用

string拼接操作

在字符串末尾拼接字符串

函數(shù)原型:

  • string& operator+=(const char* str)重載+=操作符
  • string& operator+=(const char c)重載+=操作符
  • string& operator+=(const string& str)重載+=操作符
  • string& append(const char* s)把字符串s連接到當(dāng)前字符串結(jié)尾
  • string& append(const char* s,int n)把字符串s的前n個(gè)字符連接到當(dāng)前字符串的結(jié)尾
  • string& append(const string &s)同operator+=(const string& str)
  • string& append(const string &s,int pos,int n)把字符串s中從pos開始的n個(gè)字符連接到字符串結(jié)尾

使用示例:

void test3()
{
	string str1 = "紅豆";
	str1 += "憶相思";
	cout << "str1=" << str1<< endl;

	str1 += '?';
	cout << "str1=" << str1 << endl;

	string str2 = "yyds";
	str1 += str2;
	cout << "str1=" << str1 << endl;

	string str3 = "You";
	str3.append("low");
	cout << "str3=" << str3 << endl;

	str3.append("wuwuwu qaq", 4);
	cout << "str3=" << str3 << endl;

	str3.append(str2);
	cout << "str3=" << str3 << endl;

	str3.append(str2, 0, 1);
	cout << "str3=" << str3 << endl;
}

tips:初學(xué)者只需要稍微記幾個(gè)拼接函數(shù)即可

string查找替換

指定位置查找字符串

指定位置刪除字符串

函數(shù)原型:

1.查找s第一次出現(xiàn)位置,從pos開始查找

int find(const string& str, int pos = 0) const;
int find(const char* s , int pos ==0) const;

2.從pos位置查找s的前n個(gè)字符第一次位置

int find( const char* s, int pos, int n) const;

3.查找字符c第一次出現(xiàn)位置

int find(const char c, int pos = e) const;

4.查找str最后一次位置,從pos開始查找

int rfind(const string& str, int pos = npos) const;

5.查找str最后一次位置,從pos開始查找,計(jì)數(shù)永遠(yuǎn)是從前往后

int rfind(const char* s, int pos = npos) const;

6.從pos查找s的前n個(gè)字符最后一次位置

int rfind(const char* s, int pos, int n) const;

7.查找字符c最后一次出現(xiàn)位置

int rfind(const char c, int pos - e) const;

8.替換從pos開始n個(gè)字符為字符串str

string& replace(int pos, int n, const string& str);

9.替換從pos開始的n個(gè)字符為字符串s

string& replace(int pos, int n,const char* s );

使用示例:

//字符串的查找和替換
//查找
void test4()
{
	string str1 = "abcdefgh";
	//找到返回下標(biāo),找不到返回-1
	int pos1 = str1.find("def");
	cout << "pos1=" << pos1 << endl;
	int pos2 = str1.find("s");
	cout << "pos2=" << pos2<< endl;

	pos1 = str1.rfind("ab");//從右往左找到第一個(gè)出現(xiàn),從左往右計(jì)數(shù)
	cout << "pos1=" << pos1 << endl;;
}
//替換
void test5()
{
	string str2 = "abcdef";
	str2.replace(1, 2, "1111");//從1號(hào)位置起,2個(gè)字符替換為1111
	cout << "str2=" << str2 << endl;
}

tips:

find找到字符串后返回查找的第一個(gè)字符位置,找不到返回1

函數(shù)雖然很多,但幾乎都是兩個(gè)版本的,一個(gè)是c++風(fēng)格一個(gè)c語(yǔ)言風(fēng)格

string字符串比較

字符串比較是按字符的ASCII碼進(jìn)行對(duì)比

函數(shù)原型:

int compare(const string &s) const;
int compare(const char* s) const;

使用示例:

string str1 = “zello”;
string str2 = “hello”;
if (str1.compare(str2) == 0)
{
cout << “相等” << endl;
}
else if (str1.compare(str2) > 0)
{
cout << “str1大” << endl;
}
else
{
cout << “str2大” << endl;
}

tips:字符串對(duì)比的目的是比較兩個(gè)字符串是否相等,判斷誰(shuí)大誰(shuí)小的意義并不是很大。

string字符讀取

單個(gè)字符存取有兩種方式:

函數(shù)原型:

char& operator[] (int n); //通過(guò)[]獲取字符
char& at (int n); //通過(guò)at方法獲取字符

使用示例:

string str1 = “hello”;
//通過(guò)[]訪問(wèn)單個(gè)字符
for (int i = 0; i < str1.size(); i++)
{
cout << str1[i] << " ";
}
cout << endl;
//通過(guò)at方式訪問(wèn)的單個(gè)字符
for (int i = 0; i < str1.size(); i++)
{
cout << str1.at(i) << " ";
}
cout << endl;
//修改單個(gè)字符
str1[0] = ‘z';
cout << str1 << endl;
str1.at(0) = ‘x';
cout << str1 << endl;

string插入和刪除

函數(shù)原型:

string& insert(int pos,const cahr* s);//在n位置插入字符串
string& insert(int pos,const string& s);//在n位置插入字符串
string& insert(int pos,int n,char c);//在指定位置插入n個(gè)字符c
string& erase(int pos,int n = npos);//刪除從pos位置開始的n個(gè)字符

使用示例:

string str = “hello”;
//插入
str.insert(1, “111”);
cout << "str = " << str << endl;
//刪除
str.erase(1,3);
cout << "str = " << str << endl;

tips:插入和刪除的起始下標(biāo)都是從0開始。

string求子串

從字符串中得到想要的子串

函數(shù)原型:

string substr(int pos=0,int n=npos) const ;//返回由pos位置開始的由n個(gè)字符組成的字符串

//string求子串
void test01()
{
    string str = "abcdef";
    string subStr = str.substr(1, 3);
    cout << "subStr=" << subStr << endl;
}
//使用操作
void test02()
{
    string email = "ylqb@qq.com";
    //從郵箱地址中獲取用戶名信息
    int pos = email.find("@");
    string usrName = email.substr(0, pos);
    cout << usrName << endl;
}

tips:靈活的運(yùn)用求子串功能,可以在實(shí)際開發(fā)中獲取有效的信息,在上述代碼中就可以有效獲取到不同郵箱中的用戶名。

到此這篇關(guān)于一文搞懂C++中string容器的構(gòu)造及使用的文章就介紹到這了,更多相關(guān)C++ string容器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C++中sort函數(shù)的基礎(chǔ)入門使用教程

    C++中sort函數(shù)的基礎(chǔ)入門使用教程

    這篇文章主要給大家介紹了關(guān)于C++中sort函數(shù)的基礎(chǔ)入門使用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用C++具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧
    2018-12-12
  • c++實(shí)現(xiàn)廣播通訊詳解

    c++實(shí)現(xiàn)廣播通訊詳解

    這篇文章主要為大家詳細(xì)介紹了c++實(shí)現(xiàn)廣播通訊的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以參考一下
    2024-12-12
  • C語(yǔ)言中g(shù)etchar的用法以及實(shí)例解析

    C語(yǔ)言中g(shù)etchar的用法以及實(shí)例解析

    getchar()是stdio.h中的庫(kù)函數(shù),它的作用是從stdin流中讀入一個(gè)字符,下面這篇文章主要給大家介紹了關(guān)于C語(yǔ)言中g(shù)etchar的用法以及實(shí)例的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-03-03
  • Qt?QDateTime計(jì)算時(shí)間差的實(shí)現(xiàn)示例

    Qt?QDateTime計(jì)算時(shí)間差的實(shí)現(xiàn)示例

    本文主要介紹了Qt?QDateTime計(jì)算時(shí)間差的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • 詳解QTreeWidget隱藏節(jié)點(diǎn)的兩種方式

    詳解QTreeWidget隱藏節(jié)點(diǎn)的兩種方式

    本文主要介紹了QTreeWidget隱藏節(jié)點(diǎn)的兩種方式,一種是直接隱藏,一種是間接隱藏,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • stl容器set,map,vector之erase用法與返回值詳細(xì)解析

    stl容器set,map,vector之erase用法與返回值詳細(xì)解析

    在使用 list、set 或 map遍歷刪除某些元素時(shí)可以這樣使用,如下所示
    2013-09-09
  • C++中inline用法案例詳解

    C++中inline用法案例詳解

    這篇文章主要介紹了C++中inline用法案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • C++帶有指針成員的類處理方式詳解

    C++帶有指針成員的類處理方式詳解

    這篇文章主要為大家詳細(xì)介紹了C++帶有指針成員的類處理方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • C++實(shí)現(xiàn)插入排序?qū)φ麛?shù)數(shù)組排序

    C++實(shí)現(xiàn)插入排序?qū)φ麛?shù)數(shù)組排序

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)插入排序?qū)φ麛?shù)數(shù)組排序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • 深入HRESULT與Windows Error Codes的區(qū)別詳解

    深入HRESULT與Windows Error Codes的區(qū)別詳解

    本篇文章是對(duì)HRESULT與Windows Error Codes的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05

最新評(píng)論

安泽县| 宁明县| 枣强县| 滦平县| 利津县| 米泉市| 二手房| 马关县| 宾川县| 县级市| 仪陇县| 舞钢市| 林甸县| 阿拉尔市| 霍州市| 斗六市| 怀仁县| 美姑县| 通渭县| 玛多县| 德令哈市| 桂东县| 吕梁市| 云阳县| 濮阳县| 台湾省| 长汀县| 正镶白旗| 洪雅县| 土默特左旗| 菏泽市| 蓝田县| 万源市| 隆德县| 云和县| 新丰县| 元阳县| 岳普湖县| 吴江市| 文登市| 通海县|