C++ string头文件

以下函数的定义取自http://www.cplusplus.com 中string头文件的定义

函数
  • stod(const string& str, size_t* idx = 0);
  • stof (const string& str, size_t* idx = 0);
  • stoi (const string& str, size_t* idx = 0, int base = 10);
  • stol (const string& str, size_t* idx = 0, int base = 10);
  • stold (const string& str, size_t* idx = 0);
  • stoll (const string& str, size_t* idx = 0, int base = 10);
  • stoul (const string& str, size_t* idx = 0, int base = 10);
  • stoull (const string& str, size_t* idx = 0, int base = 10);
    解析str将其内容转为浮点数,该浮点数作为double/float/int/long int/long double/long long/unsigned long/unsigned long long类型的值返回。
    如果idx不是空指针,这个函数还会将idx的值设置为str中数字后(例如有空格的情况)第一个字符的位置。
    base指代str中字符串数字的进制。

-to_string(val)
返回val对应的string

成员函数

append
在当前值的末尾附加其他字符来拓展。

  • string& append(const basic_string& str);
  • string& append (const basic_string& str, size_type subpos, size_type sublen); // 从str的subpos位置开始sublen个长度
  • string& append (const charT* s);
  • string& append (const charT* s, size_type n); //c字符串的前n个字符加入末尾
  • string& append (size_type n, charT c); //追加n个字符c在末尾
  • string& append (InputIterator first, InputIterator last); //从[first,last)的迭代器范围加入末尾
  • string& append (initializer_list< charT> il);

assign
为字符串分配一个新值,替换其当前内容。

  • string& assign(const string& str); //复制str
  • string& assign (const string& str, size_t subpos, size_t sublen); //复制从字符位置subpos开始长度为sublen字符的str的部分(或直到str的结尾,如果str太短或sublen是string :: npos)
  • string& assign (const char* s); //复制由s指向的以空终止的字符序列(C字符串)
  • string& assign (const char* s, size_t n); //从s指向的字符数组中复制前n个字符。
  • string& assign (size_t n, char c); //将当前值替换为字符c的n个连续副本。
  • string& assign (InputIterator first, InputIterator last); //相同顺序复制[first,last)范围内的字符序列
  • string& assign (initializer_list il); //以相同顺序复制il中的每个字符。
  • string& assign (string&& str) noexcept; //获取str的内容。str处于未指定但有效的状态。

at
返回对字符串中位置pos的字符的引用。
该函数自动检查pos是否是字符串中字符的有效位置(即pos是否小于字符串长度),如果不是,则抛出out_of_range异常。

  • at (size_t pos);
  • at (size_t pos) const;

back
返回对字符串最后一个字符的引用。
不应在空字符串上调用此函数。

  • char& back();
  • const char& back() const;

capacity
分配存储的返回大小。
此容量不一定等于字符串长度。 它可以相等或更大,当将新字符添加到字符串时,多余的空间可使对象优化其操作。
可以通过调用成员函数reserve来显式更改字符串的容量。

  • size_t capacity() const noexcept;

clear
擦除字符串的内容,该内容将成为一个空字符串(长度为0个字符)。

  • void clear() noexcept;

compare
将字符串对象(或子字符串)的值与其参数指定的字符序列进行比较。
返回值:
0:相等
>0:比较字符串中不匹配的第一个字符的值较大,或者所有比较字符都匹配,但比较字符串较长。
<0:比较字符串中不匹配的第一个字符的值较低,或者所有比较字符都匹配,但比较字符串较短。

  • int compare (const string& str) const noexcept;
  • int compare (size_t pos, size_t len, const string& str) const;
  • int compare (size_t pos, size_t len, const string& str,
    size_t subpos, size_t sublen) const;
  • int compare (const char* s) const;
  • int compare (size_t pos, size_t len, const char* s) const;
  • int compare (size_t pos, size_t len, const char* s, size_t n) const;

copy
复制字符串中的字符序列
将字符串对象当前值的子字符串复制到s指向的数组中。 该子字符串包含从位置pos开始的len个字符。
该函数不会在复制内容的末尾附加空字符。

  • size_t copy (char* s, size_t len, size_t pos = 0) const;
    返回复制到s指向的数组的字符数。

c_str
获取等效的C字符串
返回指向一个数组的指针,该数组包含一个以空值终止的字符序列(即C字符串),代表字符串对象的当前值。
该数组包括组成字符串对象值的相同字符序列,最后还有一个额外的终止空字符(’\ 0’)。

  • const char* c_str() const noexcept;

data
返回指向一个数组的指针,该数组包含一个以空值终止的字符序列(即C字符串),代表字符串对象的当前值。
string :: data和string :: c_str都是同义词,并且返回相同的值。

  • const char* data() const noexcept;

empty
返回字符串是否为空(即其长度是否为0)。
此函数不会以任何方式修改字符串的值。

  • bool empty() const noexcept;

erase
删除字符串中的字符

  • string& erase (size_t pos = 0, size_t len = npos); //删除字符串值中从字符位置pos开始并跨越len个字符的部分(如果内容太短或len是string :: npos则一直到字符串的结尾)
  • iterator erase (const_iterator p); //删除p指向的字符。
  • iterator erase (const_iterator first, const_iterator last); //擦除[first,last)范围内的字符序列。

find
查找字符串内容
在字符串中搜索其参数指定的首次出现的序列。
如果指定了pos,则搜索仅在位置pos或之后的字符,而忽略包括pos之前的所有可能出现的字符。

  • size_t find (const string& str, size_t pos = 0) const noexcept;
  • size_t find (const char* s, size_t pos = 0) const;
  • size_t find (const char* s, size_t pos, size_type n) const; // n:要匹配的序列是数组中的前n个字符。
  • size_t find (char c, size_t pos = 0) const noexcept; //c:要搜索的单个字符。

