#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
//1.string 的构造
void func1()
{
string s1("hello");
string s2 = "123";
string s3 = s1;
string s4(10,'a');
cout << s4 << endl;
}
//2.遍历
void func2()
{
string s2 = "123";
cout << s2 << endl;
//1.通过下标
for(unsigned int i = 0; i < s2.size();i++)
{
cout << s2.at(i);
}
//string 也有迭代器
string ::iterator it;
for(it = s2.begin();it != s2.end();it++)
{
cout << *it;
}
cout << endl;
//[]和at的区别:[]越界的时候不会抛出异常,at在访问越界的时
候会抛出异常
try
{
for(unsigned int i = 0;i < s2.size();i++)
{
cout << s2.at(i);
}
}
catch (exception& e)
{
printf("%s\n",e.what());
}
}
//3.string 和 char* 的转换
void func3()
{
string str = "hello";
printf("str = %s\n",str.c_str());
}
//4.string 的查找和替换
void func4()
{
string str = "1111 hello 2222 hello 3333 hello 4444
hello";
//字符串查找,如果存在返回的是第一个找到的下标
//找不到,返回-1
int index = str.find("hello",0);
while(index != -1)
{
cout << index << endl;
index = str.find("hello",index+1);
}
//替换
//第一个参数,替换的起始位置
//第二个参数,从起始位置开始,删除这么多个字符
//第三个参数,要插入的数据
index = str.find("hello",0);
while(index != -1)
{
str.replace(index,5,"123");
index = str.find("hello",index+1);
}
cout << str << endl;
}
//5.删除和插入
void func5()
{
string str = "hello";
//通过迭代器单个字符,删除
str.erase(str.begin());
cout << str << endl;
//区间删除:通过迭代器
//区间删除 是 左闭右开的 [str.begin(),str.begin()+3)
str.erase(str.begin(),str.begin()+3);
cout << str << endl;
string str1 = "asdfghjjhgfds";
str1.erase(str1.begin(),str1.end());
cout << str1 << endl;
string str2 = "asdfghjk";
//可以通过下标删除
str2.erase(1,4);
cout << str2 << endl;
//插入
str2.insert(0,"1111");
cout << str2 << endl;
str2.insert(str2.size(),"2222");
cout << str2 << endl;
str2.insert(8,"3333");
cout << str2 << endl;
}
//6.连接 比较
void func6()
{
string s1 = "hello";
string s2 = s1 + "world";
cout << s2 << endl;
if(s1 == s2)
{
cout << "s1 == s2";
}
else
cout << "s1 != s2" << endl;
}
//7.算法
void func7()
{
string str = "1111hello2222hello";
//转换函数算法
transform(str.begin(),str.end(),str.begin(),toupper);
cout << str << endl;
//str = "1111hello2222hello";
transform(str.begin(),str.end(),str.begin(),tolower);
cout << str << endl;
}
int main()
{
func1();
func2();
func3();
func4();
func5();
func6();
func7();
return 0;
}
#include <string>
#include <algorithm>
using namespace std;
//1.string 的构造
void func1()
{
string s1("hello");
string s2 = "123";
string s3 = s1;
string s4(10,'a');
cout << s4 << endl;
}
//2.遍历
void func2()
{
string s2 = "123";
cout << s2 << endl;
//1.通过下标
for(unsigned int i = 0; i < s2.size();i++)
{
cout << s2.at(i);
}
//string 也有迭代器
string ::iterator it;
for(it = s2.begin();it != s2.end();it++)
{
cout << *it;
}
cout << endl;
//[]和at的区别:[]越界的时候不会抛出异常,at在访问越界的时
候会抛出异常
try
{
for(unsigned int i = 0;i < s2.size();i++)
{
cout << s2.at(i);
}
}
catch (exception& e)
{
printf("%s\n",e.what());
}
}
//3.string 和 char* 的转换
void func3()
{
string str = "hello";
printf("str = %s\n",str.c_str());
}
//4.string 的查找和替换
void func4()
{
string str = "1111 hello 2222 hello 3333 hello 4444
hello";
//字符串查找,如果存在返回的是第一个找到的下标
//找不到,返回-1
int index = str.find("hello",0);
while(index != -1)
{
cout << index << endl;
index = str.find("hello",index+1);
}
//替换
//第一个参数,替换的起始位置
//第二个参数,从起始位置开始,删除这么多个字符
//第三个参数,要插入的数据
index = str.find("hello",0);
while(index != -1)
{
str.replace(index,5,"123");
index = str.find("hello",index+1);
}
cout << str << endl;
}
//5.删除和插入
void func5()
{
string str = "hello";
//通过迭代器单个字符,删除
str.erase(str.begin());
cout << str << endl;
//区间删除:通过迭代器
//区间删除 是 左闭右开的 [str.begin(),str.begin()+3)
str.erase(str.begin(),str.begin()+3);
cout << str << endl;
string str1 = "asdfghjjhgfds";
str1.erase(str1.begin(),str1.end());
cout << str1 << endl;
string str2 = "asdfghjk";
//可以通过下标删除
str2.erase(1,4);
cout << str2 << endl;
//插入
str2.insert(0,"1111");
cout << str2 << endl;
str2.insert(str2.size(),"2222");
cout << str2 << endl;
str2.insert(8,"3333");
cout << str2 << endl;
}
//6.连接 比较
void func6()
{
string s1 = "hello";
string s2 = s1 + "world";
cout << s2 << endl;
if(s1 == s2)
{
cout << "s1 == s2";
}
else
cout << "s1 != s2" << endl;
}
//7.算法
void func7()
{
string str = "1111hello2222hello";
//转换函数算法
transform(str.begin(),str.end(),str.begin(),toupper);
cout << str << endl;
//str = "1111hello2222hello";
transform(str.begin(),str.end(),str.begin(),tolower);
cout << str << endl;
}
int main()
{
func1();
func2();
func3();
func4();
func5();
func6();
func7();
return 0;
}