What are the complexity guarantees of the standard containers?

前端 未结 3 1927
夕颜
夕颜 2020-11-22 11:10

Apparently ;-) the standard containers provide some form of guarantees.

What type of guarantees and what exactly are the differences between the different types of c

相关标签:
3条回答
  • 2020-11-22 11:22

    I'm not aware of anything like a single table that lets you compare all of them in at one glance (I'm not sure such a table would even be feasible).

    Of course the ISO standard document enumerates the complexity requirements in detail, sometimes in various rather readable tables, other times in less readable bullet points for each specific method.

    Also the STL library reference at http://www.cplusplus.com/reference/stl/ provides the complexity requirements where appropriate.

    0 讨论(0)
  • 2020-11-22 11:24

    Another quick lookup table is available at this github page

    Note : This does not consider all the containers such as, unordered_map etc. but is still great to look at. It is just a cleaner version of this

    0 讨论(0)
  • 2020-11-22 11:35

    I found the nice resource Standard C++ Containers. Probably this is what you all looking for.

    VECTOR

    Constructors

    vector<T> v;              Make an empty vector.                                     O(1)
    vector<T> v(n);           Make a vector with N elements.                            O(n)
    vector<T> v(n, value);    Make a vector with N elements, initialized to value.      O(n)
    vector<T> v(begin, end);  Make a vector and copy the elements from begin to end.    O(n)
    

    Accessors

    v[i]          Return (or set) the I'th element.                        O(1)
    v.at(i)       Return (or set) the I'th element, with bounds checking.  O(1)
    v.size()      Return current number of elements.                       O(1)
    v.empty()     Return true if vector is empty.                          O(1)
    v.begin()     Return random access iterator to start.                  O(1)
    v.end()       Return random access iterator to end.                    O(1)
    v.front()     Return the first element.                                O(1)
    v.back()      Return the last element.                                 O(1)
    v.capacity()  Return maximum number of elements.                       O(1)
    

    Modifiers

    v.push_back(value)         Add value to end.                                                O(1) (amortized)
    v.insert(iterator, value)  Insert value at the position indexed by iterator.                O(n)
    v.pop_back()               Remove value from end.                                           O(1)
    v.assign(begin, end)       Clear the container and copy in the elements from begin to end.  O(n)
    v.erase(iterator)          Erase value indexed by iterator.                                 O(n)
    v.erase(begin, end)        Erase the elements from begin to end.                            O(n)
    

    For other containers, refer to the page.

    0 讨论(0)
提交回复
热议问题