向量与字符串 [英] Vector vs string

查看:31
本文介绍了向量与字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C++ std::vector 和 std::basic_string 之间的根本区别是什么(如果有的话)?

What is the fundamental difference, if any, between a C++ std::vector and std::basic_string?

推荐答案

  • basic_string 不会调用其元素的构造函数和析构函数.矢量确实如此.

    • basic_string doesn't call constructors and destructors of its elements. vector does.

      交换 basic_string 会使迭代器失效(启用小字符串优化),交换向量不会.

      swapping basic_string invalidates iterators (enabling small string optimization), swapping vectors doesn't.

      basic_string 内存在 C++03 中不能连续分配.向量总是连续的.这个区别在 C++0x [string.require] 中被移除了:

      basic_string memory may not be allocated continuously in C++03. vector is always continuous. This difference is removed in C++0x [string.require]:

      basic_string 对象中的类似字符的对象应连续存储

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

    • basic_string 具有字符串操作的接口.向量没有.

    • basic_string has interface for string operations. vector doesn't.

      basic_string 可以使用写时复制策略(在 C++11 之前).矢量不能.

      basic_string may use copy on write strategy (in pre C++11). vector can't.

      非信徒的相关引述:

      [基本字符串]:

      类模板 basic_string 符合 Sequence Container (23.2.3) 的要求,对于一个Reversible Container (23.2),以及一个 Allocator-aware 的容器(表 99),除了 basic_string不使用 allocator_traits::construct 和 allocator_- 构造或销毁其元素traits::destroy 和用于 basic_string 的 swap() 使迭代器失效.支持的迭代器通过 basic_string 是随机访问迭代器(24.2.7).

      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).

      这篇关于向量与字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