1.标准C++ string类
1)重载操作符
- += 将一个字符串加到另一个字符串后面
- = 将一个字符串赋给另一个字符串
- << 显示string对象
- [ ] 访问字符串中的各字符
2)string类的构造函数
- string( const char * s) 将string对象初始化为s指向的NBTS( null-terninated string)
- string( n,char c) 创建一个包含n个相同元素为c的string对象
- string( const char * s,n) 将string初始化为s指向NBTS的前n个字符
- string( str.bigin,str.end) 将string初始化为区间[begin,end)内的字符
3)string类输入
//情形一
char strName[max_n];
cin>>strName;
cin.getline( strName,size_n);//discard \n
cin.get( strName,size_n); //leave \n in queue
//情形二
string strName;
cin>>strName;
getline(cin,strName);
4)重载的find( )方法strName.find( char c , pos n) const 从strName的pos开始,查找字符c。若找到,则返回字符首次出现的位置,否则返回string: :npos( 字符串可储存的最大字符数)
strName.find( const &s , pos n) const \\or *s 从strName的pos开始,查找字符串s。若找到,则返回字符首次出现的位置,否则返回string: :npos
strName.find( const &s , pos n,m) const 从strName的pos开始,查找字符串s的前m个字符组成的字符串。若找到,则返回字符首次出现的位置,否则返回string: :npos
5)其它相关方法rfind() //查找字符或字符串最后一次出现的位置
find_first_of() //查找参数中任意一个字符首次在字符串中出现的位置
find_last_of() //查找参数中任意一个字符最后一次在字符串中出现的位置
find_first_not_of() //在字符串中查找第一个不被包含在参数中的字符
ind_last_not_of() //在字符串中查找最后一个不被包含在参数中的字符
2.auto_ptr 模板
void sample1()
{
int *p= new int ;
*p =18;
return ;
}
#include <memory.h>
void sample2()
{
auto_ptr<int> p(new int);
*p =18;
return ;
}
两者不同之处在于,当执行 retrun 时,sample1删除了p但没有删除储存 18 动态内存,而sample2两则均删除。所以,auto_ptr 可用来防止内存泄漏。创建 auto_ptr 对象,必须包含头文件memory,然后用模板句法实例化所需类型的指针。