C++ 为什么可以';我不能插入一个指向多集的常量指针吗?

C++ 为什么可以';我不能插入一个指向多集的常量指针吗?,c++,stl,C++,Stl,例如,根据multiset的文档,请参见。应该可以插入常量值。在我的示例中,multiset是指针的集合,但是当我尝试插入常量指针时,我得到了一个错误 template<typename Key> struct node_key: public node_base { node_key(Key k): _key(k) {} virtual ~node_key() {} const Key& key() const { return _key;}

例如,根据multiset的文档,请参见。应该可以插入常量值。在我的示例中,multiset是指针的集合,但是当我尝试插入常量指针时,我得到了一个错误

template<typename Key>
struct node_key: public node_base {
     node_key(Key k): _key(k) {}
     virtual ~node_key() {}
     const Key& key() const { return _key;}
protected:
     Key _key;
     void _copy_key(node_key<Key> *n) const { n->_key=_key;}
};

template <typename Compare>
struct ptr_less_key {
    ptr_less_key() : _comp() {}
    virtual ~ptr_less_key() {}

    template <typename Pointer>
    bool operator()(const Pointer& a, const Pointer& b) const { return _comp(a->key(), b->key()); }
    Compare _comp;
};

int main() {
  typedef node_key<int>* keyp;
  std::multiset<keyp,ptr_less_key<std::less<int>>> x;
  node_key<int> k(5);
  const node_key<int> *p=&k;
  x.insert(p); //this fails
  return 0;
}
模板
结构节点密钥:公共节点密钥{
节点密钥(密钥k):\u密钥(k){}
虚拟~node_key(){}
const Key&Key()const{return_Key;}
受保护的:
键(u键),;
void_copy_key(node_key*n)const{n->_key=_key;}
};
模板
结构ptr_less_键{
ptr_less_key():_comp(){}
虚拟~ptr_less_key(){}
模板
bool操作符()(常量指针&a,常量指针&b)常量{return}comp(a->key(),b->key());}
比较(comp),;
};
int main(){
typedef node_key*keyp;
std::多集x;
节点_键k(5);
const node_key*p=&k;
x、 插入(p);//此操作失败
返回0;
}

您当前正在执行的操作:您并没有像您认为的那样尝试插入常量指针,而是插入指向常量元素的非常量指针

改变这个

const node_key<int> *p=&k;
const node_key*p=&k;
对此

node_key<int> *const p=&k;
node_key*const p=&k;
使const关键字应用于指针而不是它所指向的对象。

 struct node {
     void member();
     void const_member() const;
 };
以这四项声明为例

 node*              pointer_to_node;
 const node*        pointer_to_const_node;
 node* const        const_pointer_to_node;
 const node* const  const_pointer_to_const_node;
const
ness有两个不同的方面:对象
节点的方面和指针的方面。前两个声明指向
节点
常量节点
的可变指针。允许(隐式)从
节点*
转换为
常量节点*
,但不能反过来,因为这样可以修改
常量节点

后两个声明声明各自的指针为常量,即这些指针不能修改(尽管
const\u pointer\u to\u node
指向的
节点可以修改)

 pointer_to_node->member();                              // okay
 pointer_to_node->const_member();                        // okay
 pointer_to_node = new node;                             // okay
 pointer_to_node = const_pointer_to_node;                // okay
 pointer_to_node = pointer_to_const_node;                // ERROR

 pointer_to_const_node->member();                        // ERROR
 pointer_to_const_node->const_member();                  // okay
 pointer_to_const_node = new node;                       // okay
 pointer_to_const_node = pointer_to_node;                // okay
 pointer_to_const_node = const_pointer_to_node;          // okay
 pointer_to_const_node = const_pointer_to_const_node;    // okay

 const_pointer_to_node->member();                        // okay
 const_pointer_to_node->const_member();                  // okay
 const_pointer_to_node = new node;                       // ERROR
 const_pointer_to_node = const_pointer_to_node;          // ERROR
 const_pointer_to_node = pointer_to_const_node;          // ERROR

 const_pointer_to_const_node->member();                  // ERROR
 const_pointer_to_const_node->const_member();            // okay
 const_pointer_to_const_node = new node;                 // ERROR 
 const_pointer_to_const_node = const_pointer_to_node;    // ERROR 
 const_pointer_to_const_node = pointer_to_const_node;    // ERROR 

“为什么要使用指针?你想用它解决的真正和实际的问题是什么?”一些程序员为了说明我的问题,我当然已经编写了代码。