#include
#include
using namespace std;
/*string类属于常用容器,#include
//string容器的基本初始化
void test1()
{
string s1;
s1 = "helloworld"; //赋值
cout << s1 << endl;
char str[] = "abcdeffdgfh";
string s2(str); //调用拷贝构造
cout << s2 << endl;
string s3(s2,6); //将s2开始于6位置的字符串赋值给s3
cout << s3 << endl;
string s4(s2,6,3); //将s2开始于6位置的3个字符串赋值给s4
cout << s4 << endl;
string s5(str,6); //将c字符串的前6个字符赋值给S5
cout << s5 << endl;
string s6(6,'A'); //将s6赋值为6个A
cout << s6 << endl;
string s7(s6.begin(),s6.begin()+2); //将s6区间begin到begin+2赋值给s7
cout << s7 << endl;
string s8(s2.begin(),s2.end()); //容器的开始和结束,迭代器
cout << s8 << endl;
}
//string容器的基本操作
void test2()
{
string s1 = "Li";
string s2 = "Li";
if (s1 == s2)
{
cout << s1 << " = " << s2 << endl;
}
string s3 = "Ab";
if (s2 > s3)
{
cout << s2 << "大于" << s3 << endl;
}
else
{
cout << s2 << "小于" << s3 << endl;
}
string s4 = "Yb";
if (s2 > s4)
{
cout << s2 << "大于" << s4 << endl;
}
else
{
cout << s2 << "小于" << s4 << endl;
}
s4 += "welcome";
cout << "s4" << s4 << endl;
#if 0
for (int i = 0; i < s4.size(); i++)
{
cout << s4[i] << " ";
}
cout << endl;
#endif
}
//string容器的常用函数
void test3()
{
string s1;
if (s1.empty()) //判断string容器是不是空:empty()
{
cout << "s1 为空" << endl;
}
else
{
cout << "s1不为空" << endl;
}
int ret = s1.capacity(); //返回当前string容器的容量,即不用增加内存可以存放的数据:capacity
cout << "s1的容量:" << ret << endl;
s1 = "hello";
ret = s1.size(); //返回当前字符串的大小s1.size()
cout << s1 << " 的大小:" << ret << endl;
cout << "s1的容量:" << s1.capacity() << endl;
cout << s1 << " 的长度:" << s1.length() << endl; //返回当前字符串的长度s1.length()
string s2 = "helloworld";
cout << s2 << "重置为:";
s2.resize(20, 'A'); //将string重置为5个A,多退少补,resize(),重置长度为20,原长度不够,用‘A’补齐,比+=灵活点;
cout << s2 << endl;;
}
void test4()
{
string s1 = "study hard and make progress everyday! make every day!!";
int pos;
pos = s1.find("make"); //从第0位开始查找as位置,找到返回第一次出现as的位置。否则返回npos
if (pos == string::npos)
{
cout << "没有找到make" << endl;
}
else
{
cout << "make第一次出现在:" << pos << endl;
}
pos = s1.find("make", 10); //从第0位开始查找as位置,找到返回第一次出现as的位置。否则返回npos
if (pos == string::npos)
{
cout << "没有找到make" << endl;
}
else
{
cout << "make第一次出现在:" << pos << endl;
}
pos = s1.rfind("make"); //反向查找
if (pos == string::npos)
{
cout << "没有找到make" << endl;
}
else
{
cout << "make第一次出现在:" << pos << endl;
}
pos = s1.rfind("make", 50);
if (pos == string::npos)
{
cout << "没有找到make" << endl;
}
else
{
cout << "make第一次出现在:" << pos << endl;
}
pos = s1.find_first_of("make"); //找到make第一次出现的位置
if (pos == string::npos)
{
cout << "没有找到make" << endl;
}
else
{
cout << "first make第一次出现在:" << pos << endl;
}
pos = s1.find_first_not_of("make"); //第一次没有出现的位置
if (pos == string::npos)
{
cout << "没有找到make" << endl;
}
else
{
cout << "first not make第一次没有在:" << pos << endl;
}
pos = s1.find_last_of("make"); //找到make最后一次出现的位置
if (pos == string::npos)
{
cout << "没有找到make" << endl;
}
else
{
cout << "last make最后在:" << pos << endl;
}
pos = s1.find_last_of("make"); //
if (pos == string::npos)
{
cout << "没有找到make" << endl;
}
else
{
cout << "last not make没有在:" << pos << endl;
}
}
//其他常用函数
void test5()
{
string s1 = "studyhardandmakeprogresseveryday!makeeveryday!!";
cout << "原始数据" << endl;
cout << s1.size() << endl;
const char *p1 = NULL;
p1 = s1.c_str(); //返回一个空结束的字符数组
printf("%d\n", strlen(p1));
//cout << "返回一个空结束的字符数组:" << *p1 << endl;
s1.insert(2, "123"); //在2位置插入123
cout << "在2位置插入123:" << s1 << endl;
s1.replace(0, 2, "AAA"); // 删除0开始的两个字符,并且替换成AA
cout << "删除0开始的两个字符,并且插入换成AA:" << s1 << endl;
string str1 = s1.erase(0, 5); //删除0位置开始的五个字符,返回字符串
cout << "删除0位置开始的五个字符:" << str1 << endl;
cout << "s1删除0位置开始的五个字符:" << s1 << endl;
string str = s1.substr(10, 10); //返回第十个位置开始的10个字符的串
cout << "s1返回第十个位置开始的10个字符的串:" << s1 << endl;
cout << "返回第十个位置开始的10个字符的串:" << str << endl;
s1.swap(str); //交换两个字符串
cout << "交换两个字符串str:" << str << endl;
cout << "交换两个字符串s1:" << s1 << endl;
s1.append(str); //把str连接到s1的末尾(相当于+=)
cout << "把str连接到s1的末尾:" << s1 << endl;
s1.push_back('A'); //在s1末尾加上A
cout << "在s1末尾加上A:" << s1 << endl;
const char *p;
p = s1.data(); //返回一个非空结束的字符数组
printf("%d\n", strlen(p));
printf("%s\n", p);
}
int main()
{
//test1();
//test2();
//test3();
//test4();
//test5();
system("PAUSE");
return 0;
}