次のコードはコンパイルされません(Ideoneリンク:https://ideone.com/DKI9Fm)。なぜ?:この行はなぜコンパイルされませんか?
EDIT:std::map
のdocumentationを1として、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;
}
ここでは、/ usr/include/C++/6/bits/stl_map.h:1079:7:注意:引数1の変換は不正です: prog.cpp:22:28:エラー: 'const int *'から 'std :: map'への無効な変換 :: key_type {aka int *} '_ –
pirho
マップ内のキーとしてのポインタはほとんど常に間違っています。 –