initializer lists and user-defined containers

Discussion in 'C++' started by jfindlay@gmail.com, Oct 14, 2004.

  1. Guest

    Is it possible to populate a container with an initializer list? I am
    not too particular about how hackish or ugly any suggestions might be,
    I'm simply interested in investigating possible elegancies (or
    perversities) for an initializer list.

    const container<double> array = mitigating_struct<double>() = {pi, e,
    phi, gamma, 0.0042};
    // container is magically filled with initalizer list members

    Of course, something without an explicit temporary would be more ideal,
    but perhaps beyond the realm of C++.


    Justin
    , Oct 14, 2004
    #1
    1. Advertising

  2. wrote:
    > Is it possible to populate a container with an initializer list? I am
    > not too particular about how hackish or ugly any suggestions might be,
    > I'm simply interested in investigating possible elegancies (or
    > perversities) for an initializer list.
    >
    > const container<double> array = mitigating_struct<double>() = {pi, e,
    > phi, gamma, 0.0042};
    > // container is magically filled with initalizer list members
    >
    > Of course, something without an explicit temporary would be more ideal,
    > but perhaps beyond the realm of C++.


    It sounds like this is probably not what you want but it should be
    interesting anyway.

    http://bdsoft.com/tools/initutil.html
    Gianni Mariani, Oct 14, 2004
    #2
    1. Advertising

  3. wrote:
    > Is it possible to populate a container with an initializer list? I am
    > not too particular about how hackish or ugly any suggestions might be,
    > I'm simply interested in investigating possible elegancies (or
    > perversities) for an initializer list.
    >
    > const container<double> array = mitigating_struct<double>() = {pi, e,
    > phi, gamma, 0.0042};
    > // container is magically filled with initalizer list members
    >
    > Of course, something without an explicit temporary would be more ideal,
    > but perhaps beyond the realm of C++.



    You may use a built in array:


    int array[]={1,2,3,4,5,6,7};

    vector<int> v(array, array+7);



    or using a temp:


    vector<int> v;

    {
    int temp[]={1,2,3,4,5,6,7};

    v.assign(temp, temp+7);
    }



    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
    Ioannis Vranos, Oct 14, 2004
    #3
  4. Ioannis Vranos, Oct 14, 2004
    #4
  5. <> wrote in message
    news:...
    | Is it possible to populate a container with an initializer list? I am
    | not too particular about how hackish or ugly any suggestions might be,
    | I'm simply interested in investigating possible elegancies (or
    | perversities) for an initializer list.
    |
    | const container<double> array = mitigating_struct<double>() = {pi, e,
    | phi, gamma, 0.0042};
    | // container is magically filled with initalizer list members
    |
    | Of course, something without an explicit temporary would be more ideal,
    | but perhaps beyond the realm of C++.

    maybe boost.assign will be just what you want (see www.boost.org) It will be
    in the next boost release, but you can
    already get it from the main cvs if you want.

    Shortly put (and among other ways), it allows you to say

    const container<double> array = list_of<double>( pi )( e )( phi )( gamma )(
    0.00042 );

    br

    Thorsten
    Thorsten Ottosen, Oct 14, 2004
    #5
    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. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    401
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  2. Oodini
    Replies:
    1
    Views:
    1,766
    Keith Thompson
    Sep 27, 2005
  3. Alden Pierre

    STL: User Defined Containers

    Alden Pierre, May 19, 2006, in forum: C++
    Replies:
    1
    Views:
    286
    mlimber
    May 19, 2006
  4. Replies:
    7
    Views:
    553
    Pete Becker
    Jan 25, 2008
  5. Sebastian Mach
    Replies:
    5
    Views:
    311
Loading...

Share This Page