[英]iterate over a vector thats defined as a typedef C++
我有一個結構如下
struct pins
{
PType pintypes;
GType Grouptypes
};
其中PType和GType被定義為
typedef std::vector< pInfo > PType;
typedef std::vector<gInfo> GType;
其中pInfo和gInfo是類,其樣本成員為
struct pInfo {
std::string pinNumber;
std::string pinName;
};
現在我要比較pins變量中的兩個PinInfos。我想看看是否給定了兩個pins,它們具有完全相同的pintype。
我正在嘗試如下。
for (std::vector<pInfo>::const_iterator prim1Pin = pins.pinTypes.begin(); prim1Pin != pins.pinTypes.end(); prim1Pin++)
{
if (bRet)
{
std::vector<pInfo>::const_iterator prim2pin = std::find(prim2.pinTypes.begin(), prim2.pinTypes.end(), (*prim1Pin).pinNumber);
if (prim2pin!=prim2.pinTypes.end())
{
bRet &= ((*prim2pin).PinNumber== (*prim1Pin).PinNumber&& (*prim2pin).pinName == (*prim1Pin).pinName)
}
else
bRet &= false;
}
}
編譯此文件時,在algorithm.h
文件中出現以下錯誤
Error C2678 binary '==' : no operator found which takes a left-hand operand of type 'pInfo' (or there is no acceptable conversion) C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\VC\\include\\algorithm 41
不知道這怎么了。 我使用的版本低於C ++ 11。
在對find
的調用中,您正在搜索包含string
的容器時,該容器包含一堆pinType
對象。 編譯器將需要operator==
的重載,該重載的左側為pinType
,右側為string
。
因此,您將需要提供該操作,或者定義一個lambda或比較類來處理它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.