inline assignment of vectors

Discussion in 'C++' started by foice, Sep 14, 2010.

  1. foice

    foice Guest

    why can't I make the same inline assignment for vectors?
    float fp_values[] = { 0.1, 0.2 , 0.3, 0.4};

    as for instance this
    vector<float> v=(1.2, 3.3, 4.);

    any idea how to do that in one line and for arbitrary lenght of the
    initialization?

    thanks roberto
     
    foice, Sep 14, 2010
    #1
    1. Advertising

  2. foice wrote:
    > why can't I make the same inline assignment for vectors?
    > float fp_values[] = { 0.1, 0.2 , 0.3, 0.4};
    >
    > as for instance this
    > vector<float> v=(1.2, 3.3, 4.);
    >
    > any idea how to do that in one line and for arbitrary lenght of the
    > initialization?


    See here :

    http://live.boost.org/doc/libs/1_36_0/libs/assign/doc/index.html
     
    Vladimir Jovic, Sep 14, 2010
    #2
    1. Advertising

  3. foice

    foice Guest

    On Sep 14, 3:44 pm, Vladimir Jovic <> wrote:
    > foice wrote:
    > > why can't I make the same inline assignment for vectors?
    > > float fp_values[] = { 0.1, 0.2 , 0.3, 0.4};

    >
    > > as for instance this
    > > vector<float> v=(1.2, 3.3, 4.);

    >
    > > any idea how to do that in one line and for arbitrary lenght of the
    > > initialization?

    >
    > See here :
    >
    > http://live.boost.org/doc/libs/1_36_0/libs/assign/doc/index.html


    i see ... it's very interesting. though not standard :(
     
    foice, Sep 14, 2010
    #3
  4. foice wrote:
    > On Sep 14, 3:44 pm, Vladimir Jovic <> wrote:
    >> foice wrote:
    >>> why can't I make the same inline assignment for vectors?
    >>> float fp_values[] = { 0.1, 0.2 , 0.3, 0.4};
    >>> as for instance this
    >>> vector<float> v=(1.2, 3.3, 4.);
    >>> any idea how to do that in one line and for arbitrary lenght of the
    >>> initialization?

    >> See here :
    >>
    >> http://live.boost.org/doc/libs/1_36_0/libs/assign/doc/index.html

    >
    > i see ... it's very interesting. though not standard :(


    It's portable, though, which is usually good enough :)

    Stu
     
    Stuart Golodetz, Sep 14, 2010
    #4
  5. On Sep 14, 3:16 pm, foice <> wrote:
    > why can't I make the same inline assignment for vectors?
    > float fp_values[] = { 0.1, 0.2 , 0.3, 0.4};
    >
    > as for instance this
    > vector<float> v=(1.2, 3.3, 4.);


    As others have mentioned, you can do that sort of thing in the
    forthcoming C++ standard (C++0x). The correct syntax will be:

    std::vector<float> v = {1.2f, 3.3f, 4.f};

    I think the latest version of the GCC compiler already supports this
    if you enable the experimental C++0x features.

    > any idea how to do that in one line and for arbitrary lenght of the
    > initialization?


    In the current C++ standard (C++03), you can't do this in one line.
    But if you can live with doing it in three lines, this is the way I do
    it:

    const float kVArray[] = {1.2f, 3.3f, 4.f};

    const size_t kVArraySize = sizeof(kVArray) / sizeof(*kVArray);

    std::vector<float> v(kVArray, kVArray + kVArraySize);

    Hope this helps.

    Ángel José Riesgo
     
    Ángel José Riesgo, Sep 16, 2010
    #5
  6. foice

    James Kanze Guest

    On Sep 16, 12:47 pm, Ángel José Riesgo <> wrote:
    > On Sep 14, 3:16 pm, foice <> wrote:


    > > why can't I make the same inline assignment for vectors?
    > > float fp_values[] = { 0.1, 0.2 , 0.3, 0.4};


    > > as for instance this
    > > vector<float> v=(1.2, 3.3, 4.);


    > As others have mentioned, you can do that sort of thing in the
    > forthcoming C++ standard (C++0x). The correct syntax will be:


    > std::vector<float> v = {1.2f, 3.3f, 4.f};


    > I think the latest version of the GCC compiler already supports this
    > if you enable the experimental C++0x features.


    > > any idea how to do that in one line and for arbitrary lenght of the
    > > initialization?


    > In the current C++ standard (C++03), you can't do this in one line.
    > But if you can live with doing it in three lines, this is the way I do
    > it:


    > const float kVArray[] = {1.2f, 3.3f, 4.f};


    > const size_t kVArraySize = sizeof(kVArray) / sizeof(*kVArray);


    > std::vector<float> v(kVArray, kVArray + kVArraySize);


    It's usual to define the template functions begin and end, so
    you can write:

    float const kvArray[] = { 1.2f, 3.3f, 4.f };
    std::vector<float> v( begin( kvArray ), end( kvArray ) );

    --
    James Kanze
     
    James Kanze, Sep 18, 2010
    #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. Anand P Paralkar
    Replies:
    2
    Views:
    9,400
    Srinivasan Venkataramanan
    Aug 4, 2003
  2. Abhi
    Replies:
    2
    Views:
    741
    E. Robert Tisdale
    Jul 3, 2003
  3. slurper

    assignment of vectors

    slurper, Nov 24, 2004, in forum: C++
    Replies:
    8
    Views:
    457
  4. Replies:
    3
    Views:
    700
    Shadowman
    Mar 26, 2008
  5. Guest
    Replies:
    0
    Views:
    444
    Guest
    Sep 14, 2005
Loading...

Share This Page