国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

第一百一十六天學(xué)習(xí)記錄:C++提高:STL-string(黑馬教學(xué)視頻)

這篇具有很好參考價(jià)值的文章主要介紹了第一百一十六天學(xué)習(xí)記錄:C++提高:STL-string(黑馬教學(xué)視頻)。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

string基本概念

string是C++風(fēng)格的字符串,而string本質(zhì)上是一個(gè)類
string和char區(qū)別
1、char
是一個(gè)指針
2、string是一個(gè)類,類內(nèi)部封裝了char*,管理這個(gè)字符串,是一個(gè)char型的容器。
特點(diǎn):
string類內(nèi)部封裝了很多成員方法
例如:查找find,拷貝copy,刪除delete替換replace,插入insert
string管理char
所分配的內(nèi)存,不用擔(dān)心復(fù)制越界和取值越界等,由類內(nèi)部進(jìn)行負(fù)責(zé)。

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

構(gòu)造函數(shù)原型:

string();     //創(chuàng)建一個(gè)空的字符串 例如:string str;
string(const char* s);    //使用字符串s初始化
string(const string& str);    //使用一個(gè)string對(duì)象初始化另一個(gè)string對(duì)象
string(int n,char c);    //使用n個(gè)字符c初始化
#include<iostream>
using namespace std;
#include<vector>
#include<string>

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


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


void test01()
{
	string s1;//默認(rèn)構(gòu)造
	const char* str = "hello world";
	string s2(str);
	cout << "s2=" << s2 << endl;
	string s3(s2);
	cout << "s3=" << s3 << endl;
	string s4(20, 'a');
	cout << "s4=" << s4 << endl;
}

int main()
{
	test01();
	return 0;
}

總結(jié):string的多種構(gòu)造方式?jīng)]有可比性,靈活使用即可。

string賦值操作

功能描述:
給string字符串進(jìn)行賦值

