c++ - Arrays vs Vectors: Introductory Similarities and Differences -


what differences between array , vector in c++? example of differences might included libraries, symbolism, abilities, etc.

array

arrays contain specific number of elements of particular type. compiler can reserve required amount of space when program compiled, must specify type , number of elements array contain when defined. compiler must able determine value when program compiled. once array has been defined, use identifier array along index access specific elements of array. [...] arrays zero-indexed; is, first element @ index 0. indexing scheme indicative of close relationship in c++ between pointers , arrays , rules language defines pointer arithmetic.

— c++ pocket reference

vector

a vector dynamically-sized sequence of objects provides array-style operator[] random access. member function push_back copies arguments via copy constructor, adds copy last item in vector , increments size one. pop_back exact opposite, removing last element. inserting or deleting items end of vector takes amortized constant time, , inserting or deleting other location takes linear time. these basics of vectors. there lot more them. in cases, vector should first choice on c-style array. first of all, dynamically sized, means can grow needed. don't have sorts of research figure out optimal static size, in case of c arrays; vector grows needed, , can resized larger or smaller manually if need to. second, vectors offer bounds checking at member function (but not operator[]), can if reference nonexistent index instead of watching program crash or worse, continuing execution corrupt data.

— c++ cookbook

arrays:

  • are builtin language construct;
  • come unmodified c89;
  • provide contiguous, indexable sequence of elements; no bells , whistles;
  • are of fixed size; can't resize array in c++ (unless it's array of pod , it's allocated malloc);
  • their size must compile-time constant unless allocated dynamically;
  • they take storage space depending scope declare them;
  • if dynamically allocated, must explicitly deallocate them;
  • if dynamically allocated, pointer, , can't determine size; otherwise, can use sizeof (hence common idiom sizeof(arr)/sizeof(*arr), fails silently when used inadvertently on pointer);
  • automatically decay pointers in situations; in particular, happens when passing them function, requires passing separate parameter size;
  • can't returned function;
  • can't copied/assigned directly;
  • dynamical arrays of objects require default constructor, since elements must constructed first;

std::vector:

  • is template class;
  • is c++ construct;
  • is implemented dynamic array;
  • grows , shrinks dynamically;
  • automatically manage memory, freed on destruction;
  • can passed to/returned functions (by value);
  • can copied/assigned (this performs deep copy of stored elements);
  • doesn't decay pointers, can explicitly pointer data (&vec[0] guaranteed work expected);
  • always brings along internal dynamic array size (how many elements stored) , capacity (how many elements can stored in allocated block);
  • the internal dynamic array not allocated inside object (which contains few "bookkeeping" fields), allocated dynamically allocator specified in relevant template parameter; default 1 gets memory freestore (the so-called heap), independently how actual object allocated;
  • for reason, may less efficient "regular" arrays small, short-lived, local arrays;
  • when reallocating, objects copied (moved, in c++11);
  • does not require default constructor objects being stored;
  • is better integrated rest of so-called stl (it provides begin()/end() methods, usual stl typedefs, ...)

also consider "modern alternative" arrays - std::array; described in another answer difference between std::vector , std::array, may want have @ it.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -