栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c++ 11标准模板(STL)string (一)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++ 11标准模板(STL)string (一)

std::basic_string        

定义于头文件

template,
     class Allocator = std::allocator
> class basic_string;

类模板 basic_string 存储并操纵作为非数组平凡标准布局类型的仿 char 对象序列。该类既不依赖字符类型,亦不依赖该类型上的原生操作。操作的定义通过 Traits 模板形参—— std::char_traits 的特化或兼容特性类提供。 Traits::char_type 和 CharT 必须指名同一类型;否则程序为病式。

basic_string 是相继存储的,即对于 basic_string s ,对任何 [0, s.size()) 中的 n 有 &*(s.begin() + n) == &*s.begin() + n ,或等价地,指向 s[0] 的指针能传递给期待指向空终止 (C++11 起) CharT[] 数组首元素指针的函数。

std::basic_string 满足具分配器容器 (AllocatorAwareContainer) 、序列容器 (SequenceContainer) 及连续容器 (ContiguousContainer) (C++17 起)的要求

提供数个对于常用字符类型的 typedef :

类型定义
std::stringstd::basic_string
std::wstringstd::basic_string
std::u16string (C++11)std::basic_string
std::u32string (C++11)std::basic_string
模板形参
CharT-字符类型
Traits-指定字符类型上操作的特性类
Allocator-用于分配内部存储的分配器 (Allocator) 类型

 

成员类型定义
traits_typeTraits
value_typeCharT
allocator_typeAllocator
size_type
    Allocator::size_type (C++11 前)     std::allocator_traits::size_type (C++11 起) 
difference_type
    Allocator::difference_type (C++11 前)     std::allocator_traits::difference_type (C++11 起) 
reference
    Allocator::reference (C++11 前)     
    value_type& (C++11 起) 
const_reference
    Allocator::const_reference (C++11 前)     
    const value_type& (C++11 起) 
pointer
    Allocator::pointer (C++11 前)         std::allocator_traits::pointer (C++11 起) 
const_pointer
    Allocator::const_pointer (C++11 前)     std::allocator_traits::const_pointer (C++11 起) 
iterator遗留随机访问迭代器 (LegacyRandomAccessIterator)
const_iterator常随机访问迭代器
reverse_iteratorstd::reverse_iterator
const_reverse_iteratorstd::reverse_iterator
 构造函数
std::basic_string::basic_string

1) 默认构造函数。构造空 string (拥有零大小和未指定的容量)。若不提供分配器,则从默认构造的实例获得分配器。

basic_string();
explicit basic_string( const Allocator& alloc );

2)构造拥有字符 ch 的 count 个副本的 string 。若会推导出不足以作为分配器的 Allocator 类型,则此构造函数不用于类模板实参推导。

basic_string( size_type count, CharT ch, const Allocator& alloc = Allocator() );

3) 以 other 的子串 [pos, pos+count) 构造 string 。若 count == npos 或未指定 count ,或若请求的子串越过字符串的结尾,则产生的子串为 [pos, other.size()) 。 

basic_string( const basic_string& other,
             size_type pos,
             size_type count = std::basic_string::npos,
             const Allocator& alloc = Allocator() );

4) 以 s 所指向的字符串的首 count 个字符构造 string 。 s 能包含空字符。 string 的长度为 count 。若 [s, s + count) 不是合法范围则行为未定义。 

basic_string( const CharT* s,
              size_type count,
              const Allocator& alloc = Allocator() );

5) 以 s 所指向的空终止字符串的副本所初始化的内容构造 string 。以首个空字符确定字符串的长度。若 [s, s + Traits::length(s)) 不是合法范围(例如若 s 是空指针)则行为未定义。若会推导出不足以作为分配器的 Allocator 类型,则此构造函数不用于类模板实参推导。 

basic_string( const CharT* s, const Allocator& alloc = Allocator() );

 6)构造拥有范围 [first, last) 内容的 string 。

若 InputIt 为整数类型,则等价于 basic_string(static_cast(first), static_cast(last), a) 。

(C++11 前)

此构造函数仅若 InputIt 满足 遗留输入迭代器 (LegacyInputIterator) 才参与重载决议。

(C++11 起)
template< class InputIt > 
basic_string( InputIt first, InputIt last,
              const Allocator& alloc = Allocator() );

7) 复制构造函数。构造拥有 other 内容副本的 string 。 

basic_string( const basic_string& other );

 8) 移动构造函数。用移动语义构造拥有 other 内容的 string 。将 other 留在合法但未指定的状态。

basic_string( basic_string&& other ) noexcept;
basic_string( basic_string&& other, const Allocator& alloc );

9) 构造拥有 initializer_list ilist 内容的 string 。 

basic_string( std::initializer_list ilist,
               const Allocator& alloc = Allocator() );

 调用示例:

    string str = "abcdefghijk";
    string str1;
    std::cout << "Default constructor is "
              << (str1.empty() ? "empty" : " not empty") << std::endl;

    //构造拥有字符 ch 的 count 个副本的 string
    string str2(3, 'c');
    std::cout << "str2: " << str2 << std::endl;

    // 以 other 的子串 [pos, pos+count) 构造 string
    string str3(str, 2, 6);
    std::cout << "str3: " << str3 << std::endl;

    // 以 s 所指向的字符串的首 count 个字符构造 string
    string str4(str.c_str(), 3);
    std::cout << "str4: " << str4 << std::endl;

    // 以 s 所指向的空终止字符串的副本所初始化的内容构造 string
    string str5(str.c_str());
    std::cout << "str5: " << str5 << std::endl;

    // 构造拥有范围 [first, last) 内容的 string
    string str6(str.begin(), str.end());
    std::cout << "str6: " << str6 << std::endl;

    // 复制构造函数。构造拥有 other 内容副本的 string
    string str7(str);
    std::cout << "str7: " << str7 << std::endl;

    // 移动构造函数。用移动语义构造拥有 other 内容的 string
    string str8(string("abc") + string("def"));
    std::cout << "str8: " << str8 << std::endl;

    string str9 {'a', 'b', 'c', 'd'};
    std::cout << "str9: " << str9 << std::endl;

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1004602.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号