简体   繁体   English

将对值分配给映射键时出错

[英]Error while assigning a pair value to a map key

This is excerpt from my code: 这是我的代码的摘录:

std::map<int, std::pair< const int, const std::vector<POINT_3d> > > m_srcHitData;
void addHit( const int edgeId, const int hit )
{
  m_srcHitData[edgeId] = std::make_pair( hit, std::vector<POINT_3d>() );
}

I keep getting the error: 我不断收到错误:

  stl_pair.h(180): error: no operator "=" matches these operands operand types are: const std::vector<POINT_3d, std::allocator<POINT_3d>> = const std::vector<POINT_3d, std::allocator<POINT_3d>> second = __p.second; ^ detected during instantiation of "std::pair<_T1, _T2> &std::pair<_T1, _T2>::operator=(const std::pair<_U1, _U2> &) 

What does that mean? 这意味着什么? I tried different approaches but still getting this or similar error. 我尝试了不同的方法,但仍然遇到此错误或类似错误。 Thank you! 谢谢!

Well, m_srcHitData[edgeId] is a pair with a const vector member. 好吧, m_srcHitData[edgeId]是一对带有const向量成员的对。 You can't simply assign to it, because that means assigning to the const vector, which is not possible... 您不能简单地分配给它,因为这意味着分配给const向量,这是不可能的...

As for what you can do about it, see: 至于您可以做什么,请参阅:

How to create a std::map of constant values which is still accessible by the [] operator? 如何创建一个常量值的std :: map,它仍然可以由[]运算符访问?

As @FrancisCugler suggests, that could be, for example, writing: 正如@FrancisCugler建议的那样,例如,可以这样写:

m_srcHitData[edgeId].insert( std::make_pair( hit, std::vector<POINT_3d>() );

However, if your vectors are long, you might not actually want to copy all that data around. 但是,如果向量很长,您实际上可能并不想复制所有这些数据。

This part in your code kind of looks ugly... 您代码中的这一部分看起来很难看...

std::map<int, std::pair< const int, const std::vector<POINT_3d> > > m_srcHitData;

You could try restructuring your code a little. 您可以尝试重组一下代码。

struct Pair {
    unsigned int key_;
    std::vector<POINT_3d> points_;

    Pair() {} // Empty Default
    Pair( const unsigned int& key, const std::vector<POINT_3d>& points ) :
        key_(key),
        points_( points ) 
    {}
};

Then... 然后...

std::map<unsigned, Pair> m_srcHitData;

void addHit( const int edgeId, const int hit ) {
    m_srcHitData[edgeId] = Pair( hit, std::vector<POINT_3d>() );
}

I made this short program to simulate a similar structure only I used strings in place of your std::vector<POINT_3d> 我做了这个简短的程序来模拟类似的结构,只是我用strings代替了std::vector<POINT_3d>

#include <string>
#include <iostream>
#include <map>

struct Pair {
    unsigned key_;
    std::string value_;

    Pair() {}
    Pair( const unsigned int& key, const std::string& value ) :
        key_( key ),
        value_( value ) {}
};

class MyClass {
public:
    std::map<unsigned, Pair> myMap_;

    void addValue( const unsigned int& key, const std::string& value ) {
        myMap_[key] = Pair( key, value );
    }
};

int main() {

    MyClass myClass;
    myClass.addValue( 1, "Hello" );
    myClass.addValue( 2, "World" );

    typedef std::map<unsigned, Pair>::iterator Iter;
    Iter it = myClass.myMap_.begin();

    for ( ; it != myClass.myMap_.end(); ++it ) {
        std::cout << "Key: " << it->first << " Pair-Key: " << it->second.key_ << " Pair-value: " << it->second.value_ << std::endl;
    }


    std::cout << "\nPress any key and enter to quit." << std::endl;
    char c;
    std::cin >> c;
}

You can use the above, except substitute your objects of vector<T> with the strings . 除了将vector<T>对象替换为strings之外,您可以使用上述内容。

I also used public interface on both the struct and class for simplicity of demonstration. 为了简化演示,我还在structclass上都使用了公共接口。 Normally the container in the class would be either protected or private with accessory functions. 通常, class的容器将通过附件功能被protectedprivate

EDIT This is to help construct the map first. 编辑这是为了帮助首先构建地图。 Once you have the map working then you can modify it to add in the const storage types if needed, but they can be tricky to work with. 启用地图后,可以根据需要对其进行修改以添加const存储类型,但是使用它们可能会很棘手。 Refer to the link in einpoklum's answer. 请参阅einpoklum答案中的链接。

If you are working with the newer versions of C++ you can change these lines of code: 如果使用的是较新版本的C ++,则可以更改以下代码行:

typedef std::map<unsigned, Pair>::iterator Iter;
Iter it = myClass.myMap_.begin();

into this: 到这个:

auto it = myClass.myMap_.begin();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM