simple map question

simple map question

Post by Scott Gustafs » Wed, 19 Jul 2000 04:00:00






>>  map<string, float> ages;
>>  string name = "bob";

>>  ages.insert(name, 25); // error occurs here

>An easier way to do this would be
>ages["bob"] = 25;

This is probably the best way.

Quote:>but if you really want to use insert then the correct syntax would be

>ages.insert(map<string, float>::value_type("bob", 25));

I have a question on this part. Can't you also just use the following:

ages.insert(ages.value_type("bob", 25));

At least it works on my compiler and I don't see a reason why it shouldn't
work for everyone.

Later,
scott

 
 
 

simple map question

Post by Josiah Manso » Thu, 20 Jul 2000 04:00:00


This is a very simple program where I am trying to get the STL class map to
work. It is a simple problem, and I have checked some literature, but oh
well. Please point out why this doesn't work. Thanks.

#include <map>
#include <string>
#include <iostream>

using namespace std;

int main()
{
 map<string, float> ages;
 string name = "bob";

 ages.insert(name, 25); // error occurs here

 cout << ages["bob"] << endl;

 return 0;

Quote:}


 
 
 

simple map question

Post by John Harriso » Thu, 20 Jul 2000 04:00:00



Quote:> This is a very simple program where I am trying to get the STL class map
to
> work. It is a simple problem, and I have checked some literature, but oh
> well. Please point out why this doesn't work. Thanks.

> #include <map>
> #include <string>
> #include <iostream>

> using namespace std;

> int main()
> {
>  map<string, float> ages;
>  string name = "bob";

>  ages.insert(name, 25); // error occurs here

An easier way to do this would be

ages["bob"] = 25;

but if you really want to use insert then the correct syntax would be

ages.insert(map<string, float>::value_type("bob", 25));

I'd seriously recommend that you typedef map<string,float> to make this sort
of thing easier to read.

john

- Show quoted text -

Quote:

>  cout << ages["bob"] << endl;

>  return 0;
> }

 
 
 

simple map question

Post by Josiah Manso » Thu, 20 Jul 2000 04:00:00


Thanks. I knew that it had to be simple.
 
 
 

simple map question

Post by Phli » Thu, 20 Jul 2000 04:00:00



> but if you really want to use insert then the correct syntax would be

> ages.insert(map<string, float>::value_type("bob", 25));

What's wrong with this?

    ages.insert(::std::make_pair ("bob", 25));

I know: Someone could overload or specialize 'map<string,
float>::value_type' to put some strange new type in. Right?

--
 Phlip
======= http://users.deltanet.com/~tegan/home.html =======

 
 
 

simple map question

Post by Homer Meye » Thu, 20 Jul 2000 04:00:00




> > but if you really want to use insert then the correct syntax would be

> > ages.insert(map<string, float>::value_type("bob", 25));

> What's wrong with this?

>     ages.insert(::std::make_pair ("bob", 25));

> I know: Someone could overload or specialize 'map<string,
> float>::value_type' to put some strange new type in. Right?

The only problem with that is that it generates a:

std::pair<const char*,int> rather than the std::pair<const std::string,
float> that is needed by std::map<std::string, float>.

--
Welcome to comp.lang.c++:  http://www.slack.net/~shiva/welcome.txt
comp.lang.c++ FAQ:  http://marshall-cline.home.att.net/cpp-faq-lite/
comp.lang.c FAQ:  http://www.eskimo.com/~scs/C-faq/top.html
alt.comp.lang.learn.c-c++:  http://www.raos.demon.co.uk/acllc-c++/faq.html

 
 
 

simple map question

Post by Andre Kost » Fri, 21 Jul 2000 04:00:00








>>>  map<string, float> ages;
>>>  string name = "bob";

>>>  ages.insert(name, 25); // error occurs here

>>An easier way to do this would be
>>ages["bob"] = 25;

>This is probably the best way.

>>but if you really want to use insert then the correct syntax would be

>>ages.insert(map<string, float>::value_type("bob", 25));

>I have a question on this part. Can't you also just use the following:

>ages.insert(ages.value_type("bob", 25));

>At least it works on my compiler and I don't see a reason why it
>shouldn't work for everyone.

>Later,
>scott

Are you formally allowed to refer to a typedef inside of a namespace/class
with the . operator?  Or does the standard specify that you should be using
the :: operator?
 
 
 

simple map question

Post by Scott Gustafs » Sat, 22 Jul 2000 04:00:00







>>>but if you really want to use insert then the correct syntax would be

>>>ages.insert(map<string, float>::value_type("bob", 25));

>>I have a question on this part. Can't you also just use the following:

>>ages.insert(ages.value_type("bob", 25));

>Are you formally allowed to refer to a typedef inside of a namespace/class
>with the . operator?  Or does the standard specify that you should be using
>the :: operator?

I thought that if you use the :: operator, then you need to be refering to
a type name. For example:

      map<string, float>::value_type

What I tried is to use the existing object to get the typename out of it.

     ages.value_type

That way you can change the object and hopefully the code elsewhere doesn't
have to be updated. I did look in my C++ books and I couldn't find any
reason why this would work.

Later,
scott

 
 
 

simple map question

Post by Andre Kost » Sat, 22 Jul 2000 04:00:00




Quote:

>I thought that if you use the :: operator, then you need to be refering
>to a type name. For example:

>      map<string, float>::value_type

Yep, you have to use :: with the type, and not the instance.

Quote:>What I tried is to use the existing object to get the typename out of
>it.

>     ages.value_type

I'm not sure if the standard agrees with you on this (I don't have a copy
of the standard, I'm hoping someone who does will jump in with the
correct/standard answer for this).  

Quote:>That way you can change the object and hopefully the code elsewhere
>doesn't have to be updated. I did look in my C++ books and I couldn't
>find any reason why this would work.

I'm not sure that a typedef in a class is technically a member of the
class, or just merely an idenifier declared within the "namespace" of the
class.
 
 
 

simple map question

Post by Victor Bazaro » Sat, 22 Jul 2000 04:00:00





> >I thought that if you use the :: operator, then you need to be
refering
> >to a type name. For example:

> >      map<string, float>::value_type

> Yep, you have to use :: with the type, and not the instance.

> >What I tried is to use the existing object to get the typename out of
> >it.

> >     ages.value_type

> I'm not sure if the standard agrees with you on this (I don't have a
copy
> of the standard, I'm hoping someone who does will jump in with the
> correct/standard answer for this).

It is OK if value_type is a member or a base class.

Quote:

> >That way you can change the object and hopefully the code elsewhere
> >doesn't have to be updated. I did look in my C++ books and I couldn't
> >find any reason why this would work.

> I'm not sure that a typedef in a class is technically a member of the
> class, or just merely an idenifier declared within the "namespace" of
the
> class.

I think the latter is correct and not the former.

Victor
--
Please remove capital A's from my address when replying by mail