Can I assign safely an array to vector?

Discussion in 'C++' started by Ram Laxman, Apr 11, 2004.

  1. Ram Laxman

    Ram Laxman Guest

    Hi all,
    Can I assign an array to a vector if both(array and vector)
    holds same data type?
     
    Ram Laxman, Apr 11, 2004
    #1
    1. Advertising

  2. Ram Laxman

    Jeff Schwab Guest

    Ram Laxman wrote:
    > Hi all,
    > Can I assign an array to a vector if both(array and vector)
    > holds same data type?


    Yes, but maybe not using the syntax you expect.


    #include <vector>

    int main( )
    {
    std::vector<int> vector;
    int array[ ] = { 1, 2, 3 };

    vector.assign( array, array + 3 );
    }
     
    Jeff Schwab, Apr 11, 2004
    #2
    1. Advertising

  3. "Jeff Schwab" <> wrote:
    > Ram Laxman wrote:
    > > Hi all,
    > > Can I assign an array to a vector if both(array and vector)
    > > holds same data type?

    >
    > Yes, but maybe not using the syntax you expect.
    >
    >
    > #include <vector>
    >
    > int main( )
    > {
    > std::vector<int> vector;
    > int array[ ] = { 1, 2, 3 };
    >
    > vector.assign( array, array + 3 );
    > }


    Or just:

    #include <vector>

    int main()
    {
    int array[] = { 1, 2, 3 };
    std::vector<int> vec(array, array + sizeof(array));
    }

    Best regards,

    Tom
     
    Thomas Tutone, Apr 11, 2004
    #3
  4. Thomas Tutone wrote:

    > Or just:
    >
    > #include <vector>
    >
    > int main()
    > {
    > int array[] = { 1, 2, 3 };
    > std::vector<int> vec(array, array + sizeof(array));


    This will probably not work correctly. You wanted the number of elements
    in the array, not the number of bytes used to store the array.

    int array[] = { 1, 2, 3 };
    int size = sizeof array / sizeof *array;
    std::vector<int> vec(array, array + size);

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
     
    Kevin Goodsell, Apr 11, 2004
    #4
  5. Ram Laxman

    Jeff Schwab Guest

    Thomas Tutone wrote:
    > "Jeff Schwab" <> wrote:
    >
    >>Ram Laxman wrote:
    >>
    >>>Hi all,
    >>> Can I assign an array to a vector if both(array and vector)
    >>>holds same data type?

    >>
    >>Yes, but maybe not using the syntax you expect.
    >>
    >>
    >>#include <vector>
    >>
    >>int main( )
    >>{
    >> std::vector<int> vector;
    >> int array[ ] = { 1, 2, 3 };
    >>
    >> vector.assign( array, array + 3 );
    >>}

    >
    >
    > Or just:
    >
    > #include <vector>
    >
    > int main()
    > {
    > int array[] = { 1, 2, 3 };
    > std::vector<int> vec(array, array + sizeof(array));
    > }


    That's not assignment, it's initialization.
     
    Jeff Schwab, Apr 11, 2004
    #5
  6. "Kevin Goodsell" <> wrote in message
    news:9dhec.5748$...
    >
    > > #include <vector>
    > >
    > > int main()
    > > {
    > > int array[] = { 1, 2, 3 };
    > > std::vector<int> vec(array, array + sizeof(array));

    >
    > This will probably not work correctly. You wanted the number of elements
    > in the array, not the number of bytes used to store the array.


    Oops. Absolutely right.

    Best regards,

    Tom
     
    Thomas Tutone, Apr 12, 2004
    #6
    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. pmatos
    Replies:
    6
    Views:
    24,049
  2. pkirk25
    Replies:
    27
    Views:
    617
    Christopher Layne
    Oct 3, 2006
  3. Replies:
    8
    Views:
    1,991
    Csaba
    Feb 18, 2006
  4. Chris Roth

    Vector Assign vs Vector operator=

    Chris Roth, Feb 21, 2007, in forum: C++
    Replies:
    4
    Views:
    385
    Ron Natalie
    Feb 22, 2007
  5. Javier
    Replies:
    2
    Views:
    607
    James Kanze
    Sep 4, 2007
Loading...

Share This Page