473,396 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

map of valarray

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 '06 #1
3 2466
wo******@gmail.com 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
May 24 '06 #2
wo******@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

May 24 '06 #3
How insidious! But it makes sense now. Thanks for your quick reply.

May 25 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Christian Brechbühler | last post by:
The template std::valarray behaves pretty much like a mathematical vector. Arithmetic operators apply elementwise. Now I'd like to extend this to a user-defined type, e.g., complex. ...
6
by: Steven T. Hatton | last post by:
I bought Josuttis's book on the repeated recommendations of people in this newsgroup. http://www.josuttis.com/libbook/ One of the first things I looked up was the std::valarray<>. And what I...
3
by: Peter | last post by:
Hi everybody, I am unfortunately stuck with a probably very simple problem. I made a class called Particle with a valarray (STL) as a class member. The code for the class goes like this: ...
1
by: ES Kim | last post by:
comp.std.c++ would be a better place for this question. Forgive me, but I can't post anything on moderated newsgroups for some reason. valarray doesn't have iterators of its own, which makes...
1
by: ES Kim | last post by:
Here's a code fragment from TC++PL, p679: void f(valarray<double>& v) { size_t i = { 3, 2, 1, 0 }; valarray<size_t> index(i, 4); valarray<double> vv = log(v); } Every compiler I tried...
2
by: Michael Hopkins | last post by:
Hi all I have a subclass of valarray<T> thus template <typename T> class uo_val : public std::valarray<T> { public: uo_val ( ) : std::valarray<T>() {} uo_val (const int sz ) :...
1
by: Dack | last post by:
Hi, I want to track memory leaks in my application (that is using <valarray>). I used the following code: #define _CRTDBG_MAP_ALLOC #include <stdlib.h> #include <crtdbg.h> But then, when I...
9
by: Jim | last post by:
Hi, I want to declare that that a valarray of a certain name exist at the beginning of some code, but I can't instatiate it until I've read in some parameters later on in a for loop i.e. int...
2
by: john | last post by:
Hi, in TC++PL3 on page 665, regarding valarray member functions, it is mentioned: "valarray operator-() const; // result= -v for every element // similarly: +, ~, !" I checked the web and...
43
by: john | last post by:
Hi, in TC++PL 3 on pages 674-675 it is mentioned: "Maybe your first idea for a two-dimensional vector was something like this: class Matrix { valarray< valarray<doublev; public: // ... };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.