马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 这是她 于 2020-6-10 21:46 编辑
We are most dive when we are in love. ——John Updike
STL(标准模板库)
STL六大组件:容器、算法、迭代器、仿函数、适配器(配接器)、空间配置器
String
String 本质上是一个类,类内部封装了char *,管理这个字符串,是一个char*型的容器
char*是一个指针
特点:
string 类内部封装了很多成员方法
string管理char*所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责
#include<iostream>
#include<string>
using namespace std;
void test1()
{
//string(); 创建一个空的字符串
//string(const char* s); 使用字符串s初始化
//string(const string& str); 使用一个string对象初始化另一个string对象
//string(int n,char c); 使用n个字符c初始化
string s1;
const char * str = "花甲粉";
string s2(str);
cout << "s2 = " << s2 << endl;
string s3(s2);
cout << "s3 = " << s3 << endl;
string s4(10,'h');
cout << "s4 = " << s4 << endl;
}
void test2()
{
//string& opertor=(const char* s); char*类型字符串赋值给当前的字符串
//string& opertor=(const string &s); 把字符串s赋给当前的字符串
//string& operator=(char c); 字符赋值给当前的字符串
//string& assgin(const char* s); 把字符串s赋给当前的字符串
//string& assgin(const char* s,int n); 把字符串s的前n个字符赋给当前的字符串
//string& assgin(const string &s); 把字符串s赋给当前字符串
//string& assgin(int n,char c); 用n个字符c赋给当前字符串
string str1;
str1 = "bang";
cout << "str1 = " << str1 << endl;
string str2;
str2 = str1;
cout << "str2 = " << str2 << endl;
string str3;
str3 = 'h';
cout << "str3 = " << str3 << endl;
string str4;
str4.assign("i love you!");
cout << "str4 = " << str4 << endl;
string str5;
str5.assign("hello world!",5);
cout << "str5 = " << str5 << endl;
string str6;
str6.assign(str5);
cout << "str6 = " << str6 << endl;
string str7;
str7.assign(10,'i');
cout << "str7 = " << str7 << endl;
}
void test3()
{
//string& operator+=(const char* str); 重载+=操作符
//string& operator+=(const char c); 重载+=操作符
//string& operator+=(const string& str); 重载+=操作符
//string& append(const char *s); 把字符串s连接到当前的字符串结尾
//string& append(const char *s,int n); 把字符串s的前n个字符串连接到当前字符串结尾
//string& append(const string &s); operator+=(const string& str);
//string& append(const string &s,int pos,int n); 字符串s中从pos开始的n个字符连接到字符串结尾
string str1;
str1 = "我";
str1 += "想去草原";
cout << "str1 = " << str1 << endl;
str1 += '!';
cout << "str1 = " << str1 << endl;
string str2 = "吃烤全羊";
str1 += str2;
cout << "str1 = " << str1 << endl;
string str3 = "i";
str3.append(" love ");
cout << "str3 = " << str3 << endl;
str3.append("fishc.too",4);
cout << "str3 = " << str3 << endl;
str3.append(str2);
cout << "str3 = " << str3 << endl;
string str4 = "hhhhh";
str3.append(str4,0,5);
cout << "str3 = " << str3 << endl;
}
void test4()
{
//int find(const string& str,int pos = 0)const;
string str1 = "Wishing you chear skies and wide eyes.";
int pos = str1.find("you");
if (pos == -1)
{
cout << "未找到字符串!!!" << endl;
}
else
{
cout << "找到字符串,pos = " << pos << endl;
}
//rfind 和 find区别
//rfind 从右向左查找 find 从左向右查找
pos = str1.rfind("wide");
cout << "pos = " << pos << endl;
//从 1号位置起,7个字符替换为1111
str1.replace(1,7,"1111");
cout << "str1 = " << str1 << endl;
}
void test5()
{
string str1 = "fishc";
string str2 = "fisha";
//int compare(const string &s) const;与字符串s比较
//int compare(const string *s) const;与字符串s比较
if (str1.compare(str2) == 0)
{
cout << "str1 = str2" << endl;
}
else if (str1.compare(str2) > 0)
{
cout << "str1 > str2" << endl;
}
else
{
cout << "str1 < str2" << endl;
}
}
void test6()
{
string str1 = "hello";
//char& operator[](int n); 通过[]方式取字符
//通过[]访问单个字符
for (int i = 0;i < str1.size();i++)
{
cout << str1[i] << " ";
}
cout << endl;
//char& at(int n); 通过at方式获取字符
//通过at方式访问单个字符
for (int i = 0; i < str1.size(); i++)
{
cout << str1.at(i) << " ";
}
cout << endl;
//修改单个字符
str1[0] = 'x';
cout << "str1 = " << str1 << endl;
str1.at(1) = 'y';
cout << "str1 = " << str1 << endl;
}
void test7()
{
string str1 = "hello";
//插入
//string& insert(int pos,const char* s); 插入字符串
//string& insert(int pos,const string& str); 插入字符串
str1.insert(1,"6666");
cout << "str1 = " << str1 << endl;
//删除
//string& insert(int pos,int n,char c); 在指定位置插入n个字符c
//string& insert(int pos,int n = npos); 删除从pos开始的n个字符
str1.erase(1,3);
cout << "str1 = " << str1 << endl;
}
void test8()
{
string str1 = "fishc";
//从字符串中获取想要的子串
//string substr(int pos = 0,int n = npos)const; 返回由pos开始的n个字符组成的字符串
string sub1 = str1.substr(1,3);
cout << "sub1 = " << sub1 << endl;
}
int main()
{
test1();
test2();//string 赋值
test3();//string 拼接
test4();//string 查找和替换
test5();//string 比较
test6();//string 存取
test7();//string 插入和删除
test8();//string 求子串
return 0;
}
是谁是谁
|