Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > std::valarray as value in std::map

Reply
Thread Tools

std::valarray as value in std::map

 
 
Chris Forone
Guest
Posts: n/a
 
      11-03-2007
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
 
Reply With Quote
 
 
 
 
Chris Forone
Guest
Posts: n/a
 
      11-03-2007
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
 
Reply With Quote
 
 
 
 
Chris Forone
Guest
Posts: n/a
 
      11-03-2007
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
 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      11-05-2007
On 2007-11-03 10:20:01 -0400, Chris Forone <> said:

> 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)

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      11-05-2007
On Nov 5, 2:22 pm, Pete Becker <p...@versatilecoding.com> wrote:
> On 2007-11-03 10:20:01 -0400, Chris Forone <4...@gmx.at> said:
> > 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:
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

 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      11-05-2007
On 2007-11-05 12:13:44 -0500, James Kanze <> said:

> On Nov 5, 2:22 pm, Pete Becker <p...@versatilecoding.com> wrote:
>
>> 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)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how this is calculated "BW Util has exceeded the threshold value of 60%,The present value is 62% -" philip007 Cisco 1 01-31-2006 03:09 PM
The value of MCSD: added value to a degree? uv2003 MCSD 4 06-23-2005 05:41 AM
DataView Find method - Error: Expecting 2 value(s) for the key being indexed, but received 3 value(s) Zenobia ASP .Net 2 06-23-2004 05:15 PM
RE: Textbox postback putting value,value =?Utf-8?B?QW5hY2hvc3RpYw==?= ASP .Net 1 06-21-2004 10:50 AM
copying value of DDL in a Datagrid "pre-edit command" to value in "post edit command" San Diego Guy ASP .Net 0 08-07-2003 08:59 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57