使用较少的< string> ... [英] using less<string>...
问题描述
- - - - - - - - - - - - - - -
#include< iostream>
#include< string>
#include< map>
using namespace std;
int main ()
{
字符串名称;
int pop;
string states [] = {" Wyoming"," Colorado"," Nevada",
" Montana"," Arizona"," Idaho"};
int pops [] = {470,2890,800,787,2718,944};
map< string,int,less< string> > mapStates;
//尝试用以下内容替换上面的内容:
// map< string,int> mapStates;
map< string,int,less< string> > :: iterator iter; // iterator
for(int j = 0; j< 6; j ++)
{
name = states [j ]。 //从数组中获取数据
pop = pops [j];
mapStates [name] = pop; //把它放在地图
}
cout<< 输入状态:; //从用户获取状态
cin>>姓名;
pop = mapStates [name]; //找到人口
cout<< 人口: << pop<< ",000; \\ n";
cout<< ENDL; //显示整个地图
for(iter = mapStates.begin(); iter!= mapStates.end(); iter ++)
cout<< (* iter).first<< ''''<< (* iter).second<< ",000 $ \\ nn ;;
返回0;
}
- - - - - - - - - - - - - - -
问题是:我看不出以下任何区别:
map< string,int,less< string> > mapStates;
和
map< string,int> mapStates;
我试图谷歌一下,发现两行都调用了地图
构造函数来创建一个地图< string,int>和较少的< string>是一个
比较函数 - 一个'弱排序''。我能看到少于< string>
在某处定义了吗?
为什么我看不出这两种方法有什么区别?是少于< string>
默认?是否还有一个< string> -function?
所以我也尝试在 www.cppreference.com 有一些很好的描述,但我找不到它......
有人可以向我澄清这个问题吗?
最好的问候/ Med venlig hilsen
Martin J?rgensen
- -
----------------------------------------- ----------------------------------
Martin J?rgensen的家 - < a rel =nofollowhref =http://www.martinjoergensen.dktarget =_ blank> http://www.martinjoergensen.dk
Hi,
- - - - - - - - - - - - - - -
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main()
{
string name;
int pop;
string states[] = { "Wyoming", "Colorado", "Nevada",
"Montana", "Arizona", "Idaho"};
int pops[] = { 470, 2890, 800, 787, 2718, 944 };
map<string, int, less<string> > mapStates;
// Try replacing the above with:
// map<string, int> mapStates;
map<string, int, less<string> >::iterator iter; //iterator
for(int j=0; j<6; j++)
{
name = states[j]; //get data from arrays
pop = pops[j];
mapStates[name] = pop; //put it in map
}
cout << "Enter state: "; //get state from user
cin >> name;
pop = mapStates[name]; //find population
cout << "Population: " << pop << ",000\n";
cout << endl; //display entire map
for(iter = mapStates.begin(); iter != mapStates.end(); iter++)
cout << (*iter).first << '' '' << (*iter).second << ",000\n";
return 0;
}
- - - - - - - - - - - - - - -
The question is: I don''t see any difference between:
map<string, int, less<string> > mapStates;
and
map<string, int> mapStates;
I tried to google around a bit and found that both lines call the map
constructor to create a map<string, int> and the less<string> is a
comparison function - a ''weak ordering''. Can I see how less<string> is
defined somewhere?
Why don''t I see any difference between both methods? Is less<string>
default? Is there also a more<string>-function?
So I also tried to look for it at www.cppreference.com which has some
nice descriptions but I couldn''t find it...
Can anyone clarify this issue to me?
Best regards / Med venlig hilsen
Martin J?rgensen
--
---------------------------------------------------------------------------
Home of Martin J?rgensen - http://www.martinjoergensen.dk
推荐答案
Martin J?rgensen写道:
Martin J?rgensen wrote:
- - - - - - - - - - - - - - -
#include< iostream>
#include< string>
#include< map>
使用命名空间std;
int main()
{
字符串名称;
int pop;
string states [] = {" Wyoming"," Colorado"," Nevada",
" Montana"," Arizona"," Idaho"};
int pops [] = {470,2890,800,787,2718,944};
map< string,int,less< string> > mapStates;
//尝试用以下内容替换上面的内容:
// map< string,int> mapStates;
map< string,int,less< string> > :: iterator iter; // iterator
for(int j = 0; j< 6; j ++)
{
name = states [j]; //从数组中获取数据
pop = pops [j];
mapStates [name] = pop; //把它放在地图
}
cout<< 输入状态:; //从用户获取状态
cin>> name;
pop = mapStates [name]; //找到人口
cout<< 人口: << pop<< ",000; \\ n";
cout<< ENDL; //显示整个地图
for(iter = mapStates.begin(); iter!= mapStates.end(); iter ++)
cout<< (* iter).first<< ''''<< (* iter).second<< ",000 \ n";
返回0;
}
- - - - - - - - - - - - - - -
map< string,int,less< string> > mapStates;
和
map< string,int> mapStates;
没有区别。地图的默认比较是
std :: less< Key>所以你只是明确地提供了默认参数。
我试着稍微google一下,发现两行都调用了地图
构造函数来创建一个地图< string,int>和较少的< string>是一个比较函数 - 一个'弱排序''。我能看到少于< string>
是在某个地方定义的吗?
Hi,
- - - - - - - - - - - - - - -
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main()
{
string name;
int pop;
string states[] = { "Wyoming", "Colorado", "Nevada",
"Montana", "Arizona", "Idaho"};
int pops[] = { 470, 2890, 800, 787, 2718, 944 };
map<string, int, less<string> > mapStates;
// Try replacing the above with:
// map<string, int> mapStates;
map<string, int, less<string> >::iterator iter; //iterator
for(int j=0; j<6; j++)
{
name = states[j]; //get data from arrays
pop = pops[j];
mapStates[name] = pop; //put it in map
}
cout << "Enter state: "; //get state from user
cin >> name;
pop = mapStates[name]; //find population
cout << "Population: " << pop << ",000\n";
cout << endl; //display entire map
for(iter = mapStates.begin(); iter != mapStates.end(); iter++)
cout << (*iter).first << '' '' << (*iter).second << ",000\n";
return 0;
}
- - - - - - - - - - - - - - -
The question is: I don''t see any difference between:
map<string, int, less<string> > mapStates;
and
map<string, int> mapStates;
There is no difference. The default Comparison for a map is
std::less<Key> so you''ve simply explicitly provided the default parameter.
I tried to google around a bit and found that both lines call the map
constructor to create a map<string, int> and the less<string> is a
comparison function - a ''weak ordering''. Can I see how less<string> is
defined somewhere?
你可以查看你的实现的定义,尽管最后
你会发现它等同于运算符< ;.
-Mark
You can look at your implementation''s definition although in the end
you''ll find it''s equivalent to operator<.
-Mark
Martin J?rgensen写道:
Martin J?rgensen wrote:
[..]
问题是:我没有看到任何区别:
map< string,int,less< string> > mapStates;
和
map< string,int> mapStates;
没有(除了在前一种情况下输入更多内容)。
我试图谷歌一下,发现两条线都叫地图
构造函数来创建一个地图< string,int>和较少的< string>是一个比较函数 - 一个'弱排序''。我能看到少于< string>是在某处定义的?
它在< functional>中定义标题。
为什么我看不出这两种方法有什么区别?少于< string>
默认?
是。
是否还有更多< string> -function?
不,不是''更多< string>''。它被称为更大< string>。
所以我也试图在 www.cppreference.com 有一些很好的描述,但我找不到它......
任何人都可以向我澄清这个问题吗?
[..]
The question is: I don''t see any difference between:
map<string, int, less<string> > mapStates;
and
map<string, int> mapStates;
There isn''t any (except for more typing in the former case).
I tried to google around a bit and found that both lines call the map
constructor to create a map<string, int> and the less<string> is a
comparison function - a ''weak ordering''. Can I see how less<string> is
defined somewhere?
It''s defined in <functional> header.
Why don''t I see any difference between both methods? Is less<string>
default?
Yes.
Is there also a more<string>-function?
No, not ''more<string>''. It''s called greater<string>.
So I also tried to look for it at www.cppreference.com which has some
nice descriptions but I couldn''t find it...
Can anyone clarify this issue to me?
关于标准库的好书可以。 Nicolai Josuttis发布了一个非常好的
好的。
V
-
请删除资金从我通过邮件回复的地址开始
A good book on Standard Library can. Nicolai Josuttis published a very
good one.
V
--
Please remove capital As from my address when replying by mail
Mark P写道:
Martin J?rgensen写道:
-snip-
没有区别。地图的默认比较是
std :: less< Key>所以你只是明确地提供了默认参数。
Martin J?rgensen wrote: -snip-
There is no difference. The default Comparison for a map is
std::less<Key> so you''ve simply explicitly provided the default parameter.
我是这么认为的。
I thought so.
我试着稍微google一下,发现两行都调用地图
构造函数来创建一个地图< string,int>和较少的< string>是一个比较函数 - 一个'弱排序''。我能看到少于< string>
是在某个地方定义的?
I tried to google around a bit and found that both lines call the map
constructor to create a map<string, int> and the less<string> is a
comparison function - a ''weak ordering''. Can I see how less<string> is
defined somewhere?
您可以查看实现的定义,但最终会发现它等同于运算符< ;。
You can look at your implementation''s definition although in the end
you''ll find it''s equivalent to operator<.
我在一台计算机上使用g ++,在另一台计算机上使用MS Visual studio 2005.
我的实现在哪里'定义?
致敬/ Med venlig hilsen
Martin J?rgensen
-
------------------------------------------------ ---------------------------
Martin J?rgensen的故乡 - http://www.martinjoergensen.dk
这篇关于使用较少的< string> ...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!