在线文档教程
C++
字符串 | Strings

std::basic_string

性病:基本[医]弦

Defined in header
template< class CharT, class Traits = std::char_traits<CharT>, class Allocator = std::allocator<CharT> > class basic_string;(1)
namespace pmr { template <class CharT, class Traits = std::char_traits<CharT>> using basic_string = std::basic_string< CharT, Traits, std::polymorphic_allocator<CharT>> }(2)(since C++17)

类模板basic_string存储和操作char-像物体。类既不依赖于字符类型,也不依赖于对该类型的操作的性质。操作的定义通过Traits模板参数-一个专门化的std::char_traits或者是一个兼容的特征类。Traits::char_typeCharT必须命名相同类型;否则行为未定义。

The elements of a basic_string are stored contiguously, that is, for a basic_string s, &*(s.begin() + n) == &*s.begin() + n for any n in [0, s.size()), or, equivalently, a pointer to s0 can be passed to functions that expect a pointer to the first element of a CharT[] array.(since C++11)

std::basic_string满足…的要求AllocatorAwareContainer,,,SequenceContainerContiguousContainer%28自C++17%29。

提供了几种常见字符类型的类型:

在标头中定义<string>

*。

类型定义

STD::String STD::basic[医]弦<char>

std::wstring std::basic[医]字符串<wchar[医]T>

STD::u16string%28C++11%29 std::basic[医]字符串<char16[医]T>

STD::u32string%28C++11%29 std::basic[医]字符串<char32[医]T>

std::pmr::string%28C++17%29 std::pmr::basic[医]弦<char>

std::pmr::wstring%28C++17%29 std::pmr::basic[医]字符串<wchar[医]T>

std::pmr::u16string%28C++17%29 std::pmr::basic[医]字符串<char16[医]T>

std::pmr::u32string%28C++17%29 std::pmr::basic[医]字符串<char32[医]T>

模板参数

CharT-character type
Traits-traits class specifying the operations on the character type
Allocator-Allocator type used to allocate internal storage

成员类型

Member typeDefinition
traits_typeTraits
value_typeCharT
allocator_typeAllocator
size_typeAllocator::size_type (until C++11) std::allocator_traits<Allocator>::size_type (since C++11)
Allocator::size_type(until C++11)
std::allocator_traits<Allocator>::size_type(since C++11)
difference_typeAllocator::difference_type (until C++11) std::allocator_traits<Allocator>::difference_type (since C++11)
Allocator::difference_type(until C++11)
std::allocator_traits<Allocator>::difference_type(since C++11)
referenceAllocator::reference (until C++11) value_type& (since C++11)
Allocator::reference(until C++11)
value_type&(since C++11)
const_referenceAllocator::const_reference (until C++11) const value_type& (since C++11)
Allocator::const_reference(until C++11)
const value_type&(since C++11)
pointerAllocator::pointer (until C++11) std::allocator_traits<Allocator>::pointer (since C++11)
Allocator::pointer(until C++11)
std::allocator_traits<Allocator>::pointer(since C++11)
const_pointerAllocator::const_pointer (until C++11) std::allocator_traits<Allocator>::const_pointer (since C++11)
Allocator::const_pointer(until C++11)
std::allocator_traits<Allocator>::const_pointer(since C++11)
iteratorRandomAccessIterator
const_iteratorConstant random access iterator
reverse_iteratorstd::reverse_iterator<iterator>
const_reverse_iteratorstd::reverse_iterator<const_iterator>

成员函数

(constructor)constructs a basic_string (public member function)
(destructor)destroys the string, deallocating internal storage if used (public member function)
operator=assigns values to the string (public member function)
assignassign characters to a string (public member function)
get_allocatorreturns the associated allocator (public member function)

元素存取

访问指定字符时,检查%28公共成员函数%29

操作者。[访问指定字符%28公共成员函数%29

前端%28C++11%29访问第一个字符%28公共成员函数%29

Back%28C++11%29访问最后一个字符%28公共成员函数%29

数据返回指向字符串%28公共成员函数%29的第一个字符的指针。

丙[医]Str返回字符串%28公共成员函数%29的不可修改的标准C字符数组版本。

运算符BASIC[医]弦[医]视图%28C++17%29返回不可修改的字符串。[医]查看整个字符串%28公共成员函数%29

迭代器

初学者%28C++11%29返回一个迭代器到开头%28公共成员函数%29

End Cend%28C++11%29将迭代器返回到末尾%28公共成员函数%29

rback crback%28C++11%29将反向迭代器返回到起始%28公共成员函数%29

Rendcrend%28C++11%29将反向迭代器返回到结束%28公共成员函数%29

容量

空检查字符串是否为空%28公共成员函数%29

Szelength返回字符数%28公共成员函数%29

马克斯[医]Size返回最大字符数%28公共成员函数%29

储备储备储备%28公众会员功能%29

容量返回当前分配的存储空间%28公共成员函数%29中可以保存的字符数。

收缩[医]到[医]FIT%28C++11%29通过释放未使用内存%28公共成员函数%29来减少内存使用

操作

清除内容%28公共成员功能%29

插入字符%28公共成员函数%29

删除字符%28公共成员函数%29

推[医]向末尾%28公共成员函数%29追加一个字符

波普[医]后退%28C++11%29移除最后一个字符%28公共成员函数%29

将字符追加到末尾%28公共成员函数%29

运算符+=将字符追加到末尾%28公共成员函数%29

比较两个字符串%28公共成员函数%29

替换字符串%28公共成员函数%29的指定部分

子字符串%28公共成员函数%29

复制字符%28公共成员函数%29

调整大小更改存储的字符数%28公共成员函数%29

交换交换内容%28公共成员函数%29

搜索

在字符串%28公共成员函数%29中查找字符

查找子字符串%28公共成员函数%29的最后一次出现

找出[医]第一[医]第一次出现字符%28公共成员函数%29

找出[医]第一[医]不[医]查找第一次缺少字符%28公共成员函数%29

找出[医]最后[医]查找最后出现的字符%28公共成员函数%29

找出[医]最后[医]不[医]%28公共成员函数%29

常数

非营利组织静态特殊价值。确切意义取决于上下文%28公共静态成员常量%29

非会员职能

operator+concatenates two strings or a string and a char (function template)
operator==operator!=operator<operator>operator<=operator>=lexicographically compares two strings (function template)
std::swap(std::basic_string)specializes the std::swap algorithm (function template)

输入/输出

运算符<<operator>>对字符串%28函数模板%29执行流输入和输出

将I/O流中的数据读取到字符串%28函数模板%29中

数值转换

斯托斯特洛斯托尔%28C++11%29%28C++11%29%28C+11%29将字符串转换为有符号整数%28函数%29

stoulstoull%28C++11%29%28C++11%29将字符串转换为无符号整数%28函数%29

stofstodstold%28C++11%29%28C++11%29%28C+11%29将字符串转换为浮点值%28函数%29

到[医]字符串%28C++11%29将整数或浮点值转换为字符串%28函数%29

到[医]wstring%28C++11%29将整数或浮点值转换为wstring%28函数%29。

文字

定义在内联命名空间std::alals::string中[医]文字

*。

运算符“s%28C++14%29将字符数组文字转换为Basic[医]字符串%28功能%29

帮助者类

std::hashstd::hashstd::hashstd::hash (C++11)(C++11)(C++11)(C++11)hash support for strings (class template specialization)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/string/basic[医]弦