最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c++的字符串string基本操作大全

 更新時(shí)間:2025年12月15日 10:39:51   作者:fish_xk  
C++中的string類型提供了豐富的字符串操作功能,包括創(chuàng)建、賦值、輸入輸出、查找、插入、刪除、截取等,同時(shí),string還支持關(guān)系運(yùn)算和與int值的轉(zhuǎn)換,本文給大家介紹c++的字符串string,感興趣的朋友跟隨小編一起看看吧

string的概念

string是c++專門創(chuàng)建的字符串類型,為了方便操作字符串。

string中很多的方法。

string的基本操作

首先要包含頭文件string.

創(chuàng)建

有初始化的創(chuàng)建,它的末尾不以'\0'為結(jié)尾。

#include <iostream>
#include<string>
using namespace std; 
int main() {
	string a="hello world";
    //string a("hello world");
	cout<<a;
	return 0;
}

string可以直接賦值

#include <iostream>
#include<string>
using namespace std; 
int main() {
	string a;
	string b("hello world");
	a=b;
	cout<<a;
	return 0;
}

輸入

cin輸入,不能輸入空格

#include <iostream>
#include<string>
using namespace std; 
int main() {
	string a;
	cin>>a;
	cout<<a;
	return 0;
}

getline是c++標(biāo)準(zhǔn)函數(shù),讀取一行文本存儲為字符。

istream&getline(istream& is,string&str);遇到'\n'停止。

#include <iostream>
#include<string>
using namespace std; 
int main() {
	string a;
	getline(cin,a); 
	cout<<a;
	return 0;
}

istream&getline(istream& is,string&str,char delim);直到遇到delim字符停止

#include <iostream>
#include<string>
using namespace std; 
int main() {
	string a;
	getline(cin,a,'m');//讀到m停止 
	cout<<a;
	return 0;
}

獲取長度

size(),string有很多函數(shù),用 . 使用

#include <iostream>
#include<string>
using namespace std; 
int main() {
	string b;
	string a="abcd"; 
	string c="abcdefg";
	string d="j k l l l";
	cout<<a.size()<<endl;
	cout<<b.size()<<endl;
	cout<<c.size()<<endl;
	cout<<d.size()<<endl;
	return 0;
}

string的數(shù)據(jù)也有下標(biāo)和正常創(chuàng)建的字符數(shù)組一樣從0開始。

迭代器

用來遍歷容器元素。

iterator類似指針,數(shù)組下標(biāo)。

需要*解引用。

begin()和end()

begin()指向第一個(gè)位置,end()指向最后的下一個(gè)位置,不存在于字符串。

可以比較大小,加減。

#include <iostream>
#include<string>
using namespace std; 
int main() {
	string a="hello world";
	string::iterator it1=a.begin();
	string::iterator it2=a.end();
	if(it1<it2)
		cout<<"<"<<endl;
	else
		cout<<">"<<endl;
	return 0;
}

可以通過*解引用,輸出存儲的字符。

#include <iostream>
#include<string>
using namespace std; 
int main() {
	string a="hello world";
	string::iterator it1=a.begin();
	string::iterator it2=a.end();
	cout<<*it1<<endl;//[0]
	it1++;
	cout<<*it1<<endl;//[1]
	it1--;
	cout<<*it1<<endl;//[0]
	return 0;
}

使用迭代器遍歷。

#include <iostream>
#include<string>
using namespace std; 
int main() {
	string a="abcdef";
	string::iterator it1=a.begin();
	string::iterator it2=a.end();
	for (string::iterator i = a.begin(); i != a.end(); ++i) {
        cout << *i;
    }
	cout<<endl;
	for(auto i=it2-1;i>=it1;--i){
		cout<<*i;
	}
	return 0;
}

還有反向迭代器,reverse_iterator  類型的rbegin(),rend()剛好相反。

尾插函數(shù)

a.push_back();在字符串尾部插入字符

#include <iostream>
#include<string>
using namespace std; 
int main() {
	string a="abcdef";
	cout<<a<<endl;
	a.push_back('g');
	cout<<a<<endl;
	return 0;
}

+=和+運(yùn)算

