std::vector

P

Priya Mishra

Hi All

It was very nice to intract with this group, While in my previous post,
I was suggested to reffer the link, in order to learn C++,

Well I was going thoruigh the link in which i had some querry,

well below is the code.

#include <vector>
class Fred {
public:
Fred(int i, int j);
...
};

int main()
{
std::vector<Fred> a(10, Fred(5,7));
}

int main()
{
Fred a[10] = {
Fred(5,7), Fred(5,7), Fred(5,7), Fred(5,7), Fred(5,7),
Fred(5,7), Fred(5,7), Fred(5,7), Fred(5,7), Fred(5,7)
};
...
}

If we see both the above code, I come to conclusion is that,
Both are achiving the same fucntionalites, But the thing what
I want to know is what exactly std::vector do, ???

No doubt there must be some core diffrence between the above
implemented code.

Please I am try to learn C++ as all these days i have wroked with C
lang.

Thanks In Advance
Priya
 
N

Neelesh Bodas

Priya said:
#include <vector>
class Fred {
public:
Fred(int i, int j);
...
};

int main()
{
std::vector<Fred> a(10, Fred(5,7));
}

int main()
{
Fred a[10] = {
Fred(5,7), Fred(5,7), Fred(5,7), Fred(5,7), Fred(5,7),
Fred(5,7), Fred(5,7), Fred(5,7), Fred(5,7), Fred(5,7)
};
...
}

If we see both the above code, I come to conclusion is that,
Both are achiving the same fucntionalites, But the thing what
I want to know is what exactly std::vector do, ???

No doubt there must be some core diffrence between the above
implemented code.

1. vectors grow dynamically, arrays do not
2. Size of array needs to be known at compile time. Not for vectors.
(this is related to 1)
3. There is no bound-checking for arrays, for vector there is a way to
throw exception if you go beyond bounds.
4. Arrays cannot be passed by value (if you ever want to pass for some
reason), vectors can be.
5. Arrays cannot be returned by value, vectors can be.

Note that "std::vector" is not the only container provided by the
standard, there is also std::list and std::deque. Each serves a
specific purpose.

These and some more reasons also explain why "arrays are evil"
http://www.parashift.com/c++-faq-lite/containers.html
 
S

Simon Biber

Neelesh said:
1. vectors grow dynamically, arrays do not
2. Size of array needs to be known at compile time. Not for vectors.
(this is related to 1)
3. There is no bound-checking for arrays, for vector there is a way to
throw exception if you go beyond bounds.
4. Arrays cannot be passed by value (if you ever want to pass for some
reason), vectors can be.
5. Arrays cannot be returned by value, vectors can be.

You can pass and return arrays by value if you wrap them in a struct.

struct array10int { int array[10]; };

array10int zero(void)
{
array10int a;
memset(a.array, 0, sizeof a.array);
return a;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top