详解C++中string的用法和例子

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

详解C++中string的⽤法和例⼦
string是C++标准库的⼀个重要的部分,主要⽤于字符串处理。

可以使⽤输⼊输出流⽅式直接进⾏操作,也可以通过⽂件等⼿段进⾏操作。

同时C++的算法库对string也有着很好的⽀持,⽽且string还和c语⾔的字符串之间有着良好的接⼝。

虽然也有⼀些弊端,但是瑕不掩瑜。

其中使⽤的代码多数都是来⾃cpp官⽹,因为例⼦⾮常全。

声明和初始化⽅法:
想使⽤string⾸先要在头⽂件当中加⼊< string >
声明⽅式也很简单
声明:
string s;//声明⼀个string 对象
string ss[10];//声明⼀个string对象的数组
初始化:
使⽤等号的初始化叫做拷贝初始化,不使⽤等号的初始化叫做直接初始化。

#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
string s;//默认初始化,⼀个空字符串
string s1("ssss");//s1是字⾯值“ssss”的副本
string s2(s1);//s2是s1的副本
string s3=s2;//s3是s2的副本
string s4(10,'c');//把s4初始化
string s5="hiya";//拷贝初始化
string s6=string(10,'c');//拷贝初始化,⽣成⼀个初始化好的对象,拷贝给s6
//string s(cp,n)
char cs[]="12345";
string s7(cs,3);//复制字符串cs的前3个字符到s当中
//string s(s2,pos2)
string s8="asac";
string s9(s8,2);//从s2的第⼆个字符开始拷贝,不能超过s2的size
//string s(s2,pos2,len2)
string s10="qweqweqweq";
string s11(s10,3,4);//s4是s3从下标3开始4个字符的拷贝,超过s3.size出现未定义
return 0;
}
字符串处理:
substr操作:
注意substr没有迭代器作为参数的操作
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
string s="abcdefg";
//s.substr(pos1,n)返回字符串位置为pos1后⾯的n个字符组成的串
string s2=s.substr(1,5);//bcdef
//s.substr(pos)//得到⼀个pos到结尾的串
string s3=s.substr(4);//efg
return 0;
}
如果输⼊的位置超过字符的长度,会抛出⼀个out_of_range的异常
insert操作:
代码来⾃cpp官⽹,经过⾃⼰的整理
注意⽤迭代器当参数和⽆符号数当参数的区别
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
string str="to be question";
string str2="the ";
string str3="or not to be";
string::iterator it;
//s.insert(pos,str)//在s的pos位置插⼊str
str.insert(6,str2); // to be the question
//s.insert(pos,str,a,n)在s的pos位置插⼊str中插⼊位置a到后⾯的n个字符
str.insert(6,str3,3,4); // to be not the question
//s.insert(pos,cstr,n)//在pos位置插⼊cstr字符串从开始到后⾯的n个字符
str.insert(10,"that is cool",8); // to be not that is the question
//s.insert(pos,cstr)在s的pos位置插⼊cstr
str.insert(10,"to be "); // to be not to be that is the question
//s.insert(pos,n,ch)在s.pos位置上⾯插⼊n个ch
str.insert(15,1,':'); // to be not to be: that is the question
//s.insert(s.it,ch)在s的it指向位置前⾯插⼊⼀个字符ch,返回新插⼊的位置的迭代器
it = str.insert(str.begin()+5,','); // to be, not to be: that is the question
//s.insert(s.it,n,ch)//在s的it所指向位置的前⾯插⼊n个ch
str.insert (str.end(),3,'.'); // to be, not to be: that is the question...
//s.insert(it,str.ita,str.itb)在it所指向的位置的前⾯插⼊[ita,itb)的字符串
str.insert (it+2,str3.begin(),str3.begin()+3); // to be, or not to be: that is the question...
return 0;
}
erase操作:
⽤来执⾏删除操作
删除操作有三种
指定pos和len,其中pos为为起始位置,pos以及后⾯len-1个字符串都删除
迭代器,删除迭代器指向的字符
迭代器范围,删除这⼀范围的字符串,范围左闭右开
代码来⾃cpp官⽹
#include <iostream>
#include <string>
int main ()
{
std::string str ("This is an example sentence.");
std::cout << str << '\n';
// "This is an example sentence."
str.erase (10,8); // ^^^^^^^^
//直接指定删除的字符串位置第⼗个后⾯的8个字符
std::cout << str << '\n';
// "This is an sentence."
str.erase (str.begin()+9);// ^
//删除迭代器指向的字符
std::cout << str << '\n';
// "This is a sentence."
// ^^^^^
str.erase (str.begin()+5, str.end()-9);
//删除迭代器范围的字符
std::cout << str << '\n';
// "This sentence."
return 0;
}
append和replace操作:
append函数可以⽤来在字符串的末尾追加字符和字符串。

由于string重载了运算符,也可以⽤+=操作实现repalce顾名思义,就是替换的意思,先删除,后增加。

代码来⾃cpp官⽹,附上⾃⼰的解释
#include <iostream>
#include <string>
int main ()
{
std::string str;
std::string str2="Writing ";
std::string str3="print 10 and then 5 more";
//直接追加⼀个str2的字符串
str.append(str2); // "Writing "
//后⾯追加str3第6个字符开始的3个字符串
str.append(str3,6,3); // "10 "
//追加字符串形参的前5个字符
str.append("dots are cool",5); // "dots "
//直接添加
str.append("here: "); // "here: "
//添加10个'.'
str.append(10u,'.'); // ".........."
//添加str3迭代器范围的字符串
str.append(str3.begin()+8,str3.end()); // " and then 5 more"
//最后这个⽐较特殊,意思是添加5个'A',实际上参数⾥⾯的65对应的asc码就是65
str.append<int>(5,65); // "....."
//字符串追加也可以⽤重载运算符实现
str+="lalala";
std::cout << str << '\n';
return 0;
}
replace的使⽤⽅法,replace⽀持使⽤⽆符号整数寻找位置,也⽀持⽤迭代器寻找位置
#include <iostream>
#include <string>
int main ()
{
std::string base="this is a test string.";
std::string str2="n example";
std::string str3="sample phrase";
std::string str4="useful.";
// replace signatures used in the same order as described above:
// Using positions: 0123456789*123456789*12345
std::string str=base; // "this is a test string."
//第9个字符以及后⾯的4个字符被str2代替
str.replace(9,5,str2); // "this is an example string." (1)
//第19个字符串以及后⾯的5个字符⽤str的第7个字符以及后⾯的5个字符代替
str.replace(19,6,str3,7,6); // "this is an example phrase." (2)
//第8个字符以及后⾯的9个字符⽤字符串参数代替
str.replace(8,10,"just a"); // "this is just a phrase." (3)
//第8个字符以及后⾯的5个字符⽤字符串参数的前7个字符替换
str.replace(8,6,"a shorty",7); // "this is a short phrase." (4)
//第22以及后⾯的0个字符⽤3个叹号替换
str.replace(22,1,3,'!'); // "this is a short phrase" (5)
//迭代器的原理同上
// Using iterators: 0123456789*123456789*
str.replace(str.begin(),str.end()-3,str3); // "sample phrase" (1)
str.replace(str.begin(),str.begin()+6,"replace"); // "replace phrase" (3)
str.replace(str.begin()+8,str.begin()+14,"is coolness",7); // "replace is cool" (4)
str.replace(str.begin()+12,str.end()-4,4,'o'); // "replace is cooool" (5)
str.replace(str.begin()+11,str.end(),str4.begin(),str4.end());// "replace is useful." (6)
std::cout << str << '\n';
return 0;
}
以上的replace操作可以⽤insert和erase的操作组合替换,但是replace操作更加⽅便。

assign操作:
assign操作在⼀起列容器当中都存在,⽐如vector等等。

是⼀个很基本的操作函数,string使⽤assign可以灵活的对其进⾏赋值。

代码来⾃cpp官⽹
#include <iostream>
#include <string>
int main ()
{
std::string str;
std::string base="The quick brown fox jumps over a lazy dog.";
// used in the same order as described above:
//直接把base赋值给str
str.assign(base);
std::cout << str << '\n';
//把base第10个字符以及后⾯的8个字符赋给str
str.assign(base,10,9);
std::cout << str << '\n'; // "brown fox"
//把参数中的0到6个字符串赋给str
str.assign("pangrams are cool",7);
std::cout << str << '\n'; // "pangram"
//直接使⽤参数赋值
str.assign("c-string");
std::cout << str << '\n'; // "c-string"
//给str赋值10个'*'字符
str.assign(10,'*');
std::cout << str << '\n'; // "**********"
//赋值是10个'-'
str.assign<int>(10,0x2D);
std::cout << str << '\n'; // "----------"
//指定base迭代器范围的字符串
str.assign(base.begin()+16,base.end()-12);
std::cout << str << '\n'; // "fox jumps over"
return 0;
}
string的搜索操作:
string类中提供了很多性能优秀,使⽤⽅便的成员⽅法。

⽽且在泛型算法当中也有很多实⽤的技巧。

find和rfind函数:
find函数主要是查找⼀个字符串是否在调⽤的字符串中出现过,⼤⼩写敏感。

代码来⾃cpp官⽹
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
std::string str ("There are two needles in this haystack with needles.");
std::string str2 ("needle");
// different member versions of find in the same order as above:
//在str当中查找第⼀个出现的needle,找到则返回出现的位置,否则返回结尾
std::size_t found = str.find(str2);
if (found!=std::string::npos)
std::cout << "first 'needle' found at: " << found << '\n';
//在str当中,从第found+1的位置开始查找参数字符串的前6个字符
found=str.find("needles are small",found+1,6);
if (found!=std::string::npos)
std::cout << "second 'needle' found at: " << found << '\n';
//在str当中查找参数中的字符串
found=str.find("haystack");
if (found!=std::string::npos)
std::cout << "'haystack' also found at: " << found << '\n';
//查找⼀个字符
found=str.find('.');
if (found!=std::string::npos)
std::cout << "Period found at: " << found << '\n';
//组合使⽤,把str2⽤参数表中的字符串代替
// let's replace the first needle:
str.replace(str.find(str2),str2.length(),"preposition");
std::cout << str << '\n';
return 0;
}
rfind函数就是找最后⼀个出现的匹配字符串,返回的位置仍然是从前往后数的。

