string
- string() 创建一个默认的string类,长度为0(默认构造函数)
string str();
- string(const char*s) 将string对象初始化为s指向的字符串
string str("hello")
- string(const char *c,size_t n) 将字符串前五个字符初始化给string对象
string str("hello word",5);
- string (const string &str) 将一个string对象初始化为string对象str(复制构造函数)
string str("hello");
string str2(str)
- string (const string &str,size_t pos,size_t len =npos)将str中pos位置往后npos个字符初始化给string对象
string str("hello");
string str1(str,3,2); ///lo
-
string (size_t n, char c); 初始化string对象10个c字符
string str(10,'x');
string str(10,42);42的ascll是'*';
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
string str("hello word");
string str1(str);
string str2(str,5,2);
string str3("hello word",5);
string str4(10,42);
cout<<"str:"<<str<<"\nstr1:"<<str1<<"\nstr2:"<<str2<<"\nstr3:"<<str3<<"\nstr4:"<<str4<<endl;
return 0;
}
2. string::append
在当前字符串末尾附加字符串
用法基本都差不多
这里就直接上代码了
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
string str("hello");
str.append("ZZZZ");
str.append("BBBBBBBB",2,4);
str.append("CCCC");
str.append("GGGGGGGGG",4);
str.append(4,'H');
cout<<str<<endl;
return 0;
}
3.string::assgin
为字符串指定一个新的值,替换当前内容
#include<iostream>
using namespace std;
int main()
{
string str("hello");
str.assign("AAAA");
cout<<str<<endl;
str.assign("BBBBBBBB",2,4);
cout<<str<<endl;
str.assign("XXXXXXXX");
cout<<str<<endl;
str.assign("GGGGGGGG",4);
cout<<str<<endl;
str.assign(4,'K');
cout<<str<<endl;
return 0;
}
4.string::at
返回string中pos位置的字符
5. string::back
返回最后一个字符
6. string::begin(如果是rbegin表示指向字符串最后一个字符)
返回指向第一个字符的迭代器
7.string::end(如果是rend表示指向第一个字符)
指向结尾字符后面的位置
#include<iostream>
using namespace std;
int main()
{
string str("hello word");
for(auto it=str.begin();it!=str.end();it++)//返回值是迭代器类型所以用auto自动推>导
{
printf("%c",*it);
}
return 0;
}
8. string::c_str
获取c字符串
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
string str("hello word bit");
char* cstr=new char[str.length()+1];
strcpy(cstr,str.c_str());
cout<<cstr<<endl;
return 0;
}
9.string::clear
删除字符串变成空
10.string::copy
将字符串对象当前值的子字符串复制到由s指向的数组中。该子字符串包含从位置pos开始的len字符。
#include<iostream>
using namespace std;
int main()
{
string str("hello word");
char buf[20];
size_t length=str.copy(buf,4,6);
buf[length]='\0';
cout<<buf<<endl;
return 0;
}
11.string ::erase
删除元素
#include<iostream>
using namespace std;
int main()
{
string str("this ccc an bbbb apple");
cout<<str<<endl;
str.erase(5,4);
cout<<str<<endl;
str.erase(str.begin()+8);//删除第8个字符
cout<<str<<endl;
str.erase(str.begin()+8,str.end()-6);
cout<<str<<endl;
return 0;
}
12string::find
return:
成功:第一个匹配的第一个字符的位置。
失败:函数返回string::npos。
#include<iostream>
using namespace std;
int main()
{
string str("The journey has not been made has in vain");
string str2("has");
size_t found=str.find(str2);
if(found!=string::npos)
{
cout<<"first 'has' found at:"<<found<<endl;
}
found=str.find("has",found+1,3);
if(found!=string::npos)
{
cout<<"second 'has' found at:"<<found<<endl;
}
found=str.find("not");
if(found!=string::npos)
{
str.replace(found,str2.length(),"DDDDD");
}
cout<<str<<endl;
return 0;
}
13.string::find_frist_not_of
在字符串中搜索与参数中指定的任何字符都不匹配的第一个字符。
#include<iostream>
using namespace std;
int main()
{
string str("hello word");
string str1("has");
size_t found= str.find_first_not_of(str1,0);
cout<<str[found]<<endl;
found= str.find_first_not_of("has",0);
cout<<str[found]<<endl;
found=str.find_first_not_of("has",2,3);
cout<<str[found]<<endl;
found=str.find_first_not_of('l',0);
cout<<str[found]<<endl;
return 0;
}
13.string::find_last_not_of
在字符串中搜索与参数中指定的任何字符都不匹配的最后一个字符。
14.string::insert
#include<iostream>
using namespace std;
int main()
{
string str("hello word");
str.insert(0,"BBB");
cout<<str<<endl;
str.insert(0,"AAACCCDDD",3,3);
cout<<str<<endl;
str.insert(0,"GGGGGGG",3);
cout<<str<<endl;
str.insert(0,3,'E');
cout<<str<<endl;
str.insert(str.begin(),'Q');
cout<<str<<endl;
return 0;
}
15.string::reserve
16.string::substr
17.string::swap
#include<iostream>
using namespace std;
int main()
{
string str("hello word");
string str1("bit");
str1.swap(str);
cout<<str1<<endl;
return 0;
}