c++ - 哪种数据结构更适合 std 字符串数组

标签 c++ string data-structures queue

我需要一个结构如下:

Figure1

结构必须保持固定大小 std::string s 使得它的元素个数是 finit (100 - 10000000)。

我希望能够按如下方式随机访问每个元素:

std::string Temp = MyStrcuture[i];

MyStrcuture[i] = std::string Temp;

我必须使用没有(可能)内存泄漏的最快结构。

哪个更适合我?

  1. std::string* MyStrcuture = new std::string[Nu_of_Elements];
  2. std::queue< std:string> MyStrcuture(Nu_of_Elements);
  3. std::vector< std:string> MyStrcuture(Nu_of_Elements);
  4. boost::circular_buffer< std::string> MyStrcuture(Nu_of_Elements);
  5. 你的建议?

最佳答案

std::vector< std:string> MyStrcuture(Nu_of_Elements);

Vector最适合您的要求。它支持基于索引的元素访问,因为元素存储在连续的内存地址中,并且具有大小灵 active 。


  1. std:string* MyStrcuture = new std::string[Nu_of_Elements];

    C++ STL vector vs array in the real world

  2. std::queue MyStrcuture(Nu_of_Elements);

    How do I get the nth item in a queue in java?
    不支持基于索引的元素访问。

  3. std::vector MyStrcuture(Nu_of_Elements);

    Clean-up : vector 的析构函数自动调用 vector 中每个元素的析构函数。

  4. Boost::circular_buffer MyStrcuture(Nu_of_Elements);

    与第二个原因相同。 Know more

关于c++ - 哪种数据结构更适合 std 字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40373397/

相关文章:

c++ - 使用指向对象的指针进行析构

c++ - 编译器似乎更改了我的 const 参数

r - 拆分和子串字符串列表

将十六进制值转换为 C 中字符串类型的 ip 地址。

mysql - 将字符串中的每个单词插入 MySql 中单独的行

c++ - 输出二叉搜索树中叶节点的数量

python - 在 Python 中模拟关系数据(或数据库)?

c++ - 常量数组

c++ - 是否可以使 QML ListView 循环?

algorithm - 二叉树的垂直和