賦值的函數(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的前n個(gè)字符賦值給當(dāng)前的字符串
string& assign(const string& s);	//把字符串s賦給當(dāng)前字符串
string& assign(int n,char n);		//用n個(gè)字符c賦給當(dāng)前字符串
#include<iostream>
using namespace std;
#include<string>

//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的前n個(gè)字符賦值給當(dāng)前的字符串
//string& assign(const string& s);	//把字符串s賦給當(dāng)前字符串
//string& assign(int n, char n);		//用n個(gè)字符c賦給當(dāng)前字符串

void test01()
{
	string str1;
	str1 = "hello world";
	cout << "str1=" << str1 << endl;

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

	string str3;
	str3 = 'a';
	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(18,'z');
	cout << "str6=" << str7 << endl;
}

int main()
{
	test01();
	return 0;
}

總結(jié):string的賦值方式很多,operator= 這種方式是比較實(shí)用的

string字符串拼接

功能描述:
實(shí)現(xiàn)在字符串末尾拼接字符串

函數(shù)原型:

string& operator+=(const char* str);	//重載+=操作符
string& operator+=(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);			//同string& operator+=(const string& str)
string& append(const string& s,int pos,int n);				//字符串s中從pos開始的n個(gè)字符連接到字符串結(jié)尾
#include<iostream>
using namespace std;
#include<string>

//string& operator+=(const char* str);	//重載+=操作符
//string& operator+=(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);			//同string& operator+=(const string& str)
//string& append(const string& s, int pos, int n);				//字符串s中從pos開始的n個(gè)字符連接到字符串結(jié)尾

void test01()
{
	string str1 = "我";
	str1 += "是誰";
	cout << "str1=" << str1 << endl;
	str1 += '?';
	cout << "str1=" << str1 << endl;
	string str2 = "Who am I?";
	str1 += str2;
	cout << "str1=" << str1 << endl;

	string str3 = "你";
	str3.append("是");
	cout << "str3=" << str3 << endl;
	str3.append("誰的誰?????",4);
	cout << "str3=" << str3 << endl;
	str3.append(str2);
	cout << "str3=" << str3 << endl;
	str3.append(str2,4,2);//截取
	cout << "str3=" << str3 << endl;
}

int main()
{
	test01();
	return 0;
}

第一百一十六天學(xué)習(xí)記錄:C++提高:STL-string(黑馬教學(xué)視頻),編程語言學(xué)習(xí),學(xué)習(xí),c++

string查找和替換

功能描述:查找:查找指定字符串是否存在
替換:在指定的位置替換字符串
函數(shù)原型:

int find(const string& str,int pos=0) const;			//查找str第一次出現(xiàn)位置,從pos開始查找
int find(const char*s,int pos=0)const;					//查找s第一次出現(xiàn)位置,從pos開始查找
int find(const char*s,int pos,int n)const;				//從pos位置查找s的前n個(gè)字符第一次位置
int find(const char c,int pos=0)const;					//查找字符c第一次出現(xiàn)位置
int rfind(const string& str,int pos=npos)const;		//查找str最后一次位置,從pos開始查找
int rfind(const char* s,int pos=npos)const;			//查找s最后一次出現(xiàn)位置,從pos開始查找
int rfind(const char*s int pos,int n)const;				//從pos查找s的前n個(gè)字符最后一次位置
int rfind(const char c,int pos=0)const;					//查找字符c最后一次出現(xiàn)位置
string& replace(int pos,int n,const string& str)	//替換從pos開始n個(gè)字符為字符串str
string& replace(int pos,int n,const char* s)			//替換從pos開始的n個(gè)字符為字符串s
#include<iostream>
using namespace std;
#include<string>

//字符串查找和替換

//1、查找

void test01()
{
	string str1 = "abcdefgde";
	//int pos = str1.find("de");
	int pos = str1.find("de",0);//3 沒有則返回-1
	if (pos == -1)
	{
		cout << "未找到字符串" << endl;
	}
	else
	{
		cout << "找到字符串,pos = " << pos << endl;
	}

	pos = str1.rfind("de");//7
	cout << "pos = " << pos << endl;
}
//rfind 和 find 區(qū)別
//rfind從右往左查找  find從左往右查找

//2、替換

void test02()
{
	string str1 = "abcdefg";
	//從1號(hào)位置起3個(gè)字符 替換為"1111111"
	str1.replace(1, 3, "1111111");
	cout << "str1 = " << str1 << endl;
}

int main()
{
	//test01();
	test02();
	return 0;
}

string字符串比較

功能描述:
字符串之間的比較
比較方式:
字符串比較是按字符的ASCII碼進(jìn)行對(duì)比

= 返回 0
> 返回 1
< 返回 -1

函數(shù)原型:
1、int compare(const string& s)const; //與字符串s比較
2、int compare(const char* s)const; //與字符串s比較

#include<iostream>
using namespace std;
#include<string>

//字符串比較

void test01()
{
	string str1 = "hello";
	string str2 = "hello";

	if (str1.compare(str2) == 0)
	{
		cout << "str1 等于 str2 " << endl;
	}
	else if (str1.compare(str2) > 0)
	{
		cout << "str1 大于 str2 " << endl;
	}
	else
	{
		cout << "str1 小于 str2 " << endl;
	}
}

int main()
{
	test01();
	return 0;
}

注:主要是用來比較是否相等,大于小于的意義不大。

string字符存取

string 中單個(gè)字符存取方式有兩種
1、char& operator[](int n); //通過[]方式取字符
2、char& at(int n);通過at方式獲取字符

#include<iostream>
using namespace std;
#include<string>

//string 字符存取

void test01()
{
	string str = "hello";
	//cout << "str=" << str << endl;

	//1、通過[]訪問單個(gè)字符

	for (int i = 0; i < str.size(); ++i)
	{
		cout << str[i] << " ";
	}
	cout << endl;

	//2、通過at方式訪問單個(gè)字符

	for (int i = 0; i < str.size(); ++i)
	{
		cout << str.at(i) << " ";
	}
	cout << endl;

	//修改單個(gè)字符
	str[0] = 'x';

	cout << "str=" << str << endl;

	str.at(1) = 'x';

	cout << "str=" << str << endl;
}

int main()
{
	test01();
	return 0;
}

string 插入和刪除

功能描述:
對(duì)string字符串進(jìn)行插入和刪除字符操作

函數(shù)原型:

string& insert(int pos,const char*s);    //插入字符串
string& insert(int pos,const string& str)//插入字符串
string& insert(int pos,int n,char c);		//在指定位置插入n個(gè)字符c
string& erase(int pos,int n=npos);		//刪除從Pos開始的n個(gè)字符
#include<iostream>
using namespace std;
#include<string>

//字符串 插入和刪除

void test01()
{
	string str = "hello";

	//插入
	str.insert(1, "111");

	cout << "str=" << str << endl;//h111ello

	//刪除
	str.erase(1, 3);

	cout << "str=" << str << endl;
}

int main()
{
	test01();
	return 0;
}

總結(jié):插入和刪除的起始下標(biāo)都是從0開始

string子串

功能描述:
從字符串中獲取想要的子串
函數(shù)原型:
string substr(int pos=0,int n =npos)const;//返回由pos開始的n個(gè)字符組成的字符串

#include<iostream>
using namespace std;
#include<string>

//string 求子串

void test01()
{
	string str = "abcdef";

	string subStr = str.substr(1, 3);

	cout << "subStr=" << subStr << endl;
}

//實(shí)用操作
void test02()
{
	string email = "hello@sina.com";

	//從郵件地址中 獲取 用戶信息

	int pos = email.find('@');

	cout << pos << endl;

	string usrName = email.substr(0, pos);

	cout << usrName << endl;

}

int main()
{
	//test01();
	test02();
	return 0;
}

總結(jié):靈活的運(yùn)用求子串功能,可以在實(shí)際開發(fā)中獲取有效的信息文章來源地址http://www.zghlxwxcb.cn/news/detail-608534.html

到了這里,關(guān)于第一百一十六天學(xué)習(xí)記錄:C++提高:STL-string(黑馬教學(xué)視頻)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 第一百一十九回 如何通過藍(lán)牙設(shè)備讀寫數(shù)據(jù)

    我們?cè)谏弦徽禄刂薪榻B了如何獲取藍(lán)牙狀態(tài)相關(guān)的內(nèi)容,本章回中將介紹 如何通過藍(lán)牙設(shè)備讀寫數(shù)據(jù) 。閑話休提,讓我們一起Talk Flutter吧。 通過藍(lán)牙設(shè)備讀寫數(shù)據(jù)有兩種方法: 一種是讀寫Characteristics; 一種是讀寫Descriptor. 我們?cè)诒菊禄刂薪榻B的讀寫數(shù)據(jù)本質(zhì)上是讀寫 Cha

    2024年02月12日
    瀏覽(18)
  • 第一百一十二回 flutter_screenutil包

    我們?cè)谏弦徽禄刂薪榻B了屏幕適配相關(guān)的內(nèi)容,本章回中將介紹 flutter_screenutil 包,該包主要用來實(shí)現(xiàn)屏幕適配.閑話休提,讓我們一起Talk Flutter吧。 我們?cè)诒菊禄刂薪榻B的包是 flutter_screenutil ,它主要用來做屏幕和字體大小的適配,它的實(shí)現(xiàn)原理和我們?cè)谏弦徽禄刂薪榻B的屏幕

    2024年02月13日
    瀏覽(17)
  • 一百一十六、Zeppelin——Zeppelin0.9.0連接ClickHouse21.9.5.16(親測(cè)有效,附步驟截圖)

    一百一十六、Zeppelin——Zeppelin0.9.0連接ClickHouse21.9.5.16(親測(cè)有效,附步驟截圖)

    http://t.csdn.cn/DGHIb http://t.csdn.cn/DGHIb [root@hurys22 bin]# ./zeppelin-daemon.sh start Zeppelin start ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [ ?OK ?] [root@hurys22 bin]#? [root@hurys22 bin]# jps 18017 NodeManager 17572 SecondaryNameNode 17136 NameNode 17297 DataNode 17849 ResourceManager 26239 ZeppelinServer ?在default u

    2024年02月05日
    瀏覽(21)
  • 第一百零六天學(xué)習(xí)記錄:數(shù)據(jù)結(jié)構(gòu)與算法基礎(chǔ):?jiǎn)捂湵恚ㄍ踝拷虒W(xué)視頻)

    第一百零六天學(xué)習(xí)記錄:數(shù)據(jù)結(jié)構(gòu)與算法基礎(chǔ):?jiǎn)捂湵恚ㄍ踝拷虒W(xué)視頻)

    結(jié)點(diǎn)在存儲(chǔ)器中的位置是任意的,即邏輯上相鄰的數(shù)據(jù)元素在物理上不一定相鄰 線性表的鏈?zhǔn)奖硎居址Q為非順序映像或鏈?zhǔn)接诚瘛?用一組物理位置任意的存儲(chǔ)單元來存放線性表的數(shù)據(jù)元素。 這組存儲(chǔ)單元既可以是連續(xù)的,也可以是不連續(xù)的,甚至是零散分布在內(nèi)存中的任意

    2024年02月16日
    瀏覽(27)
  • 第一百二十六天學(xué)習(xí)記錄:C++提高:案例-評(píng)委打分(黑馬教學(xué)視頻)
  • Android之關(guān)機(jī)/重啟/recovery模式(一百一十五)

    1.方式一:App調(diào)用系統(tǒng)api 2.方式二:setprop 注意:在init的reboot.c中, ? ? ? ? ? adb reboot -p命令其實(shí)就是調(diào)用的setprop命令開關(guān)機(jī)的。? 3.方式三:

    2024年02月14日
    瀏覽(28)
  • Emacs之改造最快文本搜索工具ripgrep(一百一十九)

    Emacs之改造最快文本搜索工具ripgrep(一百一十九)

    簡(jiǎn)介: CSDN博客專家,專注Android/Linux系統(tǒng),分享多mic語音方案、音視頻、編解碼等技術(shù),與大家一起成長(zhǎng)! 優(yōu)質(zhì)專欄: Audio工程師進(jìn)階系列 【 原創(chuàng)干貨持續(xù)更新中…… 】?? 人生格言: 人生從來沒有捷徑,只有行動(dòng)才是治療恐懼和懶惰的唯一良藥. 更多原創(chuàng),歡迎關(guān)注:An

    2024年02月15日
    瀏覽(21)
  • 一百一十三、DBeaver——從hive同步數(shù)據(jù)到clickhouse

    一百一十三、DBeaver——從hive同步數(shù)據(jù)到clickhouse

    目標(biāo):把hive的DM層結(jié)果數(shù)據(jù)導(dǎo)出到clickhouse數(shù)據(jù)庫,試了kettle、sqoop等多種方法都報(bào)錯(cuò),尤其是kettle,搞了大半天發(fā)現(xiàn)還是不行。結(jié)果目前就只能用DBeaver同步數(shù)據(jù)。 準(zhǔn)備工作:hive和clickhouse中都建好表 第一步,右擊clickhouse中的表,選擇導(dǎo)入數(shù)據(jù) 第二步,在源類型和格式中,由

    2024年02月13日
    瀏覽(26)
  • Emacs之實(shí)現(xiàn)跨程序選中自動(dòng)復(fù)制功能(一百一十八)

    Emacs之實(shí)現(xiàn)跨程序選中自動(dòng)復(fù)制功能(一百一十八)

    簡(jiǎn)介: CSDN博客專家,專注Android/Linux系統(tǒng),分享多mic語音方案、音視頻、編解碼等技術(shù),與大家一起成長(zhǎng)! 優(yōu)質(zhì)專欄: Audio工程師進(jìn)階系列 【 原創(chuàng)干貨持續(xù)更新中…… 】?? 人生格言: 人生從來沒有捷徑,只有行動(dòng)才是治療恐懼和懶惰的唯一良藥. 更多原創(chuàng),歡迎關(guān)注:An

    2024年02月15日
    瀏覽(30)
  • C/C++基礎(chǔ)講解(一百一十九)之經(jīng)典篇(最優(yōu)美的圖案)

    很多時(shí)候,特別是剛步入大學(xué)的學(xué)子們,對(duì)于剛剛開展的計(jì)算機(jī)課程基本上是一團(tuán)迷霧,想要弄明白其中的奧秘,真的要花費(fèi)一些功夫,我和大家一樣都是這么啃過來的,從不知到知知,懵懂到入門,每一步都走的很艱辛,課程上,大學(xué)老師基本上講解上機(jī)實(shí)操得時(shí)間特別有

    2024年02月09日
    瀏覽(21)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包