9

What is the analogous class of C++ std::vector in C#?

I want a class where it keeps an internal array inside and supports insertion at the back in O(1) time.

2
  • 1
    There is no exact analogous of std::vector in C#. Sep 19, 2013 at 13:19
  • Why is an array implementation important? The more important aspect seems to be the constant append time.
    – Omaha
    Sep 19, 2013 at 13:19

2 Answers 2

31

Here is a list with some C++ / C# containers that are roughly equivalent (not exact replacements) to each other:

11
  • 2
    std::set - HashSet<T> ? Sep 19, 2013 at 13:22
  • But the OP wants O(1) insertion time, which doesn't exist in vector or List.
    – Cole Tobin
    Sep 19, 2013 at 13:22
  • 1
    OP only asks for O(1) insertion at the back
    – harold
    Sep 19, 2013 at 13:26
  • 2
    List<T> have O(1) in add at the back if the size less than capacity. It s very important. Sep 19, 2013 at 13:27
  • 1
    @WeylandYutani: neither List<T> nor vector are O(1) for all insertions (only those that happen at index == count, and if a resize occurs this wouldn't be the case either).
    – user7116
    Sep 19, 2013 at 13:37
-3
  • std::list -> LinkedList ?
  • std::vector -> List ?

std::list is backed by array, each deletion would incur memory copy, same behavior as C# List. while C# LinkedList is backed by node list, same as std::vector.

so I believe below is correct.

  • std::list -> List
  • std::vector -> LinkedList
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.