Skip to content
TheChatArchiveThe Chat Archive
Kimikimi-k2-0711-preview· Moonshot AIHugging Face LMArena Human Preference 140K

give me a quick rundown on how to define and use lists in c++

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with o4-mini-2025-04-16’s answer.
  1. User

    give me a quick rundown on how to define and use lists in c++

  2. Kimi Assistant

    Quick rundown – C++ “lists”

    1. Header

      #include <vector>        // dynamic array
      #include <list>          // doubly-linked list
      #include <forward_list>  // singly-linked list
      
    2. vector (99 % of CP cases)

      vector<int> a;           // empty
      vector<int> b(5, 42);    // {42,42,42,42,42}
      vector<int> c = {1,2,3}; // initializer-list
      

      Common ops

      a.push_back(x);          // O(1) amortized
      a.pop_back();            // O(1)
      a[i] = 7;                // random access
      a.size();                // #elements
      a.empty();               // bool
      a.clear();               // erase all
      sort(a.begin(), a.end());// <algorithm>
      
    3. list (doubly-linked)

      list<int> L = {10, 20, 30};
      L.push_front(5);         // O(1)
      L.push_back(35);         // O(1)
      L.pop_front();           // O(1)
      L.pop_back();            // O(1)
      auto it = L.begin(); ++it;
      L.insert(it, 99);        // insert before iterator
      L.erase(it);             // erase at iterator
      
    4. forward_list (singly-linked)

      forward_list<int> fl = {1,2,3};
      fl.push_front(0);        // O(1)
      fl.pop_front();          // O(1)
      
    5. Range-for loop (works for all)

      for (int x : a) cout << x << ' ';
      

    That’s it—vector is your default; use list/forward_list only when you need constant-time insert/erase in the middle.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.