-10

What is vector< int > v(N) ?

  1. Is it an array which can have the maximum size of N (like vector<int> v)?

  2. is it same as vector<int> v[N]?

I think it is the first one(correct me if I am wrong).

What is vector< vector< int > > V(N)? Is it like 2D array with maximum size N? What is the difference between vector < int > v[N] and vector< vector< int > >v(N)?

YSC
  • 38,212
  • 9
  • 96
  • 149
Sammee Sharma
  • 91
  • 2
  • 3
  • 12
  • 1
    "Is it a dynamic array which can have the maximum size of N(like vector v)?" - _array_ means something quite particular in C++, and while a vector behaves much like an array, it is not an array. You should re-word your question. – davmac Feb 18 '16 at 10:22
  • 6
    Seriously not even the slightest effort made. [`vector`](http://en.cppreference.com/w/cpp/container/vector/vector) (3). – luk32 Feb 18 '16 at 10:22
  • Dynamic was in case of size not with dynamic values . sorry for writing dynamic array – Sammee Sharma Feb 18 '16 at 10:28
  • The downvotes **raining** down on this one should tell you there is something *inherently* wrong with your question. **Could you at least stop editing it**, because every single of your edits invalidates the answer? – DevSolar Feb 18 '16 at 10:50
  • Even though the question is terrible, I wouldn't advise to close it since it got a good answer. – YSC Feb 18 '16 at 12:01
  • In its current state, this seems a reasonable question which has attracted a good answer. – Adrian McCarthy Mar 19 '16 at 14:09

2 Answers2

11

Assuming that

  • N is some kind of integer,
  • there is an #include <vector> somewhere,
  • there is either a using namespace std; or a using std::vector; somewhere...

This is the declaration of an object v, of type std::vector< int >, initialized to hold N objects (of type int), which are default-initialized (i.e., indeterminate, since int is a POD type for which no default initialization is defined).

Documentation of vector constructors -- this one is case (2).

Spiral rule -- which needs some adapting to C++ but is still a good start.

It is not "a dynamic array line vector v", it is a vector.

And no, it is not the same as vector v[N] (which would not even compile). It is also not the same as vector<int> v[N] -- that would be an array of N different vector<int> objects.


Now if it is the first one then what is vector< vector< int > > V(N) ?

Since it's not the first one, do I still have to answer this? :-D

vector< vector< int > > V(N);

That is the declaration of an object V of type vector< vector< int > >, i.e. "vector of int-vectors", initialized to hold N default-initialized objects...

...i.e., a vector holding N objects of vector< int > -- which are, in turn, empty (because "empty" is what default-initialized vectors are).


C++ has...

  • The array (int v[N];), which works exactly like the C array.
  • The std::vector (std::vector< int > v;), which is dynamic in size.
  • The std::array (std::array< int, N >;), which is static in size like the C array, but does offer some of the niceties of std::vector.

You need to be exact about what you are referring to. These are quite distinct types with a distinct feature set.


UPDATE:

With your latest couple of edits, it became clear that your real question is:

What is a vector?

It's a C++ class template implementing a container that can hold contiguous elements of a given type (like a C array), but is dynamically-sized (unlike a C array).

See the documentation.

Generally speaking, you don't use C arrays in C++ code anymore, except for some really special cases. You use std::string for strings, and std::vector for (almost) everything else you used arrays for in C.

DevSolar
  • 67,862
  • 21
  • 134
  • 209
  • is vector < vector < int > > like vector < int > v[N] ? – Sammee Sharma Feb 18 '16 at 10:34
  • @SammeeSharma: I strongly suggest you pick up [a good C++ book](http://stackoverflow.com/questions/388242). A `std::vector` is to a C-style array like a 2015 Sedan to a Ford T. Yes, both hold contiguous objects and are commonly accessed via `v[ index ]`, but that is about as far as similarities go. And *try* to refer to the documentation of the types you are using... – DevSolar Feb 18 '16 at 10:41
2

std::vector<int> v(N)declares a variable v with the type of std::vector<int> and initializes the vector's size to hold N ints which are default initialized, meaning in this case that their initial value is undefined.

std::vector is a template from the C++ standard library which implements a resizable array whose elements are stored contiguously in memory. It is essentially a safer alternative to C arrays.

Kristian Duske
  • 1,769
  • 9
  • 14
  • Upvoted for the avatar alone. :-D – DevSolar Feb 18 '16 at 11:01
  • 1
    Thanks, and yes, I am positive that default initialized non-POD types are initialized to indeterminate values, which is a better word than saying that the initial value is undefined. Either way, it follows the general C++ rule of "You don't pay for what you don't use". See here: http://en.cppreference.com/w/cpp/language/default_initialization -- If you want, you could provide the initial value to the constructor as the second argument, though. – Kristian Duske Feb 18 '16 at 11:08
  • Yes, found the info (on that very page...) and edited the quesion out of my comment (and the clarification into my answer). Since I am pretty OCD about initializing my values, the matter just never came up for me. ;-) – DevSolar Feb 18 '16 at 11:09