#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
std::string str ("The sixth sick sheik's sixth sheep's sick.");
std::string key ("sixth");// ^
//rfind是找最后⼀个出现的匹配字符串
std::size_t found = str.rfind(key);
if (found!=std::string::npos)
{
cout<<found<<endl;//输出23
str.replace (found,key.length(),"seventh");//找到的sixth替换成seventh
}
std::cout << str << '\n';
return 0;
}
查找的效率⾮常⾼,我没看过stl源码剖析,但是感觉是⽤kmp实现的。

呵呵,可以⾃⼰写⼀个。

find_….of函数:
find_first_of(args) 查找args中任何⼀个字符第⼀次出现的位置
find_last_of(args) 最后⼀个出现的位置
find_fist_not_of(args) 查找第⼀个不在args中的字符
find_last_not_of 查找最后⼀个不在args中出现的字符
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
std::string str1 ("Please, replace the vowels in this sentence by asterisks.");
std::size_t found1 = str1.find_first_of("aeiou");
//把所有元⾳找出来⽤*代替
while (found1!=std::string::npos)
{
str1[found1]='*';
found1=str1.find_first_of("aeiou",found1+1);
}
std::cout << str1 << '\n';
//在str2中找到第⼀个不是消协英⽂字母和空格的字符
std::string str2 ("look for non-alphabetic characters...");
std::size_t found2 = str2.find_first_not_of("abcdefghijklmnopqrstuvwxyz ");
if (found2!=std::string::npos)
{
std::cout << "The first non-alphabetic character is " << str2[found2];
std::cout << " at position " << found2 << '\n';
}
return 0;
}
find_last_of和find_last_not_of与first基本相同,就不写例⼦代码了。

⽐较与转换:
类似c语⾔的字符串⽐较函数strcmp函数⼀样,⽀持字符串⽐较操作,同时也类似python、C#语⾔中的函数⼀样,⽀持把数字和字符串转换。

有些特性是C++11当中才有。

注意编译器bug:
在MinGW编译器当中如果版本低于3.8,虽然⽀持c++11但是⾥⾯有⼀个bug,就是不⽀持字符串和数组的转换!要更新MinGW的版本才可以,或者直接使⽤g++。

compare函数:
和strcmp函数⼀样,如果两个字符串相等,那么返回0,调⽤对象⼤于参数返回1,⼩于返回-1。

在compare当中还⽀持部分⽐较,⾥⾯有6个参数可以设置。

#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
string s1="123",s2="123";
cout<<pare(s2)<<endl;//0
s1="123",s2="1234";
cout<<pare(s2)<<endl;//-1
s1="1234",s2="123";
cout<<pare(s2)<<endl;//1
std::string str1 ("green apple");
std::string str2 ("red apple");
if (pare(str2) != 0)
std::cout << str1 << " is not " << str2 << '\n';
//str1的第6个字符以及后⾯的4个字符和参数⽐较
if (pare(6,5,"apple") == 0)
std::cout << "still, " << str1 << " is an apple\n";
if (pare(str2.size()-5,5,"apple") == 0)
std::cout << "and " << str2 << " is also an apple\n";
//str1的第6个字符以及后⾯的4个字符和str2的第4个字符以及后⾯的4个字符⽐较
if (pare(6,5,str2,4,5) == 0)
std::cout << "therefore, both are apples\n";
return 0;
}
由于string重载了运算符,可以直接⽤>,<,==来进⾏⽐较,也很⽅便。

数值转换:
在io的部分有过数值和字符串相互转换的例⼦,使⽤的是stringstream函数,在c++11当中有定义好的现成的函数取调⽤,⾮常⽅便。

string和数值转换
to_string(val)把val转换成string
stoi(s,p,b)把字符串s从p开始转换成b进制的int
stol(s,p,b)long
stoul(s,p,b)unsigned long
stoll(s,p,b)long long
stoull(s,p,b)unsigned long long
stof(s,p)float
stod(s,p)double
stold(s,p)long double
//注意,下段代码在MinGw中会报错!即使使⽤c++11编译也⼀样,⽆法识别to_string!
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
string s1;
s1=to_string(100);
cout<<s1<<endl;
int a=stoi(s1,0,10)+1;
cout<<a<<endl;
return 0;
}
总结
以上所述是⼩编给⼤家介绍的C++中string的⽤法和例⼦,希望对⼤家有所帮助,如果⼤家有任何疑问请给我留⾔,⼩编会及时回复⼤家的。

在此也⾮常感谢⼤家对⽹站的⽀持!。

相关文档
最新文档