string的构造函数
void Strwrite()
{
string s1(5,'a');//5代表长度,a输入的字符
cout<<"s1:"<<s1<<endl;
cout<<endl;
string s2("asdfghjkl");
cout<<"s2"<<s2<<endl;
cout<<endl;
string s3(s2,3,5);//3是下标,5代表长度
cout<<"s3:"<<s3<<endl;
cout<<endl;
string s4(s2);//直接把s2复制到s4
cout<<"s4:"<<s4;
}
输出结果
string的属性
void StrShuxing()
{
string s1(5,'a');
cout<<"s1的容量:"<<s1.capacity()<<endl;
string s2("asdfghjkl");
s2.reserve(9);//reserve修改容量只能变大不能变小;
cout<<"s2的容量:"<<s2.capacity()<<endl;//s2的原本容量为9,修改不成1
s2.reserve(30);
cout<<"s2再次修改后的容量:"<<s2.capacity()<<endl;
cout<<"s2的长度:"<<s2.length()<<endl;//length()求长度
cout<<"s2的字符数:"<<s2.size()<<endl;//size()//求字符数
string s3("asdfghjkl");
s3.resize(3);//重新设置字符个数,容量不变
cout<<"s3的容量:"<<s3.capacity()<<endl;
cout<<"s3的长度:"<<s3.length()<<endl;
cout<<"s3的字符数:"<<s3.size()<<endl;
}
输出结果
String的输出
void Strshuchu()
{
//全部输出
string s1(5,'a');
cout<<"s1:"<<s1<<endl;
cout<<"s1:"<<s1.c_str()<<endl; //c_str()
cout<<endl;
//输出单个字符
string s2("asdfghjkl");
cout<<"s2中下标为3的字符:"<<s2[3]<<endl;
cout<<"s2中下标为3的字符:"<<s2.at(3)<<endl;//at()
}
输出结果
string的修改
修改指定元素
void Strxiugai()
{
string s2("asdfghjkl");
s2[2]='2';//下标修改 结果为s2:as2fghjkl
s2.at(3)='3';//at()修改 s2:as23ghjkl
cout<<"s2:"<<s2<<endl;
}
中间插入
void Strxiugai()
{
string s2("asdfghjkl");
s2.insert(2,"qwert");//在s2下标为2的地方插入
cout<<s2<<endl;
string s1("11223");
s2.insert(1,s1); //在s2下标为1的地方插入 s1;
cout<<s2<<endl;
string s3("woaini");
string s4("hahahahaha");
s3.insert(1,s4,2,5);//在s3下标为1的地方插入s4中从下标为2开始长度为5的字符串
cout<<s3<<endl;
s4.insert(2,"1234",2);//在s4下标为2的地方插入"1234"中前两个字符
cout<<s4<<endl;
string s5("ASD123");
s5.insert(2,6,'Q');//在s5下标为2的地方插入6个Q
cout<<s5<<endl;
}
输出结果
#include < iostream >
#include < algorithm >
#include < string >
using namespace std;
void StrChange()
{
string str("asdfghj");
string s1("123");
str.append(s1);//将字符串S1拼接在str末尾
cout<<str<<endl;
s1.append("QWER");//拼接一个字符串
cout<<s1<<endl;
string s2=("hahah");
s2.append(str,2,5);//在s2的末尾拼接 s1从下标为2开始长度为5的字符串
cout<<s2<<endl;
s2.append("098",2);//在s2的末尾拼接“098”的前两位数
cout<<s2<<endl;
s2.append(9,'A');//在s2的末尾拼接 9 个A字符
cout<<s2<<endl;
}
void StrNew()
{//重新赋值,修改的是已经存在的,不存在的不能直接进行重新赋值
cout<<"重新赋值"<<endl;
string a("123098465");
a[1]='A';//下标运算赋值;
cout<<a<<endl;
a.assign("QWERT");
cout<<a<<endl;
string b("hello i love you");
a.assign(b);
cout<<a<<endl;
a.assign(b,0,5);
cout<<a<<endl;
a.assign(10,'h');
cout<<a<<endl;
}
void StrOperater()
{
cout<<"string的比较"<<endl;
string st1("Hello world!");
string st2("hello world!");
string st3("Hello world!");
cout<<(st1<st2)<<endl;//真返回1,假返回0;
cout<<(st1<st3)<<endl;
/*compare比较 相等返回0
小于返回-1
大于返回1*/
cout<<st1.compare(st3)<<endl;
cout<<st1.compare(st2)<<endl;
cout<<st2.compare(st1)<<endl;
cout<<st1.compare(1,2,st2)<<endl;//compare(index,length,string)
cout<<st1.compare(1,2,st2,1,2)<<endl; //compare(index,length,string,index,length)
cout<<"string的复制"<<endl;
char c[20]={0};
st1.copy(c,0,3);
cout<<st1<<endl;
cout<<"string查找子串"<<endl;
cout<<st1.find("world",0)<<endl; //在st1中查找world子串,找到后返回下标
cout<<(int)st1.find("hello",0)<<endl;//找不到时,强制类型转换输出-1
cout<<"返回子串"<<endl;
cout<<st1.substr(2,4)<<endl; //返回下标2到4的子串
cout<<"字符串的拼接用加法"<<endl;
cout<<st1+st2<<endl;
}
void StrIterator()
{
cout<<"string的迭代器"<<endl;
string zifu("GANGGANG");
string::iterator ite;
string::iterator ite1;
ite=zifu.begin();
ite1=zifu.begin();
cout<<"1.遍历输出"<<endl;
for(int i=0;i<zifu.size();i++)
{
cout<<*ite<<endl;
ite++;
}
cout<<"2.遍历输出"<<endl;
for(ite1;ite1!=zifu.end();ite1++)
{
cout<<*ite1<<" ";
}
cout<<endl;
cout<<"3.赋值"<<endl;
/*第一种方法
for(ite;ite!=zifu.end();ite++)
{
*ite='a';
}*/
/*第二种*/
for(int i=0;i<zifu.size();i++)
{
ite[i]=='a';
}
/*下标直接赋值*/
ite[2]='o';
}
void StrHanshu()
{
cout<<"与迭代器相关的函数"<<endl;
string string1("WHY18811881");
string string2("not1919919");
//string::iterator ite;
string1.append(string2.begin(),string2.end());//末尾添加
cout<<string1<<endl;
string1.erase(10);//删除后还是10个字符
cout<<string1<<endl;
string1.erase(string1.begin()+2);// 删除下标为2的字符
cout<<string1<<endl;
}
void fun(char c)
{
cout<<c<<" ";
}
void Stryongfa()
{
string first("hello,everyone");
sort(first.begin(),first.end());//从小到大排序
for_each(first.begin(),first.end(),fun);//for_each输出
cout << endl;
sort(first.begin(),first.end(),greater<char>()); //从大到小
for_each(first.begin(),first.end(),fun);//for_each输出
}
int main ()
{
StrNew ();
StrChange ();
StrOperater ();
StrIterato r();
StrHanshu ();
Stryongfa ();
return 0;
}