string的成员用法

本文详细介绍了C++中的字符串类型如string和wstring,及其常用操作,包括字符转换函数和字符判断函数。同时,文章涵盖了内存操作的相关函数,如memset、memcpy、memmove等,以及文件操作的基本概念和函数,如文件打开、文件大小改变等。
摘要由CSDN通过智能技术生成

string和wstring

容器


allocate           //分配器
iterator           //访回string的迭代器
const_iterator     //访回string的常量迭代器
pointer            //指向T(char)的指针
const_pointer      //指向T(char)的常量指针
const_reference    //T(char)的常量引用
reference          //T(char)的引用
const_reverse_iterator     //访回string的方向常量迭代器
reverse_iterator   //访回string的方向迭代器
difference_type    //int类型
size_type     unsigned int类型
traits_type
value_type         //对象类型T(char)

字符串操作

const char &operator[](int n)const;
const char &at(int n)const;    //返回当前字符串中第n个字符的位置,越界会抛出out_of_range异常
const char *data()const;       //返回一个非null终止的c字符数组,其大小为size()的返回值,
const char *c_str()const;      //返回以null终止的c风格字符串
      int  copy(char *s, int n, int pos = 0) const;  //把当前串中以pos开始的n个字符拷贝到s字符数组中,返回实际拷贝的数目
string&    assign(const char *s); //用新字符串填充,等同operator=
string&    append(const char *s); //把s连接到当前字符串结尾
           clear()             //清空字符串
string&    replace(size_type p,size_type n,const char *s);//删除从p开始的n个字符,然后在p处插入s
string&    insert(size_type p,const char *s);      //在位置p插入s
iterator   erase(iterator first, iterator last);   //删除[first,last)之间的所有字符,
substr(pos) const; //返回pos开始的n个字符组成的字符串
void       swap(string& str);                              //交换当前字符串与str的值


特性

size_type capacity() const;      //返回容量即不必增加内存即可存放的元素个数
void      reserve(size_type n);  //强制修改容量为n
size_type max_size() const;      //返回string对象中可存放的最大字符串长度,不是capacity()的大小
size_type size() const;          //返回当前字符串的大小
size_type length() const;        //返回当前字符串的长度,跟size()相识。
void resize(size_type n, char c = char()); //把字符串当前大小置为n,并用字符c填充不足的部分

bool empty() const; //当前字符串是否为空与size()效率相同



字符串比较

int    compare(const string& str) const;               //比较字符串大小。小于str返回-1;相等返回0;

查找

size_type find(char c,size_type pos = 0) const;  //从pos开始查找c的位置。失败返回string::npos
size_type find_first_of(char c,size_type pos = 0) const; //从pos开始查找字符c第一次出现的位置。失败返回string::npos。
size_type find_first_not_of(char c,size_type pos = 0)const;//从pos开始查找c不在当前字符串第一次出现的位置。失败返回string::npos。

size_type rfind(char c,size_type pos = npos) const;//从当前字符串pos开始从后向前查找。默
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值