C++ 什么';地图怎么了?

C++ 什么';地图怎么了?,c++,map,C++,Map,我期望: 3 Spain Donna Elvira England Jane Doe Spain Donna Anna 但我收到: England 1 Spain 2 我确信变量名每次都是应该的,但是映射出了问题 将生成日志更改为std::string时的生成日志 Spain 3 1>----构建已启动:项目:UVa,配置:调试Win32------ 1> UVa.cpp 1> c:\program files(x86)\microsoft visual studio 11.0\vc\in

我期望:

3
Spain Donna Elvira
England Jane Doe
Spain Donna Anna
但我收到:

England 1
Spain 2
我确信变量名每次都是应该的,但是映射出了问题

将生成日志更改为std::string时的生成日志

Spain 3
1>----构建已启动:项目:UVa,配置:调试Win32------
1> UVa.cpp

1> c:\program files(x86)\microsoft visual studio 11.0\vc\include\xstddef(180):错误C2784:“bool std::operator
char*
作为指针而不是字符串进行比较。改用
std::string


正如P0W所提到的,
scanf
printf
get
都是C风格的。映射和迭代器是C++风格的。不要把它们混在一起。使用<代码> STD::Cix>代码>代码> STD::CUT,<代码> STD::C++中的IO GETLION<代码> .< > >代码> char */COD>作为指针,而不是字符串。改用
std::string


正如P0W所提到的,
scanf
printf
get
都是C风格的。映射和迭代器是C++风格的。不要把它们混在一起。使用< C++ >代码:> CIN < /代码>,<代码> STD::CUT,<代码> STD::GILLION/CODE> IO中的C++。

< P>如果您以“代码> char */CODE >为关键字,您实际上是映射了字符串的地址,而不是将字符串本身作为键。由于每次都在堆栈上以相同的方式分配缓冲区,因此无论内容如何,它都很可能最终位于相同的地址并映射到相同的值


从STD::String,C++替代C字符串的映射,将自动使用字符串的内容作为键。

如果以“代码> char */CODE >为关键字,您实际上是映射了字符串的地址,而不是将字符串本身作为键。由于每次都在堆栈上以相同的方式分配缓冲区,因此无论内容如何,它都很可能最终位于相同的地址并映射到相同的值


从STD::String,C++替代C字符串,映射将自动使用字符串的内容作为键。< /P>请不要混合C和C++。不要使用<代码>获取< /代码>;它从根本上是不安全的。在将

char*
更改为
std::string
后,您可能会收到这些错误,因为您正在将
name
(即
char[]
)传递到
std::map::operator[](std::string)
,如你所见,它不带字符指针。请不要混合C和C++。不要使用<代码>获取< /代码>;它从根本上是不安全的。在将
char*
更改为
std::string
后,可能会出现这些错误,因为您正在将
名称
(这是一个
char[]
)传递到
std::map::operator[](std::string)
,正如您所看到的,它不带字符指针。事实上,不要把
get
和任何东西混在一起。@md5:除非你没有明目张胆地说“不要使用
printf
”,否则可能会有一个blenderI'd+1。在编译时已知的输出字符串时,这是一个比任何东西都重要的首选项。这是我在将其更改为std::string@CoffeeandCode你在谈论高级话题,比如性能。我怀疑OP是否能胜任这些工作。所以简单的建议是最好的。事实上,不要把
get
和任何东西混在一起。@md5:如果你没有明目张胆地说“不要使用
printf
”的话,可能除了blenderI'd+1之外。在编译时已知的输出字符串时,这是一个比任何东西都重要的首选项。这是我在将其更改为std::string@CoffeeandCode你在谈论高级话题,比如性能。我怀疑OP是否能胜任这些工作。所以简单的建议是最好的。
Spain 3
1>------ Build started: Project: UVa, Configuration: Debug Win32 ------
1>  UVa.cpp
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstddef(180): error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtree(2245) : see declaration of 'std::operator <'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstddef(179) : while compiling class template member function 'bool std::less<_Ty>::operator ()(const _Ty &,const _Ty &) const'
1>          with
1>          [
1>              _Ty=std::string
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\map(194) : see reference to function template instantiation 'bool std::less<_Ty>::operator ()(const _Ty &,const _Ty &) const' being compiled
1>          with
1>          [
1>              _Ty=std::string
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\type_traits(743) : see reference to class template instantiation 'std::less<_Ty>' being compiled
1>          with
1>          [
1>              _Ty=std::string
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtree(1028) : see reference to class template instantiation 'std::is_empty<_Ty>' being compiled
1>          with
1>          [
1>              _Ty=std::less<std::string>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\map(67) : see reference to class template instantiation 'std::_Tree<_Traits>' being compiled
1>          with
1>          [
1>              _Traits=std::_Tmap_traits<std::string,int,std::less<std::string>,std::allocator<std::pair<const std::string,int>>,false>
1>          ]
1>          c:\users\amiraz\documents\visual studio 2012\projects\uva\uva\uva.cpp(13) : see reference to class template instantiation 'std::map<_Kty,_Ty>' being compiled
1>          with
1>          [
1>              _Kty=std::string,
1>              _Ty=int
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtree(2245) : see declaration of 'std::operator <'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtree(2245) : see declaration of 'std::operator <'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtree(2245) : see declaration of 'std::operator <'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstddef(180): error C2784: 'bool std::operator <(const std::move_iterator<_RanIt> &,const std::move_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::move_iterator<_RanIt> &' from 'const std::string'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1983) : see declaration of 'std::operator <'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1983) : see declaration of 'std::operator <'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1983) : see declaration of 'std::operator <'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1983) : see declaration of 'std::operator <'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstddef(180): error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const std::string'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1259) : see declaration of 'std::operator <'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1259) : see declaration of 'std::operator <'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1259) : see declaration of 'std::operator <'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1259) : see declaration of 'std::operator <'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstddef(180): error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'const std::string'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1075) : see declaration of 'std::operator <'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1075) : see declaration of 'std::operator <'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1075) : see declaration of 'std::operator <'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1075) : see declaration of 'std::operator <'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstddef(180): error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const std::string'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\utility(232) : see declaration of 'std::operator <'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\utility(232) : see declaration of 'std::operator <'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\utility(232) : see declaration of 'std::operator <'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\utility(232) : see declaration of 'std::operator <'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstddef(180): error C2676: binary '<' : 'const std::string' does not define this operator or a conversion to a type acceptable to the predefined operator
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========