文章目录
string类的简单概念理解
1,首先,标准库给我们提供了提供string类,使用的时候需要使用标准命名空间
using namespace std; 另外,它帮我们封装好了成员函数,和成员变量。
这样外面在解决问题的时候,我们可以直接调用早就写好的成员函数了。
class string
{
//构造函数
//析构函数
//........各种功能的函数 ,大佬已经帮我们写好了
private:
char* _str;
size_t capacity;
size_t size;
};
C++文档网站
我们需要去网站里面去查看文档去学习
这个比较好,不混乱 ,早期用这个,推荐这个 : https://cplusplus.com/
这个是官方网站,比较混乱,但是更新的比较快,适合查看新语法:https://en.cppreference.com/w/
点开之后直接搜索string即可
先看看构造函数,析构函数,赋值重载
析构函数没什么,就是释放空间,对象的生命周期结束后自动调用。
#include <iostream>
using namespace std;
int main()
{
string s1(); //无参构造函数,这是个匿名对象,生命周期只在这一行
string s2("hello world"); //传字符串的构造函数
string s3(s2); //传同类型的构造函数
cout << s3 << endl;
string s4("haha");
s4 = s3; //同类型的赋值重载
cout << s4 << endl;
s4 = "24556"; //const char* 的赋值重载
cout << s4 << endl;
s4 = 'a'; //单个字符的赋值重载
cout << s4 << endl;
return 0;
}
看看迭代器iterator,如何去遍历
不用想的太复杂,就是类似于指针的东西,我们现在会用就可以了
int main()
{
string s1("hello world");
string::iterator it = s1.begin(); //这里的作用域操作符,指定的是string的迭代器
while (it != s1.end())
{
cout << *it << " ";
it++;
}
string::reverse_iterator rit = s1.rbegin(); //倒着遍历
while (rit != s1.rend())
{
cout << *rit << " ";
it++;
}
return 0;
}
capacity 关于容量的函数
int main()
{
string s1("hello world");
cout << s1.size() << endl; //输出的有效字符的个数,\0是标识字符
cout << s1.capacity() << endl; //返回的是空间大小,编译器自己存在扩容机制
//关于resize 和 reserve (重要)
s1.resize(15); //这里会自动填充‘\0’
s1.reserve(10); //什么事情都不做
s1.reserve(50);
cout << s1.capacity() << endl; //输出不是50,
return 0;
}
如何访问字符呢?
各种插入删除操作
int main()
{
string s1("hello world ");
string s2 ("!!!!!!!!!!!!!!!!!");
s1 += s2; //这里的作用是把s2追加到s1
cout << s1 << endl;
s1 += "#######"; //这里是追加字符串
cout << s1 << endl;
string s3("i am a handsome boy");
string s4("you are a good girl");
//s3.append(s4); //类似于+= ,s4追加到s3 的后面
//cout << s3 << endl;
//s3.append(s4,5); //从第5个位置开始追加
//cout << s3 << endl;
s3.append(s4, 5,2); //从第5个位置开始追加,只追加2个
cout << s3 << endl;
//s3.append("!!!!!!!!!!!!"); //追加字符串
//cout << s3 << endl;
s3.append("!!!!!!!!!!!!",3); //追加3个字符串
cout << s3 << endl;
return 0;
}
一些零零散散比较重要的
string的习题
int main()
{
string a="hello world";
string b=a;
if (a.c_str()==b.c_str())
{
cout<<"true"<<endl;
}
else cout<<"false"<<endl;
string c=b;
c="";
if (a.c_str()==b.c_str())
{
cout<<"true"<<endl;
}
else cout<<"false"<<endl;
a="";
if (a.c_str()==b.c_str())
{
cout<<"true"<<endl;
}
else cout<<"false"<<endl;
return 0;
}
解析:string的拷贝构造和赋值重载都是深拷贝,c_str()返回的是指向的空间,那肯定都是不相等的
求下面的输出结果
int main()
{
string str(“Hello Bit.”);
str.reserve(111);
str.resize(5);
str.reserve(50);
cout<<str.size()<<“:”<<str.capacity()<<endl;
return 0;
}
解析:resever调整空间大小,比原空间小,不做任何事情, 所以 5,111
看下面结果的输出,
int main()
{
string strText = "How are you?";
string strSeparator = " ";
string strResult;
int size_pos = 0;
int size_prev_pos = 0;
while((size_pos=strText.find_first_of(strSeparator, size_pos)) != string::npos) //从size_pos位置查找空格’ ‘字符返回下标
{
strResult = strText.substr(size_prev_pos, size_pos-size_prev_pos); //然后拿出strText从prepos的位置向后n个的字符
cout<<strResult<<" ";
size_prev_pos = ++size_pos;
}
if(size_prev_pos != strText.size())
{
strResult = strText.substr(size_prev_pos, size_pos-size_prev_pos);
cout<<strResult<<" ";
}
cout<<endl;
return 0;
}
解析:请看注释,结果是 How are you?