vectors question

Hi all,
I have a question. after learning vectors as I knew it acts like an array with improved function, so will I need to use arrays again or vectors are enough ?
Internally a vector stores and manipulates an array, so there isn't an instance I can think of in which you could use an array but not a vector. In some cases an array will be faster but vectors are so much safer and will increase development speed, my advice would be stick with vectors.
The speed of vector(i am not sure about the vector<vector>) would be the same as the
dynamic array of C.
There are two kinds of arrays in C -- fixed-length arrays and variable-length arrays. std::vector<> most closely models
a variable-length array. boost::array<> implements a fixed-length array.

This is important because many people try to compare the execution time of an algorithm using std::vector<> and then
a fixed-length array. This is comparing apples to oranges, unless care is taken to ensure that std::vector<> behaves like
a fixed-length array. (The way to do that is to call std::vector<>.reserve() to "fix" the size of the array before inserting
elements.

So, ahmed93, you can get by exclusively with std::vector<> now, however I would also encourage you to look at
boost::array<>. The advantage of boost::array<> for fixed-length arrays over std::vector<> is that you can
initialize a boost::array<> as if it were a C array:

 
boost::array< int, 5 > a = { { 1, 2, 3, 4, 5 } };  // Extra set of braces needed by some compilers 


whereas you need to write a little more code to initialize a std::vector<>:

1
2
3
4
std::vector<int> v;
v.reserve( 5 );
for( int i = 1; i < 6; ++i )
    v.push_back( i );


The boost::array<> line is much easier to understand than the second because the boost::array
example is simply a declaration whereas the std::vector<> example requires you to figure out
the algorithm (albeit easy).
Topic archived. No new replies allowed.