std::vector

Discussion in 'C++' started by Priya Mishra, Dec 16, 2005.

  1. Priya Mishra

    Priya Mishra Guest

    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
    Priya Mishra, Dec 16, 2005
    #1
    1. Advertising

  2. 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
    Neelesh Bodas, Dec 16, 2005
    #2
    1. Advertising

  3. Priya Mishra

    Simon Biber Guest

    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.
    Simon Biber, Dec 16, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Jansson
    Replies:
    5
    Views:
    6,273
    Ivan Vecerina
    Mar 17, 2005
  2. Anonymous
    Replies:
    20
    Views:
    4,276
    Pete Becker
    Mar 30, 2005
  3. Jason Heyes
    Replies:
    8
    Views:
    711
    Andrew Koenig
    Jan 15, 2006
  4. Replies:
    8
    Views:
    1,896
    Csaba
    Feb 18, 2006
  5. Rune Allnor
    Replies:
    4
    Views:
    929
    Rune Allnor
    Dec 11, 2008
Loading...

Share This Page