常见容器类
一、string类
·string类型支持长度可变的字符串,C++标准库负责管理与存储字符相关的内存,以及
提供各种有用的操作。
·typedef basic_string<char> string;
·typedef basic_string<wchar_t> wstring;
·要使用string类型对象,必须包含相关头文件
#include <string>
using std :: string;
1.string类的构造函数
·string s1; //默认构造函数,s1为空串
·string s2(s1); //将s2初始化为s1的一个副本
·string s3(“value”); //将s3初始化为一个字符串字面值副本
·string s4(n,’c’); //将s4初始化为字符’c’的n个副本
代码示例:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1;
cout << s1 << endl;
string s2("hello");
string s3(s2);
cout << s3 << endl;
string s4(10, 'a');
cout << s4 << endl;
return 0;
}
运行结果:
2.string类的字符操作
·const char &operator[](int n)const;
·const char &at(int n)const;
·char &operator[](int n);
·char &at(int n);
operator[]和at()均返回当前字符串中第n个字符的位置,但at函数提供范围检查,当越
界时会抛出out_of_range异常,下标运算符[]不提供检查访问
· int copy(char *s, int n, int pos = 0) const;//把当前串中以pos开始的n个字符拷贝到以s为
起始位置的字符数组中,返回实际拷贝的数目
代码示例1:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1;
cout << s1 << endl;
string s2("hello");
cout << s2[2] << endl;
cout << s2.at(2) << endl;
string s3(s2);
cout << s3 << endl;
string s4(10, 'a');
cout << s4 << endl;
return 0;
}
运行结果:
代码示例2:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1("hello");
cout << s1 << endl;
string s2("hello");
cout << s2[2] << endl;
cout << s2.at(2) << endl;
string s3(s2);
cout << s3 << endl;
string s4(10, 'a');
cout << s4 << endl;
char buffer[100] = {0};
s1.copy(buffer,3,1);
cout << buffer << endl;
return 0;
}
运行结果:
3.string类的特性描述
·int capacity()const; //返回当前容量(即string中不必增加内存即可存放的元素个数)
·int max_size()const; //返回string对象中可存放的最大字符串的长度
·int size()const; //返回当前字符串的大小
·int length()const; //返回当前字符串的长度
·bool empty()const; //当前字符串是否为空
·void resize(int len,char c);//把字符串当前大小置为len,并用字符c填充不足的部分
4.string类的输入输出操作
·string类重载运算符operator>>用于输入,同样重载运算符operator<<用于输出操作。
·函数getline(istream &in,string &s);用于从输入流in中读取字符串到s中,以换行符'\n'分开
代码比较:
版本1:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1;
cin >> s1;
cout << s1 << endl;
return 0;
}
版本2:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s2;
getline(cin, s2);
cout << s2 << endl;
return 0;
}
运行结果对比:
5.string类的赋值
·string &operator=(const string &s);//把字符串s赋给当前字符串
·string &assign(const char *s);//用c类型字符串s赋值
·string &assign(const char *s,int n);//用c字符串s开始的n个字符赋值
·string &assign(const string &s);//把字符串s赋给当前字符串
·string &assign(int n,char c);//用n个字符c赋值给当前字符串
·string &assign(const string &s,int start,int n);//把字符串s中从start开始的n个字符赋给当
前字符串
·string &assign(const_iterator first,const_itertor last);//把first和last迭代器之间的部分赋给
字符串
代码示例:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i;
string s1;
s1.assign("hello");
cout << s1 << endl;
for (i = 0; i < s1.length(); i++)
{
cout << s1[i] << endl;
}
return 0;
}
运行结果:
6.string类的迭代器处理
string类提供了向前和向后遍历的迭代器iterator,迭代器提供了访问各个字符的语法,类 似于指针操作,迭代器不检查范围。
用string::iterator或string::const_iterator声明迭代器变量,const_iterator不允许改变迭代的内容。常用迭代器函数有:
·const_iterator begin()const;
·iterator begin(); //返回string的起始位置
·const_iterator end()const;
·iterator end(); //返回string