473,395 Members | 1,468 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,395 software developers and data experts.

std::valarray as value in std::map

Hello group,

g++ (3.4.2, mingw):

float init[] = {1.f, 2.f, 3.f};
std::map<std::string, std::valarray<float mp;

mp["name"] = std::valarray(init, 3);
mp["name"].size(); // should be 3, but IS 0!

Do i ignored something? Does map not do a copy of value?

HAND, Chris
Nov 3 '07 #1
5 2594
Chris Forone schrieb:
Hello group,

g++ (3.4.2, mingw):

float init[] = {1.f, 2.f, 3.f};
std::map<std::string, std::valarray<float mp;

mp["name"] = std::valarray(init, 3);
mp["name"].size(); // should be 3, but IS 0!

Do i ignored something? Does map not do a copy of value?

HAND, Chris
compilable:

#include <iostream>
#include <map>
#include <valarray>

int main()
{
float init[] = {1.f, 2.f, 3.f};
std::map<std::string, std::valarray<float mp;

mp["name"] = std::valarray<float>(init, 3);

std::cout << mp["name"].size() << std::endl;
}

g++ (4.1.2, ubuntu) has the same behavior...

Chris
Nov 3 '07 #2
Chris Forone schrieb:
Chris Forone schrieb:
>Hello group,

g++ (3.4.2, mingw):

float init[] = {1.f, 2.f, 3.f};
std::map<std::string, std::valarray<float mp;

mp["name"] = std::valarray(init, 3);
mp["name"].size(); // should be 3, but IS 0!

Do i ignored something? Does map not do a copy of value?

HAND, Chris

compilable:

#include <iostream>
#include <map>
#include <valarray>

int main()
{
float init[] = {1.f, 2.f, 3.f};
std::map<std::string, std::valarray<float mp;

mp["name"] = std::valarray<float>(init, 3);

std::cout << mp["name"].size() << std::endl;
}

g++ (4.1.2, ubuntu) has the same behavior...

Chris
SOLVED!

..insert does the trick... operator[] only does std-ctor...

HAND, Chris
Nov 3 '07 #3
On 2007-11-03 10:20:01 -0400, Chris Forone <4o**@gmx.atsaid:
Chris Forone schrieb:
>Chris Forone schrieb:
>>Hello group,

g++ (3.4.2, mingw):

float init[] = {1.f, 2.f, 3.f};
std::map<std::string, std::valarray<float mp;

mp["name"] = std::valarray(init, 3);
mp["name"].size(); // should be 3, but IS 0!

Do i ignored something? Does map not do a copy of value?

HAND, Chris

compilable:

#include <iostream>
#include <map>
#include <valarray>

int main()
{
float init[] = {1.f, 2.f, 3.f};
std::map<std::string, std::valarray<float mp;
mp["name"] = std::valarray<float>(init, 3);
std::cout << mp["name"].size() << std::endl;
}

g++ (4.1.2, ubuntu) has the same behavior...

Chris

SOLVED!

.insert does the trick... operator[] only does std-ctor...
No, it doesn't. It just happens to look like it works. The problem has
nothing to do with valarray: you'll see the same behavior with any type
for the value. The problem is in the index, not the value. Quoted
strings are not guaranteed to be unique, so mp["name"] may be a
different map element than some other mp["name"].

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Nov 5 '07 #4
On Nov 5, 2:22 pm, Pete Becker <p...@versatilecoding.comwrote:
On 2007-11-03 10:20:01 -0400, Chris Forone <4...@gmx.atsaid:
Chris Forone schrieb:
Chris Forone schrieb:
Hello group,
>g++ (3.4.2, mingw):
>float init[] = {1.f, 2.f, 3.f};
std::map<std::string, std::valarray<float mp;
>mp["name"] = std::valarray(init, 3);
mp["name"].size(); // should be 3, but IS 0!
>Do i ignored something? Does map not do a copy of value?
>HAND, Chris
compilable:
#include <iostream>
#include <map>
#include <valarray>
int main()
{
float init[] = {1.f, 2.f, 3.f};
std::map<std::string, std::valarray<float mp;
mp["name"] = std::valarray<float>(init, 3);
std::cout << mp["name"].size() << std::endl;
}
g++ (4.1.2, ubuntu) has the same behavior...
SOLVED!
.insert does the trick... operator[] only does std-ctor...
No, it doesn't. It just happens to look like it works. The
problem has nothing to do with valarray: you'll see the same
behavior with any type for the value. The problem is in the
index, not the value. Quoted strings are not guaranteed to be
unique, so mp["name"] may be a different map element than some
other mp["name"].
But his map uses std::string as a key, not char const*, so the
string literal will be converted.

I'm not familiar enough with valarray to really comment, but his
initial use of std::map seems correct: the call to mp["name"]
does insert a valarray constructed withe the default
constructor, before returning a reference to the new object; the
assignment operator then does whatever the assignment operator
for valarray does.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Nov 5 '07 #5
On 2007-11-05 12:13:44 -0500, James Kanze <ja*********@gmail.comsaid:
On Nov 5, 2:22 pm, Pete Becker <p...@versatilecoding.comwrote:
>No, it doesn't. It just happens to look like it works. The
problem has nothing to do with valarray: you'll see the same
behavior with any type for the value. The problem is in the
index, not the value. Quoted strings are not guaranteed to be
unique, so mp["name"] may be a different map element than some
other mp["name"].

But his map uses std::string as a key, not char const*, so the
string literal will be converted.
Whoops, sorry about confusing the issue.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Nov 5 '07 #6

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

Similar topics

3
by: Woodster | last post by:
I have declared the following std::map<std::string, std::string> myMap; to pass myMap to functions should I be declaring functions as: void function(std::map<std::string, std::string>); ...
2
by: Serengeti | last post by:
Hello, in my class I have a map that translates strings to pointers to some member functions. The code goes like this: class F { typedef void (Function::*MathFuncPtr)(); std::map<std::string,...
19
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not...
3
by: Dan Trowbridge | last post by:
Hi everyone, In my attempt to port code from VS 6.0 to VS.NET I had some code break along the way, mostly due to not adhereing closely to the C++ standard. This may be another instance but I...
1
by: Avery Fong | last post by:
The following program will result in a compile error when building under Debug but will compile under Release. Why does is work under Release mode but not under Debug This program is developed...
13
by: kamaraj80 | last post by:
Hi I am using the std:: map as following. typedef struct _SeatRowCols { long nSeatRow; unsigned char ucSeatLetter; }SeatRowCols; typedef struct _NetData
5
by: Diwa | last post by:
Does the "value" type (value as in key-value pair )of "std::map" require a default ctor even if it is not used ? If I comment out Line 1 in the code attached later, i.e remove the default ctor...
2
by: digz | last post by:
Hi, I am trying to write a program which has two threads one of them write to a map , and the other one deletes entries based on a certain criterion.. first I cannot get the delete portion to...
6
by: Juha Nieminen | last post by:
joseph cook wrote: Not always. By default, yes, but you can specify other comparators, eg: std::map<int, int, std::greaterreversedMap;
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.