What’s the standard or common data structure for a list of objects in C++?

In C++, what’s the standard or common data structure for a list of objects?

In C++, the common data structure for a sequence (“list”) of objects may be std::vector.

A vector is a dynamically resizable array. It is “The most extensively used container in the C++ Standard Library …, offering a combination of dynamic memory management and efficient random access.” — Lock-free Dynamically Resizable Arrays

And vector is the default first data structure you should think about for a problem for its compactness and efficiency. Bjarne Stroupstrup has an excellent talk on this, check it out.

From the standard draft C++11 (with important parts highlighted):

A vector is a sequence container that supports random access iterators. In addition, it supports (amortized) constant time insert and erase operations at the end; insert and erase in the middle take linear time. Storage management is handled automatically, though hints can be given to improve efficiency. The elements of a vector are stored contiguously

Check the standard draft section 23.3.6 for more details.

One usage example from cppreference.com:

#include <iostream>
#include <vector>
 
int main()
{
    // Create a vector containing integers
    std::vector<int> v = {7, 5, 16, 8};
 
    // Add two more integers to vector
    v.push_back(25);
    v.push_back(13);
 
    // Iterate and print values of vector
    for(int n : v) {
        std::cout << n << 'n';
    }
}

Run it on Caliru.

Similar Posts

  • Micosoft招聘部分算法题

    Micosoft招聘部分算法题 1.链表和数组的区别在哪里? 2.编写实现链表排序的一种算法。说明为什么你会选择用这样的方法? 3.编写实现数组排序的一种算法。说明为什么你会选择用这样的方法? 4.请编写能直接实现strstr()函数功能的代码。 5.编写反转字符串的程序,要求优化速度、优化空间。 6.在链表里如何发现循环链接? 7.给出洗牌的一个算法,并将洗好的牌存储在一个整形数组里。 8.写一个函数,检查字符是否是整数,如果是,返回其整数值。(或者:怎样只用4行代码编写出一个从字符串到长整形的函数?) 9.给出一个函数来输出一个字符串的所有排列。 10.请编写实现malloc()内存分配函数功能一样的代码。 11.给出一个函数来复制两个字符串A和B。字符串A的后几个字节和字符串B的前几个字节重叠。 12.怎样编写一个程序,把一个有序整数数组放到二叉树中? 13.怎样从顶部开始逐层打印二叉树结点数据?请编程。 14.怎样把一个链表掉个顺序(也就是反序,注意链表的边界条件并考虑空链表)? 来源:·日月光华 bbs.fudan.edu.cn Read more: How to Connect to MySQL in JSP OCaml Learning Materials Inline Assembly with GCC on Linux Online Tutorials for Linux Beginners How to Run a Command Upon Files or Directories Changes on Linux Filter Salutation in…

  • |

    Introduction to Xen Source Code Structure

    The Xen hypervisor is a powerful virtualization solution that provides virtualization capabilities for x86, x86-64, and ARM architectures. The source code structure of Xen is organized into several directories, each of which contains a specific set of files. The backend and frontend drivers are an important part of the Xen hypervisor, and they provide a…

  • How to print the name of the current file being edited in Emacs?

    In Emacs, how to print the name of the current file that I am editing? The built-in function buffer-file-name gives the full path of your file. To get the file name: M-: buffer-file-name Read more: How to merge a commit from another branch to my current branch in git? Linux Kernel: xt_quota: report initial quota…

Leave a Reply

Your email address will not be published. Required fields are marked *