1. String类
- string是表示字符串的字符串类
- 该类的接口与常规容器的接口基本相同,再添加一些专门用来操作string的常规操作。
- String在底层实际是:basic_string模版类的别名,typedef basic_string<char,char_traits,allocator>string;
- 不能操作多字节或者变长字符的序列。
在使用string类的时候,必须包含头文件以及using namespace std;
2. String类的常见接口说明
(1) string类对象的常见构造
string();//构造空字符串
string(const char* s);//用C-string来构造string类对象
string(size_t n, char c); //string类对象中包含n个字符的c
string(const string&s); //拷贝构造函数
string(const string&s, size_t n);//用s中前n个字符构造新的string类对象
(2) string类对象的容量操作
size_t size() const;// 返回字符串有效字符长度
size_t length() const;// 返回字符串有效字符长度
size_t capacity() const;// 返回空间总大小
bool empty() const;// 检测字符串释放为空串,是返回true,否则返回false
void clear();// 清空有效字符
void resize(size_t n, char c);// 将有效字符的个数该成n个,多出的空间用字符c填充
void resize(size_t n);// 将有效字符的个数改成n个,多出的空间用0填充
void reserve(size_t res_arg = 0);// 为字符串预留空间
eg1:
length/size/capacity的区别
注意:string类对象支持直接用cin和cout进行输入和输出。
string s("hello!");
cout << s.length() << endl;//6
cout << s.size() << endl;//6
cout << s.capacity() << endl;//15
注意:size()与length()方法底层实现原理相同,引用size()的原因是为了于其他容器的接口保持一致,一般情况下基本都用size().
eg 2:
s.clear();
cout << s.size() << endl;//0
cout << s.capacity() << endl;//15
注意:s.clear();函数 将s中的有效字符清空,只将size清0,不改变底层空间的大小。
eg 3:
//将s中有效字符的个数增加到10个,多出位置用'a'进行填充
s.resize(10, 'a');
cout << s.size() << endl;//10
cout << s.capacity() << endl;//15
cout << s << endl;//"hello!aaaa"
//将s中有效字符的个数增加到15个,多出的位置用缺省值'\0'进行填充
s.resize(15);
cout << s.size() << endl;//15
cout << s.capacity() << endl;//15
cout << s << endl;//"hello"
//resize将元素个数减少,底层空间总大小不变
s.resize(5);
cout << s.size() << endl;//5
cout << s.capacity() << endl;//15
cout << s << endl;
//resize将元素个数增多,改变了底层容量的大小
s.resize(100);
cout << s.size() << endl;//100
cout << s.capacity() << endl;//111
// reserve(size_t res_arg=0):为string预留空间,不改变有效元素个数,当reserve的参数小于 string的底层空间总大小时,reserver不会改变容量大小。
s.resize(50);
cout << s.size() << endl;//50
cout << s.capacity() << endl;//111
(3) string类对象的访问操作
返回pos位置的字符,const string类对象调用。
char& operator[](size_t pos)
{
return _str[pos];
}
返回pos位置的字符,非const string类对象调用。
const char& operator[](size_t pos)const
3. string类对象的修改操作
void push_back(char c) //在字符串后尾插字符c
string& append(const char* s); //在字符串后追加一个字符串
string& operator+=(const string& str)//在字符串后追加字符串str
string& operator+=(const char* s) //在字符串后追加C个数字符串
string& operator+=(char c) //在字符串后追加字符c
const char* c_str()const //返回C格式字符串
//从字符串pos位置开始往后找字符c,返回该字符在 字符串中的位置
size_t find(char c, size_t pos = 0)const
//从字符串pos位置开始往前找字符c,返回该字符在 字符串中的位置
size_t rfind(char c, size_t pos = npos)
//在str中从pos位置开始,截取n个字符,然后将其返回
string substr(size_t pos = 0, size_t n = npos)const
eg1:
string str;
str.push_back(' ');
// 在str后插入空格
str.append("hello");
// 在str后追加一个字符"hello"
str += 'w';
// 在str后追加一个字符'w'
str += "orld";
// 在str后追加一个字符串"orld"
cout<<str<<endl;
cout<<str.c_str()<<endl;
// 以C语言的方式打印字符串
// 获取file的后缀
string file1("string.cpp");
size_t pos = file.rfind('.');
string suffix(file.substr(pos, file.size()-pos));
cout << suffix << endl;
// npos是string里面的一个静态成员变量
// static const size_t npos = -1;
// 取出url中的域名
sring url("http://www.cplusplus.com/reference/string/string/find/");
cout << url << endl;
size_t start = url.find("://");
if (start == string::npos)
{
cout << "invalid url" << endl;
return;
}
start += 3;
size_t finish = url.find('/', start);
string address = url.substr(start, finish - start);
cout << address << endl;
// 删除url的协议前缀
pos = url.find("://");
url.erase(0, pos+3);
cout<<url<<endl;
eg2:
// 利用reserve提高插入数据的效率,避免增容带来的开销
void TestPushBack()
{ string s;
size_t sz = s.capacity();
cout << "making s grow:\n";
for (int i = 0; i < 100; ++i)
{
s += 'c';
if (sz != s.capacity())
{
sz = s.capacity();
cout << "capacity changed: " << sz << '\n';
}
}
}
void TestPushBack_P()
{
string s;
s.reserve(100);
size_t sz = s.capacity();
cout << "making s grow:\n";
for (int i = 0; i < 100; ++i)
{
s += 'c';
if (sz != s.capacity())
{
sz = s.capacity();
cout << "capacity changed: " << sz << '\n';
}
}
}
***注意:
- 在string尾部追加字符时,s.push_back© / s.append(1, c) / s += 'c’三种的实现方式差不多,一般 情况下string类的+=操作用的比较多,+=操作不仅可以连接单个字符,还可以连接字符串。
- 对string操作时,如果能够大概预估到放多少字符,可以先通过reserve把空间预留好。***
4.string类非成员函数
operator+ 尽量少用,因为效率低
operator>> 输入运算符重载
operator<< 输出运算符重载
getline 获取一行字符串
relational operators 大小比较
PS:在C++中,一个程序要运行起来,需要经历以下几个阶段:预处理,编译,汇编,链接。