map of valarray

W

woessner

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
 
M

Marcus Kwok

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.
 
J

Jonathan Mcdougall

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,839
Latest member
MartinaBur

Latest Threads

Top