gpt4 book ai didi

c++ - vector 与字符串

转载 作者:IT老高 更新时间:2023-10-28 22:00:47 28 4
gpt4 key购买 nike

C++ std::vector 和 std::basic_string 之间的根本区别是什么?

最佳答案

  • basic_string 不调用其元素的构造函数和析构函数。 vector 可以。

  • 交换 basic_string 会使迭代器无效(启用小字符串优化),交换 vector 不会。

  • basic_string 内存在 C++03 中可能不会连续分配。 vector 总是连续的。这种差异在 C++0x [string.require] 中被移除:

    The char-like objects in a basic_string object shall be stored contiguously

  • basic_string 具有字符串操作的接口(interface)。 vector 没有。

  • basic_string 可以使用写时复制策略(在 C++11 之前)。 vector 不能。

非信徒的相关引述:

[基本字符串]:

The class template basic_string conforms to the requirements for a Sequence Container (23.2.3), for a Reversible Container (23.2), and for an Allocator-aware container (Table 99), except that basic_string does not construct or destroy its elements using allocator_traits::construct and allocator_- traits::destroy and that swap() for basic_string invalidates iterators. The iterators supported by basic_string are random access iterators (24.2.7).

关于c++ - vector 与字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4557009/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com