map of valarray

Discussion in 'C++' started by woessner@gmail.com, May 24, 2006.

  1. Guest

    Hi all,

    I just tried to create a map<int, valarray<int> > and got some really
    weird behavior. Here's a simple example:

    int main()
    {
    std::map<int, std::valarray<int> > m;
    std::valarray<int> v(3);

    v[0] = 13;
    v[1] = 42;
    v[2] = 99;

    m[0] = v;

    std::cout << m[0][0] << ", " << m[0][1] << ", " << m[0][2] << '\n';

    return EXIT_SUCCESS;
    }

    Instead of getting "13, 42, 99", I got "0, 0, 0". And, sure enough, if
    I change valarray to vector, I get the expected output.

    This is my first time using valarrays. Is there some hidden pitfall
    I'm missing? Or is this possible an implementation problem? (For
    reference, I'm using g++ 4.1.0).

    Thanks in advance,
    Bill
    , May 24, 2006
    #1
    1. Advertising

  2. Marcus Kwok Guest

    wrote:
    > I just tried to create a map<int, valarray<int> > and got some really
    > weird behavior. Here's a simple example:
    >
    > int main()
    > {
    > std::map<int, std::valarray<int> > m;
    > std::valarray<int> v(3);
    >
    > v[0] = 13;
    > v[1] = 42;
    > v[2] = 99;
    >
    > m[0] = v;
    >
    > std::cout << m[0][0] << ", " << m[0][1] << ", " << m[0][2] << '\n';
    >
    > return EXIT_SUCCESS;
    > }
    >
    > Instead of getting "13, 42, 99", I got "0, 0, 0". And, sure enough, if
    > I change valarray to vector, I get the expected output.
    >
    > This is my first time using valarrays. Is there some hidden pitfall
    > I'm missing? Or is this possible an implementation problem? (For
    > reference, I'm using g++ 4.1.0).


    After adding the necessary #includes for <iostream>, <map>, and
    <valarray>, I get the output:

    13, 42, 99

    using VC++ 7.1 (VS .NET 2003). However, using g++ 3.4.4-2 I get the
    same "0, 0, 0" output, so I would guess that it's a g++ implementation
    issue.

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
    Marcus Kwok, May 24, 2006
    #2
    1. Advertising

  3. wrote:
    > Hi all,
    >
    > I just tried to create a map<int, valarray<int> > and got some really
    > weird behavior. Here's a simple example:
    >
    > int main()
    > {
    > std::map<int, std::valarray<int> > m;
    > std::valarray<int> v(3);
    >
    > v[0] = 13;
    > v[1] = 42;
    > v[2] = 99;
    >
    > m[0] = v;


    Use std::map::insert() here. The operator[] first creates an empty
    valarray and then calls operator= on it, passing 'v', which has 3
    elements. Assigining a valarray of a different size is undefined
    behavior. That's what happening here. Visual C++ 2005 prints "13, 42,
    99" and g++ 3.4.4 prints "0, 0, 0". By making this

    m.insert(std::make_pair(0, v));

    everything works.

    > std::cout << m[0][0] << ", " << m[0][1] << ", " << m[0][2] << '\n';
    >
    > return EXIT_SUCCESS;
    > }
    >
    > Instead of getting "13, 42, 99", I got "0, 0, 0". And, sure enough, if
    > I change valarray to vector, I get the expected output.
    >
    > This is my first time using valarrays. Is there some hidden pitfall
    > I'm missing? Or is this possible an implementation problem? (For
    > reference, I'm using g++ 4.1.0).



    Jonathan
    Jonathan Mcdougall, May 24, 2006
    #3
  4. Guest

    How insidious! But it makes sense now. Thanks for your quick reply.
    , May 25, 2006
    #4
    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. Glen Low
    Replies:
    3
    Views:
    980
    Glen Low
    Jun 26, 2003
  2. Marc Schellens

    Re: valarray resize

    Marc Schellens, Jul 29, 2003, in forum: C++
    Replies:
    5
    Views:
    1,280
    Marc Schellens
    Jul 30, 2003
  3. Jim West
    Replies:
    2
    Views:
    495
    Jim West
    Dec 23, 2003
  4. map of valarray

    , May 24, 2006, in forum: C++
    Replies:
    0
    Views:
    299
  5. Chris Forone

    std::valarray as value in std::map

    Chris Forone, Nov 3, 2007, in forum: C++
    Replies:
    5
    Views:
    381
    Pete Becker
    Nov 5, 2007
Loading...

Share This Page