返回值:
第一个匹配项的第一个字符的位置。
如果未找到匹配项,则该函数返回string :: npos。

find_first_not_of
在字符串中搜索与参数中指定的任何字符都不匹配的第一个字符。
如果指定了pos,则搜索仅包括位置pos或之后的字符,而忽略该字符之前的所有可能出现的情况。

  • size_t find_first_not_of (const string& str, size_t pos = 0) const noexcept;
  • size_t find_first_not_of (const char* s, size_t pos = 0) const;
  • size_t find_first_not_of (const char* s, size_t pos, size_t n) const;
  • size_t find_first_not_of (char c, size_t pos = 0) const noexcept;
    返回值:
    不匹配的第一个字符的位置。
    如果找不到这样的字符,该函数将返回string :: npos。

find_first_of
在字符串中搜索与参数中指定的任何字符匹配的第一个字符。

  • size_t find_first_of (const string& str, size_t pos = 0) const noexcept;
  • size_t find_first_of (const char* s, size_t pos = 0) const;
  • size_t find_first_of (const char* s, size_t pos, size_t n) const;
  • size_t find_first_of (char c, size_t pos = 0) const noexcept;
    返回值:
    匹配的第一个字符的位置。

find_last_not_of
在字符串中搜索与参数中指定的任何字符都不匹配的最后一个字符。

  • size_t find_last_not_of (const string& str, size_t pos = npos) const noexcept;
  • size_t find_last_not_of (const char* s, size_t pos = npos) const;
  • size_t find_last_not_of (const char* s, size_t pos, size_t n) const;
  • size_t find_last_not_of (char c, size_t pos = npos) const noexcept;

find_last_of
在字符串中搜索与参数中指定的任何字符匹配的最后一个字符。

  • size_t find_last_of (const string& str, size_t pos = npos) const noexcept;
  • size_t find_last_of (const char* s, size_t pos = npos) const;
  • size_t find_last_of (const char* s, size_t pos, size_t n) const;
  • size_t find_last_of (char c, size_t pos = npos) const noexcept;

front
访问第一个字符
返回对字符串第一个字符的引用。
不应在空字符串上调用此函数。

  • char& front();
  • const char& front() const;

get_allocator
获取分配器
返回与该字符串关联的分配器对象的副本。
字符串使用默认的allocator 类型,该类型没有状态

insert
插入字符串
在pos(或p)指示的字符之前,在字符串中插入其他字符:

  • string& insert (size_t pos, const string& str); //插入str的副本
  • string& insert (size_t pos, const string& str, size_t subpos, size_t sublen); //插入str子字符串的副本
  • string& insert (size_t pos, const char* s); //插入由s指向的以空终止的字符序列(C字符串)形成的字符串的副本。
  • string& insert (size_t pos, const char* s, size_t n); //在s指向的字符数组中插入前n个字符的副本。
  • string& insert (size_t pos, size_t n, char c); //插入字符c的n个连续副本。
    iterator insert (const_iterator p, size_t n, char c);
  • iterator insert (const_iterator p, char c); //插入字符c。
  • template
    iterator insert (iterator p, InputIterator first, InputIterator last);
  • string& insert (const_iterator p, initializer_list il);

max_size
返回字符串可以达到的最大长度。

  • size_t max_size() const noexcept;

pop_back
删除最后一个字符
擦除字符串的最后一个字符,有效地将其长度减少一个。

  • void pop_back();

replace
替换部分字符串
用新内容替换以字符pos开头并跨越len个字符的字符串部分(或[i2,i2)范围内的字符串部分)

  • string& replace (size_t pos, size_t len, const string& str);
    string& replace (const_iterator i1, const_iterator i2, const string& str); //复制str
  • string& replace (size_t pos, size_t len, const string& str,
    size_t subpos, size_t sublen); //复制从字符位置subpos开始并跨越sublen字符的str的部分
  • string& replace (size_t pos, size_t len, const char* s);
    string& replace (const_iterator i1, const_iterator i2, const char* s); //复制由s指向的以空终止的字符序列(C字符串)。
  • string& replace (size_t pos, size_t len, const char* s, size_t n);
    string& replace (const_iterator i1, const_iterator i2, const char* s, size_t n); //从s指向的字符数组中复制前n个字符。
  • string& replace (size_t pos, size_t len, size_t n, char c);
    string& replace (const_iterator i1, const_iterator i2, size_t n, char c); //用字符c的n个连续副本替换字符串的一部分。
  • template
    string& replace (const_iterator i1, const_iterator i2,
    InputIterator first, InputIterator last);
  • string& replace (const_iterator i1, const_iterator i2, initializer_list il);

reserve
要求更改容量
请求根据计划的大小更改将字符串容量调整为最多n个字符的长度。

  • void reserve (size_t n = 0);

resize
调整字符串大小
将字符串的大小调整为n个字符的长度。
如果n小于当前字符串的长度,则当前值将缩短为它的前n个字符,并删除第n个字符之外的字符。
如果n大于当前字符串长度,则通过在结尾处插入任意数量的字符来扩展当前内容,以达到n的大小。 如果指定了c,则新元素将初始化为c的副本,否则,它们是值初始化的字符(空字符)。

  • void resize (size_t n);
  • resize (size_t n, char c);

substr
产生子字串
返回一个新构造的字符串对象,其值初始化为该对象的子字符串的副本。

子字符串是对象的一部分,从字符位置pos开始并跨越len个字符(或直到字符串的结尾,以先到者为准)。

  • string substr (size_t pos = 0, size_t len = npos) const;

swap
交换字符串值
用另一个字符串对象str的内容交换容器的内容。 长度可能会有所不同。

  • void swap (string& str);
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值