Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > std::vector

Reply
Thread Tools

std::vector

 
 
Priya Mishra
Guest
Posts: n/a
 
      12-16-2005
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

 
Reply With Quote
 
 
 
 
Neelesh Bodas
Guest
Posts: n/a
 
      12-16-2005
Priya Mishra wrote:
>
> #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

 
Reply With Quote
 
 
 
 
Simon Biber
Guest
Posts: n/a
 
      12-16-2005
Neelesh Bodas wrote:
> 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;
}

--
Simon.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments