我到处都是google,无法找到如何使用std :: set和控制插入顺序的工作示例。我发现了我的结构类型的注释,但是我收到了编译错误。
我正在尝试对myMaskName进行排序,以便我得到
address class
0x19ec220 TEST1 2 26
0x19eba90 TEST2 100 26
0x19ebb00 TEST3 230 26
0x19eba20 TEST4 240 26
VS。此默认订单
0x19eba20 TEST4 240 26
0x19eba90 TEST2 100 26
0x19ebb00 TEST3 230 26
0x19ec220 TEST1 2 26
这是我的测试代码:
class FoIxCompareMaskNew
{
public:
// Main constructor.
FoIxCompareMaskNew (const char *maskName, const int& startAddress, const int& numWords) :
myMaskName(maskName),
myStartAddress(startAddress),
myNumberWords(numWords)
{
}
~FoIxCompareMaskNew ();
std::string MaskName() {return myMaskName;}
int StartAddress() {return myStartAddress;}
int NumberWords() {return myNumberWords;}
private:
std::string myMaskName;
int myStartAddress;
int myNumberWords;
};
struct FoIxCompareMaskNewComp {
bool operator()(const FoIxCompareMaskNew* p_lhs, const FoIxCompareMaskNew* p_rhs) const
{
std::string temp = p_lhs->MaskName();
return temp.compare(p_rhs->MaskName());
}
};
void newWay()
{
std::multiset<FoIxCompareMaskNew *, FoIxCompareMaskNewComp> maskList;
std::multiset<FoIxCompareMaskNew *, FoIxCompareMaskNewComp> maskList2;
FoIxCompareMaskNew * list[4];
list[0] = new FoIxCompareMaskNew("TEST1", 2, 26);
list[2] = new FoIxCompareMaskNew("TEST3", 230, 26);
list[1] = new FoIxCompareMaskNew("TEST2", 100, 26);
list[3] = new FoIxCompareMaskNew("TEST4", 240, 26);
maskList.insert(list[0] );
maskList.insert(list[1] );
maskList2.insert(list[0] );
maskList2.insert(list[1] );
maskList2.insert(list[2] );
maskList2.insert(list[3] );
}
int main(int, char**)
{
newWay();
}
这是我在编译时看到的错误。我不喜欢访问会员。
./main.C: In member function ‘bool FoIxCompareMaskNewComp::operator()(const FoIxCompareMaskNew*, const FoIxCompareMaskNew*) const’:
./main.C:205:38: error: passing ‘const FoIxCompareMaskNew’ as ‘this’ argument of ‘std::string FoIxCompareMaskNew::MaskName()’ discards qualifiers [-fpermissive]
std::string temp = p_lhs->MaskName();
^
./main.C:206:39: error: passing ‘const FoIxCompareMaskNew’ as ‘this’ argument of ‘std::string FoIxCompareMaskNew::MaskName()’ discards qualifiers [-fpermissive]
return temp.compare(p_rhs->MaskName());
我创建的所有示例都是针对int或std :: string的。我没有找到使用类似这样的类的例子,除了设置比较结构并使用它。
那么我做错了什么?
这是指针的更正代码:
class FoIxCompareMaskNew
{
public:
// Main constructor.
FoIxCompareMaskNew (const char *maskName, const int& startAddress, const int& numWords) :
myMaskName(maskName),
myStartAddress(startAddress),
myNumberWords(numWords)
{
}
~FoIxCompareMaskNew ();
std::string MaskName() const {return myMaskName;}
int StartAddress() const {return myStartAddress;}
int NumberWords() const {return myNumberWords;}
private:
std::string myMaskName;
int myStartAddress;
int myNumberWords;
};
struct FoIxCompareMaskNewComp {
bool operator()(const FoIxCompareMaskNew* p_lhs, const FoIxCompareMaskNew* p_rhs) const
{
return (p_lhs->MaskName().compare(p_rhs->MaskName()) < 0);
}
};
void newWay()
{
std::multiset<FoIxCompareMaskNew *, FoIxCompareMaskNewComp> maskList;
std::multiset<FoIxCompareMaskNew *, FoIxCompareMaskNewComp> maskList2;
FoIxCompareMaskNew * list[4];
list[0] = new FoIxCompareMaskNew("TEST1", 2, 26);
list[2] = new FoIxCompareMaskNew("TEST3", 230, 26);
list[1] = new FoIxCompareMaskNew("TEST2", 100, 26);
list[3] = new FoIxCompareMaskNew("TEST4", 240, 26);
for (int i = 0; i < 4; i++)
{
std::cout << "list[" << i << "] " << list[i] << std::endl;
}
maskList.insert(list[0] );
maskList.insert(list[1] );
maskList2.insert(list[0] );
maskList2.insert(list[1] );
maskList2.insert(list[2] );
maskList2.insert(list[3] );
for (auto it=maskList2.begin(); it!=maskList2.end(); ++it)
{
// Check first to see if the item in in the list and if so delete it.
maskList.erase(*it);
maskList.insert(*it); // now insert the unique pointer
}
std::cout << std::endl << "Unique set of masks using erase check" << std::endl;
for (auto it=maskList.begin(); it!=maskList.end(); ++it)
{
FoIxCompareMaskNew * node = (FoIxCompareMaskNew *)(*it);
std::cout << node << " " << node->MaskName() << " " << node->StartAddress() << " " << node->NumberWords() << std::endl;
}
std::cout << std::endl;
}
int main(int, char**)
{
std::cout << std::endl << "unique std::set list of pointers" << std::endl;
newWay2();
}
将返回值更改为const修复了使用struct方式进行指针声明的编译问题(请参阅上面的更正代码),但我也尝试使用inline与struct进行:
inline bool operator< (const FoIxCompareMaskNew* p_lhs, const FoIxCompareMaskNew* p_rhs)
{
return (p_lhs->MaskName().compare(p_rhs->MaskName()) < 0);
}
从“std :: set maskList”声明行中删除FoIxCompareMaskNewComp后,我得到了这个编译错误。
错误:'bool运算符&lt;(const FoIxCompareMaskNew *,const FoIxCompareMaskNew *)'必须具有类或枚举类型的参数 内联布尔运算符&lt; (const FoIxCompareMaskNew * p_lhs,const FoIxCompareMaskNew * p_rhs)
如果我不使用指针,那么下面的内联工作,内联是:
inline bool operator< (const FoIxCompareMaskNew lhs, const FoIxCompareMaskNew rhs)
{
return (lhs.MaskName().compare(rhs.MaskName()) < 0);
}
std::set<FoIxCompareMaskNew> maskList
那么,如果我将其声明为指针,但为什么不指示呢?为什么它不起作用呢?