C++string容器基本概念詳解
string基本概念
本質(zhì):
string是C++風(fēng)格的字符串,而string本質(zhì)上是一個(gè)類
string和char*區(qū)別:
- char*是一個(gè)指針
- 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ù)
#include <iostream>
using namespace std;
#include<string>
//string 的構(gòu)造函數(shù)
void test01()
{
//默認(rèn)構(gòu)造
string s1;
//string(const char* s);使用字符串s初始化
const char* str = "hello,world";
string s2(str);
cout << "s2= " << s2 << endl;
//string(const string& str)使用一個(gè)string對(duì)象初始化另一個(gè)string對(duì)象
string s3(s2);
cout << "s3= " << s3 << endl;
//string(int n,char c);使用n個(gè)字符c初始化
string s4(10, 'a');
cout << s4 << endl;
}
int main()
{
test01();
}
string賦值操作
功能描述:
給string字符串進(jìn)行賦值
#include <iostream>
using namespace std;
#include<string>
//string的賦值操作
void test01()
{
//string& operator=(const char* s)//char*類型字符串 賦值給當(dāng)前的字符串
string str1;
str1 = "hello,world";
cout << "str1=" <<str1<< endl;
//string& operator=(const string &s);//把字符串s賦給當(dāng)前的字符串
string str2;
str2 = str1;
cout << "str2= " << str2 << endl;
//string& operator=(char c);//字符賦值給當(dāng)前的字符串
string str3;
str3 = 'a';
cout << "str3= " << str3 << endl;
//string& assign(const char *s);//把字符串賦給當(dāng)前的字符串
string str4;
str4.assign("hello C++");
cout << "str4= " << str4 << endl;
//string& assign(const char *s,int n);//把字符串s的前n個(gè)字符賦給當(dāng)前的字符串
string str5;
str5.assign("hello C++", 5);
cout << "str5= " << str5 << endl;
//string& assign(const string &s);//把字符串s賦給當(dāng)前字符串
string str6;
str6.assign(str5);
cout << "str6= " << str6 << endl;
string str7;
str7.assign(10, 'w');
cout << "str7= " << str7 << endl;
}
int main()
{
test01();
}
string字符串拼接
功能描述:
實(shí)現(xiàn)在字符串末尾拼接字符串
#include <iostream>
using namespace std;
#include<string>
//string字符串拼接
void test01()
{
//第一種方法:重載+=操作符
string str1 = "我";
str1 += "愛(ài)玩游戲";
cout << "str1= " << str1 << endl;
str1 += ':';
cout << "str1= " << str1 << endl;
string str2 = "LOL";
str1 += str2;
cout << "str1= " << str1 << endl;
//把字符串s連接到當(dāng)前字符串結(jié)尾
string str3 = "I";
str3.append("love");
cout << "str3= " << str3 << endl;
//把字符串s的前n個(gè)字符連接到當(dāng)前字符字符串結(jié)尾
str3.append("game abcde", 4);
cout << "str3= " << str3 << endl;
//用append追加一個(gè)字符串
str3.append(str2);
cout << "str3= " << str3 << endl;
//字符串s從pos開(kāi)始的n個(gè)字符連接到字符串結(jié)尾
str3.append(str2, 0, 3);//參數(shù)2:從哪個(gè)位置開(kāi)始截取,參數(shù)3 截取字符個(gè)數(shù)
cout << "str3= " << str3 << endl;
}
int main()
{
test01();
}
string查找和替換操作
功能描述:
- 查找:查找指定字符串是否存在
- 替換:在指定位置替換字符串
#include <iostream>
using namespace std;
#include<string>
//字符串的查找和替換
//1.查找
void test01()
{
string str1 = "abcdefg";
int pos=str1.find("de");
if (pos == -1)
{
cout << "未找到字符串" << endl;
}
else
{
cout << "找到字符串pos: " << pos << endl;
}
//rfind
pos=str1.rfind("de");
cout << "pos= " << pos << endl;
//rfind和find的區(qū)別
//rfind從右往左查找,find從左往右查找
}
//2.替換
void test02()
{
string str1 = "abcdefg";
//replace在替換時(shí),要指定從哪個(gè)位置起,多少個(gè)字符,替換成什么樣的字符串
//從1號(hào)位置起3個(gè)字符替換為"1111"
str1.replace(1, 3, "1111");
cout << "str1= " << str1 << endl;
}
int main()
{
test01();
test02();
}
string字符串比較
功能描述:
- 字符串之間的比較
- 字符串比較是按字符的ASCII碼進(jìn)行比較
- =返回 0
返回 1
< 返回 -1
#include <iostream>
using namespace std;
#include<string>
//字符串比較
void test01()
{
string str1 = "A";
string str2 = "a";
if (str1.compare(str2) == 0)
{
cout << "str1等于str2" << endl;
}
else if (str1.compare(str2) > 0)
{
cout << "str1大于str2" << endl;
}
else if (str1.compare(str2) < 0)
{
cout << "str1小于str2" << endl;
}
}
int main()
{
test01();
}
string字符存取
string中單個(gè)字符存取方式有兩種:
- char& operator[](int n);//通過(guò)[]方式取字符
- char& at(int n);//通過(guò)at方法獲取字符
#include <iostream>
using namespace std;
#include<string>
//string 字符存取
void test01()
{
string str = "hello";
cout << "str= " <<str << endl;
//通過(guò)[]訪問(wèn)單個(gè)字符
for (int i = 0; i < str.size(); i++)
{
cout << str[i] << endl;
}
//通過(guò)at方式訪問(wèn)單個(gè)字符
for (int i = 0; i < str.size(); i++)
{
cout << str.at(i) << endl;
}
//修改單個(gè)字符
str[0] = 'x';
str.at(1) = 'x';
cout << "str= " << str << endl;
}
int main()
{
test01();
}
string插入和刪除
功能描述:
對(duì)string字符串進(jìn)行插入和刪除字符操作
#include <iostream>
using namespace std;
#include<string>
//字符串的插入和刪除
void test01()
{
string str = "hello";
//插入
str.insert(1, "111");
cout << "str= " << str << endl;
//刪除
str.erase(1, 3);
cout << "str= " << str << endl;
}
int main()
{
test01();
}
string子串
功能描述:
從字符串中獲取想要的子串‘
#include <iostream>
using namespace std;
#include<string>
//string求子串
void test01()
{
string str = "abcdef";
string subStr = str.substr(1, 3);
cout << "subStr= " << subStr << endl;
}
int main()
{
test01();
}
到此這篇關(guān)于C++string容器基本概念詳解的文章就介紹到這了,更多相關(guān)C++string容器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++插件化 NDD源碼的插件機(jī)制實(shí)現(xiàn)解析
這篇文章主要介紹了C++插件化 NDD源碼的插件機(jī)制實(shí)現(xiàn)解析,這里再介紹推薦下優(yōu)秀的國(guó)產(chǎn)軟件開(kāi)源項(xiàng)目?NDD(notepad--),一個(gè)支持windows/linux/mac的文本編輯器,目標(biāo)是要國(guó)產(chǎn)替換同類軟件,需要的朋友可以參考下2023-03-03
C++實(shí)現(xiàn)LeetCode(179.最大組合數(shù))
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(179.最大組合數(shù)),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
C語(yǔ)言詳解如何實(shí)現(xiàn)帶頭雙向循環(huán)鏈表
帶頭雙向循環(huán)鏈表:結(jié)構(gòu)最復(fù)雜,一般用在單獨(dú)存儲(chǔ)數(shù)據(jù)。實(shí)際中使用的鏈表數(shù)據(jù)結(jié)構(gòu),都是帶頭雙向循環(huán)鏈表。另外這個(gè)結(jié)構(gòu)雖然結(jié)構(gòu)復(fù)雜,但是使用代碼實(shí)現(xiàn)以后會(huì)發(fā)現(xiàn)結(jié)構(gòu)會(huì)帶來(lái)很多優(yōu)勢(shì),實(shí)現(xiàn)反而簡(jiǎn)單2022-04-04
C語(yǔ)言實(shí)現(xiàn)飛機(jī)大戰(zhàn)小游戲
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)飛機(jī)大戰(zhàn)小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06

