具有字符串成员的结构的集合与向量

set vs. vector of structs with string member

本文关键字:集合 向量 结构 字符串 成员      更新时间:2023-10-16

对于以下定义:

struct Operand
    {
        Operand(std::string opName,bool isInput,bool isOutput,bool isReg) : m_opName(opName),m_isInput(isInput),m_isOutput(isOutput),m_isReg(isReg) {}
        std::string m_opName;
        bool m_isInput;
        bool m_isOutput;
        bool m_isReg;
    };
typedef std::set<Operand> SensitivityList;
SensitivityList m_sensitivityList;

应执行以下循环:

for (SensitivityList::iterator it = m_sensitivityList.begin();it != m_sensitivityList.end();++it)
    {
        AddToInterfaceList(it->m_opName,portList,portList,false);
}

AddToInterfaceList的签名为:

static void AddToInterfaceList(std::string& data,std::string& interfaceList32Bit,std::string& interfaceList1Bit);

上述代码的编译失败,错误为:

'AddToInterfaceList' : cannot convert parameter 1 from 'const std::string' to 'std::string &'

如果我将SensitivityList重新定义为:

typedef std::vector<Operand> SensitivityList;

编译成功。集合与向量的问题是什么?如何修复?感谢

std::set上的迭代程序只能返回集合中项的const引用。如果你可以获得一个对集合中某个项的非常数引用,你可以更改该项的值,这可能会使该集合不再是一个合适的集合。如果将AddToInterfaceList的第一个参数更改为conststd::string&数据应该编译得很好。