我仍在学习 C++ 的诀窍。
我想将向量的某些元素移动到另一个向量(基于向量是否包含在 unordered_set 中)。
但是,我不断收到错误消息,即向量类没有采用引用变量的插入方法。具体来说,这个错误:
In file included from TorrentTrackerCommManager.h:10:0,
from TorrentTrackerCommManager.cpp:1:
PeerList.h: In member function ‘void PeerList::addPeers(std::vector<Peer*>)’:
PeerList.h:51:24: error: no matching function for call to ‘std::vector<Peer>::insert(Peer&)’
peerList.insert(*p);
我正在使用的代码在这里:
class PeerList {
private:
/* List holding all of the Peer objects. */
std::vector<Peer> peerList;
/* Set holding all of the Peer objects. */
std::unordered_set<Peer, PeerHash> peerSet;
public:
void addPeers(std::vector<Peer *> newPeers) {
std::vector<Peer *>::iterator it;
for (it = newPeers.begin(); it != newPeers.end(); ++it) {
Peer p = **it;
//If we don't already have that peer
if (peerSet.find(p) != peerSet.end()) {
//add him
peerList.insert(p);
peerSet.insert(p);
}
}
}
}