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

一文搞懂C++中string容器的構造及使用

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

string容器

string基本概念

本質:

string是c++風格的字符串,不同于c語言的 char*,他本質是一個類

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

char*是一個指針

string是一個類,類內部封裝了char*來管理字符串,是一個char&型的容器

特點:

strint類內部封裝了很多成員方法

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

string管理char*所分配的內存,不用考慮賦值越界和取值越界等問題,由類內部進行負責

string構造函數

四種函數原型

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

使用示例:

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

上面就是四個構造方法對應的舉例了,第一種方式是我們頻繁使用的;第二種方式就是設置不可變字符數組傳入構造來初始化;第三種方式理解為調用拷貝構造即可;第四種方式就比較有意思了,在上面代碼里的意思就是用6個'a'來初始化字符串,輸入s4結果為:aaaaaa。

string賦值操作

給string字符串賦值

賦值的函數原型:

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

使用示例:

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拼接操作

在字符串末尾拼接字符串

函數原型:

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

使用示例:

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:初學者只需要稍微記幾個拼接函數即可

string查找替換

指定位置查找字符串

指定位置刪除字符串

函數原型:

1.查找s第一次出現位置,從pos開始查找

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

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

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

3.查找字符c第一次出現位置

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

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

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

5.查找str最后一次位置,從pos開始查找,計數永遠是從前往后

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

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

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

7.查找字符c最后一次出現位置

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

8.替換從pos開始n個字符為字符串str

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

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

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

使用示例:

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

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

tips:

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

函數雖然很多,但幾乎都是兩個版本的,一個是c++風格一個c語言風格

string字符串比較

字符串比較是按字符的ASCII碼進行對比

函數原型:

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:字符串對比的目的是比較兩個字符串是否相等,判斷誰大誰小的意義并不是很大。

string字符讀取

單個字符存取有兩種方式:

函數原型:

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

使用示例:

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

string插入和刪除

函數原型:

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個字符c
string& erase(int pos,int n = npos);//刪除從pos位置開始的n個字符

使用示例:

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

tips:插入和刪除的起始下標都是從0開始。

string求子串

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

函數原型:

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

//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:靈活的運用求子串功能,可以在實際開發(fā)中獲取有效的信息,在上述代碼中就可以有效獲取到不同郵箱中的用戶名。

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

相關文章

  • C++中sort函數的基礎入門使用教程

    C++中sort函數的基礎入門使用教程

    這篇文章主要給大家介紹了關于C++中sort函數的基礎入門使用的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用C++具有一定的參考學習價值,需要的朋友們下面來一起看看吧
    2018-12-12
  • c++實現廣播通訊詳解

    c++實現廣播通訊詳解

    這篇文章主要為大家詳細介紹了c++實現廣播通訊的相關知識,文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的小伙伴可以參考一下
    2024-12-12
  • C語言中getchar的用法以及實例解析

    C語言中getchar的用法以及實例解析

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

    Qt?QDateTime計算時間差的實現示例

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

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

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

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

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

    C++中inline用法案例詳解

    這篇文章主要介紹了C++中inline用法案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下
    2021-09-09
  • C++帶有指針成員的類處理方式詳解

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

    這篇文章主要為大家詳細介紹了C++帶有指針成員的類處理方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • C++實現插入排序對整數數組排序

    C++實現插入排序對整數數組排序

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

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

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

最新評論

萨迦县| 永春县| 鄂托克旗| 图片| 通化市| 德钦县| 蓝田县| 天镇县| 伊宁县| 定南县| 三亚市| 武陟县| 钟祥市| 铁岭县| 彩票| 武清区| 呈贡县| 洪雅县| 博客| 大渡口区| 雅安市| 嘉兴市| 疏勒县| 舟山市| 吉水县| 蓝田县| 庄浪县| 怀远县| 龙江县| 陇川县| 岳池县| 大渡口区| 嘉峪关市| 岱山县| 邢台县| 固原市| 三都| 惠州市| 马山县| 关岭| 遂溪县|