C++string容器基本概念詳解
string基本概念
本質:
string是C++風格的字符串,而string本質上是一個類
string和char*區(qū)別:
- char*是一個指針
- string是一個類,類內部封裝了char*,管理這個字符串,是一個char*型的容器。
特點:
string類內部封裝了很多成員方法
例如:查找find,拷貝copy,刪除delete,替換replace,插入insert
string管理char*所分配的內存,不用擔心復制越界和取值越界等,由類內部進行負責。
string構造函數(shù)
#include <iostream>
using namespace std;
#include<string>
//string 的構造函數(shù)
void test01()
{
//默認構造
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*類型字符串 賦值給當前的字符串
string str1;
str1 = "hello,world";
cout << "str1=" <<str1<< endl;
//string& operator=(const string &s);//把字符串s賦給當前的字符串
string str2;
str2 = str1;
cout << "str2= " << str2 << endl;
//string& operator=(char c);//字符賦值給當前的字符串
string str3;
str3 = 'a';
cout << "str3= " << str3 << endl;
//string& assign(const char *s);//把字符串賦給當前的字符串
string str4;
str4.assign("hello C++");
cout << "str4= " << str4 << endl;
//string& assign(const char *s,int n);//把字符串s的前n個字符賦給當前的字符串
string str5;
str5.assign("hello C++", 5);
cout << "str5= " << str5 << endl;
//string& assign(const string &s);//把字符串s賦給當前字符串
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連接到當前字符串結尾
string str3 = "I";
str3.append("love");
cout << "str3= " << str3 << endl;
//把字符串s的前n個字符連接到當前字符字符串結尾
str3.append("game abcde", 4);
cout << "str3= " << str3 << endl;
//用append追加一個字符串
str3.append(str2);
cout << "str3= " << str3 << endl;
//字符串s從pos開始的n個字符連接到字符串結尾
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();
}
到此這篇關于C++string容器基本概念詳解的文章就介紹到這了,更多相關C++string容器內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C++實現(xiàn)LeetCode(179.最大組合數(shù))
這篇文章主要介紹了C++實現(xiàn)LeetCode(179.最大組合數(shù)),本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下2021-08-08

