std::valarray as value in std::map

C

Chris Forone

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
 
C

Chris Forone

Chris said:
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
 
C

Chris Forone

Chris said:
Chris said:
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
 
P

Pete Becker

Chris said:
Chris said:
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"].
 
J

James Kanze

Chris said:
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.
 
P

Pete Becker

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.
 

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

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top