也可以頭部拼接。

尾刪pop_back()

空的情況下刪除會報(bào)錯(cuò)。

insert()插入

a.insert(pos  ,  str),在a字符串中的pos位置前插入,str字符串。str可以是string類型的內(nèi)容,可以是字符串如下圖。

a.insert(pos,n,str)插入n個(gè)str的字符到pos前。

#include <iostream>
#include<string>
using namespace std; 
int main() {
	string a="abcdef";
	cout<<a<<endl;
	a.insert(3,"6");
	cout<<a<<endl;
	return 0;
}

find查找

a.find()返回第一次出現(xiàn)的位置。它的返回值是size_t,相當(dāng)于下標(biāo)

a.find(str,位置(默認(rèn)0))

#include <iostream>
#include<string>
using namespace std; 
int main() {
	string a="abcdef abc def l g k";
	string b="de";
	string c="l";
	size_t t=a.find(b);
	cout<<t<<endl;
	cout<<a.find(b,t+1)<<endl;
	return 0;
}

a.find(char*,size_t  pos);

可以查找c風(fēng)格的字符串。

a.find(str,size_t pos ,n)a字符串中查找str前n給字符

也可以找字符。str

找不到會放回-1;無符號整型的-1。2^32-1;

substr()

a.substr(pos,len=npos);pos截取的開始,npos截取的長度。pos默認(rèn)為0,npos默認(rèn)到字符串末尾

的長度。

用于截取a字符串pos位置開始n個(gè)的字符。

#include <iostream>
#include<string>
using namespace std; 
int main() {
	string a="abcdefghi";
	string b=a.substr(3,3);
	string c=a.substr(3);
	cout<<b<<endl;
	cout<<c<<endl;
	return 0;
}

關(guān)系運(yùn)算

==,至少有一個(gè)str的字符才可以比較,比較的ascii值對應(yīng)位置。

#include <iostream>
#include<string>
using namespace std; 
int main() {
	string a="abcdefghi";
	string b="abcdefghi";
	if(a==b)
		cout<<"=="<<endl;
	string c="abcdefgh";
	if(a!=c)
		cout<<"!="<<endl;
	string d="b";
	if(a<d)
		cout<<"<"<<endl;
	string e="aa";
	if(a>e)
		cout<<">"<<endl;
	return 0;
}

字符串和int 值的轉(zhuǎn)換。

stoi/stol,stol是int ,stol是long int。

int stoi(str,size_t* idx=0,int base=0)

long stol(str,size_t* idx=0,int base=0)

str是string類型的字符串。

idx是一個(gè)指針,從idx位置開始到第一個(gè)無法匹配的下標(biāo)返回

如“123q”返回q的位置

base表示被解析的數(shù)字的進(jìn)制,

2表示被解析的數(shù)字當(dāng)作2進(jìn)制數(shù)字,轉(zhuǎn)化為10進(jìn)制。

0是自動(dòng)推到如0x是16,0是8;

#include <iostream>
#include<string>
using namespace std; 
int main() {
	string a="123p";
	size_t t=0;
	int b=stoi(a,&t,10);
	int c=stoi(a,&t,8);
	cout<<b<<endl;
	cout<<c<<endl;
	return 0;
}

stod轉(zhuǎn)化為double,stof轉(zhuǎn)化為float

stod(str,size_t* idx);只有10進(jìn)制。

stof(str,size_t* idx);

to_string

把數(shù)字轉(zhuǎn)換為字符串。

反轉(zhuǎn)字符串

reverse要包含頭文件algorithm.

reverse(首元素的地址,反轉(zhuǎn)范圍末尾的下一個(gè)元素)。

#include <iostream>
#include <string>
#include <cstdio>
#include <algorithm>
using namespace std; 
int main() {
    string str;
    cin>>str;
	reverse(str.begin(),str.end());
	cout<<str;
    return 0;
}

