新增数字和string的变换函数
string–>整型
stoi, stol, stoul, stoll, stoull
例如: int stoi(const std::string& str, std::size_t* pos = 0, int base = 10);
实现的效果与strtol, strtoll, strtoul, strtoull相同。
如果base为0,由函数自行选择进制:如果前缀是0,使用八进制;如果前缀是0x或0X,使用十六进制;其他情况使用十进制。
string–>浮点型
stof, stod, stold
例如: float stof(const std::string& str, std::size_t* pos = 0);
实现的效果与strtof, strtod相同。
还支持二进制浮点表达式,例如“-0xa.cp-2”
表示的是16进制下的-0xa.c
再乘上2的-2次方,-(10+12/16)*2^(-2)=-2.6875,其中0x或0X是必需的,其他部分可以省略。
以上函数,如果pos不为空,将存储第一个未转换字符的位置信息;如果无法转换,抛出std::invalid_argument;如果溢出抛出std::out_of_range。
数字–>string
to_string函数有多个重载版本,参数分别支持int, long, long long, unsigned, unsigned long, unsigned long long, float, double, long double向string的转化。
例如: std::string to_string(int value);
to_string实现的效果和sprintf相同。
basic_string主要的新增部分
template <class CharT,
class Traits = std::char_traits<CharT>,
class Allocator = std::allocator<CharT>>
class basic_string;
移动构造,移动赋值,assign增加右值引用版本:
basic_string(basic_string&& other) noexcept;
basic_string& operator=(basic_string&& str);
basic_string& assign(basic_string&& str);
构造,赋值,assign,insert,append,+=,replace增加初始化列表版本:
basic_string(std::initializer_list<CharT> ilist, const Allocator& alloc = Allocator());
basic_string& operator=(std::initializer_list<CharT> ilist);
basic_string& assign(std::initializer_list<CharT> ilist);
basic_string& operator+=(std::initializer_list<CharT> ilist);
basic_string& replace(const_iterator first, const_iterator last, std::initializer_list<CharT> ilist);
新增front,back,pop_back:
如果string为空,函数行为未定义
CharT& front();
const CharT& front() const;
CharT& back();
const CharT& back() const;
void pop_back();
cbegin,cend,crbegin,crend:
相比begin,end增加显式返回const迭代器的版本
const_iterator cbegin() const noexcept;
const_iterator cend() const noexcept;
const_reverse_iterator crbegin() const noexcept;
const_reverse_iterator crend() const noexcept;
shrink_to_fit:
请求移除无用空间,如果发生了内存重分配,所有指针、引用、迭代器都变为无效
void shrink_to_fit();
hash对string进行了特化:
template<> struct hash<std::string>;