Hello friends
Can anyone tell me Why and How to use vector in C++ ????
Making a sudoko game in c++

first of all read this
http://www.cplusplus.com/reference/stl/vector/
shows you how to use a vector.

A vector is basically a dynamic array in which means you do not need to define the size before compiling it is done automatically

first of all read this
http://www.cplusplus.com/reference/stl/vector/
shows you how to use a vector.

A vector is basically a dynamic array in which means you do not need to define the size before compiling it is done automatically

Be careful how you say that. The resizing is only done under certain conditions, and only if you use the correct operators/functions. If you use the subscript operator (operator[]) improperly, you will still get segmentation faults.

In order for the vector to resize, you need to use vector::resize() or vector::push_back(). I think vector::reserve() can cause a resize as well, but the site you linked seems to be having issues at the moment.

I think vector::reserve() can cause a resize as well, but the site you linked seems to be having issues at the moment.

Confirmed, now that the site is (kind of) working. The member functions vector::reserve() and vector::insert() both can cause re-allocation as well.

it was just a simple way of explaining it which some times helps people get their heads round things

it was just a simple way of explaining it which some times helps people get their heads round things

Which is fine. But your language was misleading. The "Dynamic" nature of the vector only comes in to play if you use it correctly. Your post makes it sound like you use it just like an array without regard for boundaries.

A better way to say it would probably be "A vector is basically an array that allows you to modify its capacity when necessary."

That's all I'm saying...

not trying to start a fight or anything but it does come across very critical. In theory a vector is dynamic is just a term in which you know can grow or shrink.

>>I think vector::reserve() can cause a resize
>>The member functions vector::reserve()... both can cause re-allocation as well

Just to let you know, your first statement is different from your second. reserve does not "resize" the vector as in vector::resize does, so just be weary of the lingo. And as for your second statement, you should point out that, vector::reserve does not affect its element when it tries to change its capacity.

I am aware of the difference. The first one should have been re-allocate as well, it seems I got ahead of myself while writing it.

For why to make use of vectors, the answer is very simple. They make programming easier to maintain and modify later. They Produce compact code.
They are part of STL of standard C++ Implementation.
You should read a good C++ book covering STL. I think that will help you much better, then being on any forum or searching the web.

Thanks and have good time learning C++.
-Manoj

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.