到此這篇關(guān)于c++的字符串string基本操作大全的文章就介紹到這了,更多相關(guān)c++字符串string內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

  • C語言 從根本上理解數(shù)組

    C語言 從根本上理解數(shù)組

    數(shù)組是一組有序的數(shù)據(jù)的集合,數(shù)組中元素類型相同,由數(shù)組名和下標(biāo)唯一地確定,數(shù)組中數(shù)據(jù)不僅數(shù)據(jù)類型相同,而且在計(jì)算機(jī)內(nèi)存里連續(xù)存放,地址編號最低的存儲單元存放數(shù)組的起始元素,地址編號最高的存儲單元存放數(shù)組的最后一個(gè)元素
    2022-04-04
  • c++中struct和class的區(qū)別小結(jié)

    c++中struct和class的區(qū)別小結(jié)

    在C++中,class和struct都是用于定義自定義數(shù)據(jù)類型的關(guān)鍵字,本文主要介紹了c++中struct和class的區(qū)別小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-08-08
  • C/C++實(shí)現(xiàn)矩陣的轉(zhuǎn)置(示例代碼)

    C/C++實(shí)現(xiàn)矩陣的轉(zhuǎn)置(示例代碼)

    C/C++實(shí)現(xiàn)矩陣的轉(zhuǎn)置(示例代碼)需要的朋友可以過來參考下,希望對大家有所幫助
    2013-10-10
  • C語言驅(qū)動(dòng)開發(fā)之內(nèi)核解鎖與強(qiáng)刪文件

    C語言驅(qū)動(dòng)開發(fā)之內(nèi)核解鎖與強(qiáng)刪文件

    在某些時(shí)候我們的系統(tǒng)中會出現(xiàn)一些無法被正常刪除的文件,如果想要強(qiáng)制刪除則需要在驅(qū)動(dòng)層面對其進(jìn)行解鎖后才可刪掉,本文為大家介紹了內(nèi)核解鎖與強(qiáng)刪文件的方法,希望對大家有所幫助
    2023-06-06
  • C++?指針和對象成員訪問的區(qū)別:`.`?與?`->`?的使用小結(jié)

    C++?指針和對象成員訪問的區(qū)別:`.`?與?`->`?的使用小結(jié)

    在學(xué)習(xí)?C++?時(shí),常常會遇到訪問對象成員的兩種符號:.?和?->,這兩個(gè)符號看似簡單,但它們的正確使用卻需要理解指針和對象的本質(zhì)差異,本文介紹C++?指針和對象成員訪問的區(qū)別:`.`?與?`->`?的使用指南,感興趣的朋友一起看看吧
    2024-12-12
  • C語言實(shí)現(xiàn)文件讀寫

    C語言實(shí)現(xiàn)文件讀寫

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)文件讀寫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • C/C++并查集的查詢與合并實(shí)現(xiàn)原理

    C/C++并查集的查詢與合并實(shí)現(xiàn)原理

    這篇文章主要介紹了C/C++并查集的查詢與合并,并查集是一種用來管理元素分組情況的數(shù)據(jù)結(jié)構(gòu)。并查集可以高效地進(jìn)行如下操作
    2023-02-02
  • C++中new和delete的使用方法詳解

    C++中new和delete的使用方法詳解

    這篇文章主要介紹了C++中new和delete的使用方法詳解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-10-10
  • C++四種類型轉(zhuǎn)換全解

    C++四種類型轉(zhuǎn)換全解

    C++的四種命名式類型轉(zhuǎn)換操作符是:static_cast、dynamic_cast、const_cast 和 reinterpret_cast,??本文介紹C++四種類型轉(zhuǎn)換是什么,感興趣的朋友一起看看吧
    2026-06-06
  • 最新評論

    新民市| 萝北县| 嘉荫县| 惠州市| 理塘县| 封丘县| 绍兴县| 西乌珠穆沁旗| 贵州省| 孟村| 德昌县| 鹤岗市| 南雄市| 大厂| 东港市| 旬邑县| 松潘县| 栖霞市| 濮阳市| 道孚县| 芜湖市| 大竹县| 景谷| 桓台县| 稻城县| 改则县| 塔城市| 广饶县| 孝感市| 琼海市| 富蕴县| 左贡县| 巴林右旗| 寻甸| 九江县| 蓝田县| 郴州市| 合川市| 巨野县| 鄂托克前旗| 砀山县|