Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > map of valarray

Reply
Thread Tools

map of valarray

 
 
woessner@gmail.com
Guest
Posts: n/a
 
      05-24-2006
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

 
Reply With Quote
 
 
 
 
Marcus Kwok
Guest
Posts: n/a
 
      05-24-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
Jonathan Mcdougall
Guest
Posts: n/a
 
      05-24-2006
(E-Mail Removed) 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

 
Reply With Quote
 
woessner@gmail.com
Guest
Posts: n/a
 
      05-25-2006
How insidious! But it makes sense now. Thanks for your quick reply.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
std::valarray as value in std::map Chris Forone C++ 5 11-05-2007 07:32 PM
map of valarray woessner@gmail.com C++ 0 05-24-2006 07:13 PM
Why not use valarray<valarray<T> > ? Jim West C++ 2 12-23-2003 03:54 PM
Re: valarray resize Marc Schellens C++ 5 07-30-2003 07:06 AM
New Altivec-optimized valarray implementation Glen Low C++ 3 06-26-2003 05:07 AM



Advertisments