欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++string容器基本概念詳解

 更新時間:2021年07月01日 09:57:11   作者:鬼才fjz  
c++相比c的一個好處就是實現(xiàn)了很多的容器和泛型算法,使得程序員的工作得到了很大的簡化,本文重點給大家介紹C++string容器基本概念講解,需要的朋友參考下吧

string基本概念

本質(zhì):

string是C++風(fēng)格的字符串,而string本質(zhì)上是一個類

string和char*區(qū)別:

  • char*是一個指針
  • string是一個類,類內(nèi)部封裝了char*,管理這個字符串,是一個char*型的容器。

特點:
string類內(nèi)部封裝了很多成員方法
例如:查找find,拷貝copy,刪除delete,替換replace,插入insert
string管理char*所分配的內(nèi)存,不用擔(dān)心復(fù)制越界和取值越界等,由類內(nèi)部進行負(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)使用一個string對象初始化另一個string對象
	string s3(s2);
	cout << "s3= " << s3 << endl;
	//string(int n,char c);使用n個字符c初始化
	string s4(10, 'a');
	cout << s4 << endl;
}
int main()
{
	test01();
}

string賦值操作

功能描述:

給string字符串進行賦值

#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個字符賦給當(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字符串拼接

功能描述:

實現(xiàn)在字符串末尾拼接字符串

#include <iostream>
using namespace std;
#include<string>
//string字符串拼接
void test01()
{
	//第一種方法:重載+=操作符
	string str1 = "我";
	str1 += "愛玩游戲";
	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個字符連接到當(dāng)前字符字符串結(jié)尾
	str3.append("game abcde", 4);
	cout << "str3= " << str3 << endl;
	//用append追加一個字符串
	str3.append(str2);
	cout << "str3= " << str3 << endl;
	//字符串s從pos開始的n個字符連接到字符串結(jié)尾
	str3.append(str2, 0, 3);//參數(shù)2:從哪個位置開始截取,參數(shù)3 截取字符個數(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在替換時,要指定從哪個位置起,多少個字符,替換成什么樣的字符串
	//從1號位置起3個字符替換為"1111"
	str1.replace(1, 3, "1111");
	cout << "str1= " << str1 << endl;
}
int main()
{
	test01();
	test02();
}

string字符串比較

功能描述:

  • 字符串之間的比較
  • 字符串比較是按字符的ASCII碼進行比較
  • =返回 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中單個字符存取方式有兩種:

  • char& operator[](int n);//通過[]方式取字符
  • char& at(int n);//通過at方法獲取字符
#include <iostream>
using namespace std;
#include<string>
//string 字符存取
void test01()
{
	string str = "hello";
	cout << "str= " <<str << endl;
	//通過[]訪問單個字符
	for (int i = 0; i < str.size(); i++)
	{
		cout << str[i] << endl;
	}
	//通過at方式訪問單個字符
	for (int i = 0; i < str.size(); i++)
	{
		cout << str.at(i) << endl;
	}
	//修改單個字符
	str[0] = 'x';
	str.at(1) = 'x';
	cout << "str= " << str << endl;
}
int main()
{
	test01();
}

string插入和刪除

功能描述:

對string字符串進行插入和刪除字符操作

#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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • c++函數(shù)指針使用示例分享

    c++函數(shù)指針使用示例分享

    這篇文章主要介紹了c++函數(shù)指針使用示例,需要的朋友可以參考下
    2014-03-03
  • 詳解C語言#define預(yù)處理宏定義

    詳解C語言#define預(yù)處理宏定義

    本文主要介紹了C語言#define預(yù)處理宏定義,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • 利用Matlab復(fù)刻羊了個羊小游戲

    利用Matlab復(fù)刻羊了個羊小游戲

    最近羊了個羊游戲可謂是異?;鸨磉厧缀醵荚谕?,他其實就是一個簡單的卡通背景消除闖關(guān)游戲,本文將用Matlab復(fù)刻這一游戲,感興趣的可以了解一下
    2022-09-09
  • 深入理解C++中的文件操作

    深入理解C++中的文件操作

    這篇文章主要給大家深入的介紹了C++中的文件操作,文件的操作對每個程序員來說都是很重要的,本文的介紹的很詳細,有需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-01-01
  • C++插件化 NDD源碼的插件機制實現(xiàn)解析

    C++插件化 NDD源碼的插件機制實現(xiàn)解析

    這篇文章主要介紹了C++插件化 NDD源碼的插件機制實現(xiàn)解析,這里再介紹推薦下優(yōu)秀的國產(chǎn)軟件開源項目?NDD(notepad--),一個支持windows/linux/mac的文本編輯器,目標(biāo)是要國產(chǎn)替換同類軟件,需要的朋友可以參考下
    2023-03-03
  • C++實現(xiàn)LeetCode(179.最大組合數(shù))

    C++實現(xiàn)LeetCode(179.最大組合數(shù))

    這篇文章主要介紹了C++實現(xiàn)LeetCode(179.最大組合數(shù)),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • C中的volatile使用方法

    C中的volatile使用方法

    volatile 影響編譯器編譯的結(jié)果,指出,volatile 變量是隨時可能發(fā)生變化的,與volatile變量有關(guān)的運算,不要進行編譯優(yōu)化,以免出錯
    2013-02-02
  • C語言詳解如何實現(xiàn)帶頭雙向循環(huán)鏈表

    C語言詳解如何實現(xiàn)帶頭雙向循環(huán)鏈表

    帶頭雙向循環(huán)鏈表:結(jié)構(gòu)最復(fù)雜,一般用在單獨存儲數(shù)據(jù)。實際中使用的鏈表數(shù)據(jù)結(jié)構(gòu),都是帶頭雙向循環(huán)鏈表。另外這個結(jié)構(gòu)雖然結(jié)構(gòu)復(fù)雜,但是使用代碼實現(xiàn)以后會發(fā)現(xiàn)結(jié)構(gòu)會帶來很多優(yōu)勢,實現(xiàn)反而簡單
    2022-04-04
  • C語言實現(xiàn)飛機大戰(zhàn)小游戲

    C語言實現(xiàn)飛機大戰(zhàn)小游戲

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)飛機大戰(zhàn)小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • 淺談 C++17 里的 Visitor 模式

    淺談 C++17 里的 Visitor 模式

    Visitor模式經(jīng)常用于將更新的設(shè)計封裝在一個類中,并且由待更改的類提供一個接受接口,其關(guān)鍵技術(shù)在于雙分派技術(shù),本文主要介紹 C++17 里的 Visitor 模式的相關(guān)資料,需要的朋友可以參考下面文章的具體內(nèi)容
    2021-09-09

最新評論