C++ 为什么这一行不编译?

C++ 为什么这一行不编译?,c++,stl,C++,Stl,以下代码未编译(Ideone链接:)。为什么 编辑:根据std::map,const Key&类型被std::map::find接受。因此,将const int*传递到find()应该可以 #include <iostream> #include <map> using namespace std; class ConstDemo { std::map<int*, int> m_Map; public: ConstDemo(int count

以下代码未编译(Ideone链接:)。为什么

编辑:根据
std::map
const Key&
类型被
std::map::find
接受。因此,将
const int*
传递到
find()
应该可以

#include <iostream>
#include <map>
using namespace std;

class ConstDemo
{
    std::map<int*, int> m_Map;

public:
   ConstDemo(int count, int* pArray)
   {
       for(int i=0; i < count; ++i)
       {
          m_Map.insert(std::make_pair(&(pArray[i]),0));
       }

   }

   bool Find(const int* i) const
   {
       // DOESN"T COMPILE!
       return (m_Map.find(i) != m_Map.end());
   }


};


int main() {

    int a[10];
    ConstDemo cd(10, a);
    if(cd.Find(&a[5]))
       cout << "Found!" << std::endl;


    return 0;
}
#包括
#包括
使用名称空间std;
类ConstDemo
{
std::map mu map;
公众:
ConstDemo(整数计数,整数*帕雷)
{
对于(int i=0;icout
int*const
int*const
不一样。请尝试将其更改为
int*const

bool Find(int* const i) const
这是因为您的
key\u type
int*
std::map m\u map;
)和
m\u map.find
需要一个
const key\u type
作为参数,即在您的例子中是
int*const
。但是您正在传递一个
const int**

如果将
int*
传递给
m\u Map.find
,也可以,因为它可以将
int*
转换为
int*const
,但不能将
int*
转换为
const*

另外,在下一行int
main
的末尾缺少一个分号:

ConstDemo cd(10, a)
现在,在这里看到它

编辑

在编辑完你的问题之后

根据
std::map
的文档,
std::map::find
接受
const Key&
类型。因此将
const int*
传递到
find()
应该可以

#include <iostream>
#include <map>
using namespace std;

class ConstDemo
{
    std::map<int*, int> m_Map;

public:
   ConstDemo(int count, int* pArray)
   {
       for(int i=0; i < count; ++i)
       {
          m_Map.insert(std::make_pair(&(pArray[i]),0));
       }

   }

   bool Find(const int* i) const
   {
       // DOESN"T COMPILE!
       return (m_Map.find(i) != m_Map.end());
   }


};


int main() {

    int a[10];
    ConstDemo cd(10, a);
    if(cd.Find(&a[5]))
       cout << "Found!" << std::endl;


    return 0;
}
const Key&
是一个常量
Key
,因此在您的情况下,需要传递一个常量
int*
现在。但是
const int*
没有定义一个常量
int*
,它只定义了一个指向
const int
的指针
int*const
定义了一个常量
int*
,所以如果你传递
const int*
const*
int*const
,它就不会出错我。尝试将其更改为
int*const

bool Find(int* const i) const
这是因为您的
key\u type
int*
std::map m\u map;
)和
m\u map.find
需要一个
const key\u type
作为参数,即在您的例子中是
int*const
。但是您正在传递一个
const int**

如果将
int*
传递给
m\u Map.find
,也可以,因为它可以将
int*
转换为
int*const
,但不能将
int*
转换为
const*

另外,在下一行int
main
的末尾缺少一个分号:

ConstDemo cd(10, a)
现在,在这里看到它

编辑

在编辑完你的问题之后

根据
std::map
的文档,
std::map::find
接受
const Key&
类型。因此将
const int*
传递到
find()
应该可以

#include <iostream>
#include <map>
using namespace std;

class ConstDemo
{
    std::map<int*, int> m_Map;

public:
   ConstDemo(int count, int* pArray)
   {
       for(int i=0; i < count; ++i)
       {
          m_Map.insert(std::make_pair(&(pArray[i]),0));
       }

   }

   bool Find(const int* i) const
   {
       // DOESN"T COMPILE!
       return (m_Map.find(i) != m_Map.end());
   }


};


int main() {

    int a[10];
    ConstDemo cd(10, a);
    if(cd.Find(&a[5]))
       cout << "Found!" << std::endl;


    return 0;
}
const Key&
是一个常量
Key
,因此在您的情况下,需要传递一个常量
int*
现在。但是
const int*
并没有定义一个常量
int*
,它只是定义了一个指向
const int
的指针
int*const
定义了一个常量
int*
,所以如果你传递
const int*
,它就会给出错误。简短回答:

使用


长答覆:

在C++11之前,
std::map::find
的声明:

iterator find( const Key& key );
const_iterator find( const Key& key ) const;
参数类型是
常量键&
,而不是
键&

在您的情况下,
键是
int*
,而不是
cons int*

如果我们在声明中替换
,它们看起来像:

iterator find( int * const & key );  // Not "const int*&"
                                     // That's unforunate part
const_iterator find( int * const & key ) const;
不能使用类型为
const int*
的变量调用此类函数。这将允许函数中断指针指向的对象的
常量。

简短回答:

使用


长答覆:

在C++11之前,
std::map::find
的声明:

iterator find( const Key& key );
const_iterator find( const Key& key ) const;
参数类型是
常量键&
,而不是
键&

在您的情况下,
键是
int*
,而不是
cons int*

如果我们在声明中替换
,它们看起来像:

iterator find( int * const & key );  // Not "const int*&"
                                     // That's unforunate part
const_iterator find( int * const & key ) const;

此类函数不能使用类型为
const int*
的变量调用。这将允许函数中断指针指向的对象的
常量。

您读到这里了吗:/usr/include/c++/6/bits/stl_map.h:1079:7:注意:参数1的转换格式错误:prog.cpp:22:28:error:inv从“const int*”到“std::map::key_type{aka int*}”的alid转换映射中作为键的指针几乎总是错误的。您读到这里了吗:/usr/include/c++/6/bits/stl_map.h:1079:7:注意:参数1的转换格式错误:prog.cpp:22:28:错误:从“const int*”到“std::map::key_type{aka int*}”的转换无效“指针作为地图中的键几乎总是错误的。