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)部進(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)使用一個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字符串進(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個字符賦給當(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碼進(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中單個字符存取方式有兩種:
- 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字符串進(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一篇文章帶你了解C++ static的作用,全局變量和局部變量的區(qū)別
這篇文章介紹了C++ static的作用,全局變量和局部變量的區(qū)別,需要的朋友可以過來參考下,希望能夠給你帶來幫助2021-09-09
OpenCV實現(xiàn)彩色照片轉(zhuǎn)換成素描卡通片
這篇文章主要為大家詳細(xì)介紹了OpenCV實現(xiàn)彩色照片轉(zhuǎn)換成素描卡通片,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-01-01
stringstream操縱string的方法總結(jié)
下面小編就為大家?guī)硪黄猻tringstream操縱string的方法總結(jié)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12

