自学内容网 自学内容网

C++中string函数用法总结

一,string的构造函数

string()  //无参构造,初始化为空串
string(const string& str)  //用str拷贝构造
string(size_t n,char c)   //用n个字符c初始化

string(const char* s,size_t n) //用字符串s的前n个字符初始化
string(const string& str,size_t pos,size_t len=npos) //将字符串str,从下标pos位置开始,选取长度为len个的字符,来初始化

string str1;                   //空串
string str2("hello world");   //用"hello world"拷贝构造str2
string str3(4, 'x');          //用4个字符'x'初始化
string str4(str2, 5, 6);      //用str2中,从下标为5位置开始,长度为6的字符串初始化
string("hello world", 5);     //用字符串"hello world" 前5个字符初始化

 

string(const string& str,size_t pos,size_t len=npos)  

注:上面的缺省值npos,定义为:size_t npos=-1. npos为最大值,表示不传参数时,会用str中pos位置开始后的所有字符来初始化

二,string的大小和容量

size()和length():返回string对象的字符个数

capacity():string对象的容量,也就是可以存储字符的最大个数

string str("hello world");
cout << "size==" << str.size() << endl;
cout << "length==" << str.length() << endl;
cout << "capacity==" << str.capacity() << endl;

 

三,string的插入 

insert

string str1("hello world");
str1.insert(5, "xxxxxx");  //在5位置之前插入字符串"xxxxxx"
cout << str1 << endl;

string str2("hello world");
str2.insert(5, 2, '%');   //在5位置之前插入2个"%"
cout << str2 << endl;

string str3("hello world");
string::iterator it1 = str3.begin();
str3.insert(it1, '#');    //在开始位置之前插入'#'
cout << str3 << endl;

string str4("hello world");
string::iterator it2 = str4.begin();
str4.insert(it2, 2, '#'); //在开始位置之前插入两个'#'
cout << str4 << endl;

 push_back(尾插)

string s;         //空串
s.push_back('a'); //尾插
s.push_back('c');
s.push_back('b');  //s中存放的字符串为"abc"

四,string的遍历

迭代器遍历或者下标遍历

string str1("hello world");

//下标遍历
for (int i = 0; i < str1.size(); i++)
{
cout << str1[i];
}
cout << endl;


//迭代器遍历
//正向迭代器
string::iterator it1 = str1.begin();
while (it1 != str1.end())
{
cout << *it1;
it1++;
}
cout << endl;

//反向迭代器
string::reverse_iterator it2 = str1.rbegin();
while (it2 != str1.rend())
{
cout << *it2;
it2++;
}
cout << endl;

//const正向迭代器
const string s("hello world");
string::const_iterator cit = s.begin();
while (cit != s.end())
{
cout << *cit;
cit++;
}
cout << endl;

//const反向迭代器
string::const_reverse_iterator crit = s.rbegin();
while (crit != s.rend())
{
cout << *crit;
crit++;
}
cout << endl;

 五,string的删除

erase(iterator p);                                //删除p所指位置的字符
erase(iteraror first,iterator last)          //迭代器区间删除,删除first到last区间的字符
erase(size_t pos,size_t len=npos)    //删除pos位置开始的len个字符

string str("hello world");
string::iterator it = str.begin();

str.erase(it);            //删除开始位置字符
cout << str << endl;

str.erase(it, it + 2);   //删除迭代器区间
cout << str << endl;

str.erase(5, 1);        //删除下标为5的1个字符
cout << str << endl;

 六,string的字符替换

replace(size_t pos,size_t n,const char* s)         //将pos位置开始的n个字符替换成字符串s
replace(size_t pos,size_t n,size_t n1,char c)     //将pos位置开始的n个字符,替换成n1个字符c
replace(iterator it1,iterator it2,const char* s) //将迭代器区间的字符串替换成串s

string str("hello world");
str.replace(5, 1, "%%");  //将5位置的1个字符替换成字符串s
cout << str << endl;

str.replace(1, 1, 2, 'h');  //将1位置的1个字符替换成2个字符'h'
cout << str << endl;

str.replace(str.begin(), str.begin() + 2, "xx");
cout << str << endl;

七,string的分割(截取子串)

string str("hello world");
string s = str.substr(2, 5);  //从2位置开始截取5个字符的子串
cout << s << endl;

八,string的查找

find(char c,size_t pos=0)    //从pos位置开始查找字符c,返回下标,查找失败返回-1
find(const char* s,size_t pos=0)    //从pos位置开始查找子串s,返回下标,查找失败返回-1
rfind(char c,size_t pos=npos)   //从pos位置开始反向查找字符c,返回下标,查找失败返回-1
rfind(const char* s,size_t pos=npos)  //从pos位置开始反向查找子串s,返回下标,查找失败返回-1

string s("test.cpp.zip");
size_t pos = s.find('.');      //正向查找
string s2 = s.substr(pos);
cout <<"s2=="<< s2 << endl;

  

string s("test.cpp.zip");
size_t pos = s.rfind('.');      //反向查找
string s2 = s.substr(pos);
cout <<"s2=="<< s2 << endl;

 find_first_of(const char* s,size_t pos=0)  //从pos位置开始,查找子串s,返回位置,-1表示找不到
 find_first_not_of(const char* s,size_t pos=0) //从pos位置开始,查找第一个不位于子串s的字符,返回位置,-1表示找不到
find_last_of(const char* s,size_t pos=npos)   //从pos位置开始,查找最后一个位于子串s的字符,返回位置,-1表示找不到
find_last_not_of(const char* s,size_t pos=npos)//从pos位置开始,查找最后一个不位于子串s的字符,返回位置,-1表示找不到

string str("Please, replace the vowels in this sentence by asterisks.");
cout << str << endl;
size_t found = str.find_first_of("abcdef"); //在str中找字符a b c d e f
while (found != string::npos)
{
str[found] = '*'; //将找到的位置改成'*'
found = str.find_first_of("abcdef",found+1);
}
cout << str << endl;

 也可以用作分割路径

void SplitFileName(string s)
{
cout << s << endl;
size_t found = s.find_last_of("\\/");

cout << "path:" << s.substr(0,found) << endl;
cout << "file:" << s.substr(found+1) << endl;

}
void test_string10()
{
string str1("usr/bin/bash");
string str2("D:\\code2\\pratice-1\\pratice-1\\x64");

SplitFileName(str1);
SplitFileName(str2);
}

九,operator+和opertor+= 

string s1("hello world");
string s2 = s1 + "xxx";
s1 += "xxx";
cout << s1 << endl;
cout << s2 << endl;


原文地址:https://blog.csdn.net/2401_82677021/article/details/142871837

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!