2013-02-17 43 views
0

這是我的迭代器位置代碼RGD:矢量迭代器取得索引位置

struct node { 
int nodeid; 
vector<fingerTable> fTable; 
vector<string> data; 
}; 

vector<node> cNode; 

vector<node>::iterator position = find(cNode.begin(),cNode.end(), id); 

我有大約100個對象,我想找到的如NODEID「80」索引/元/位假設我對象全部按照nodeid升序排列。

我關心的是速度和內存使用情況,我以前使用

for(int i=0;i<cNode.size();i++) 
{ 
//if logic-- match nodeid with the nodeid input.. then assign the i to an integer.. 
} 

,但現在我想使用和迭代器,我聽到它的速度更快..得到它修復或任何建議有更好的方式,通過它的價值「NODEID」

找到我的矢量指數我知道地圖是我的情況良好的性病容器但IA位運行的時間做的修改,所以我一定要堅持使用矢量..

vector<node>::iterator position = find(cNode.begin(),cNode.end(), id); 

錯誤輸出,當我嘗試編譯上面的迭代器行。

In member function ‘void chord::removePeer(int)’: 
testfile.cpp:532:69: error: no matching function for call to ‘chord::find(std::vector<chord::node>::iterator, std::vector<chord::node>::iterator, int&)’ 
testfile.cpp:532:69: note: candidate is: 
testfile.cpp:177:5: note: int chord::find(int, int, bool) 
testfile.cpp:177:5: note: no known conversion for argument 1 from ‘std::vector<chord::node>::iterator {aka __gnu_cxx::__normal_iterator<chord::node*, std::vector<chord::node> >}’ to ‘int’ 
+0

Did you include ?什麼是id的類型? – billz 2013-02-17 08:33:47

+0

@billz我確實包括,id是一個整數,它是唯一的,不同的,只在整個向量中出現一次。它按升序排列 – user2017011 2013-02-17 08:34:19

+0

這個問題看起來很像[我怎樣才能通過C++中的數據獲取向量索引?](http://stackoverflow.com/q/14914985/78845)從11小時前,[我回復「'std :: find()'以線性時間(O(n))運行,與'for'循環相同。」](http://stackoverflow.com/a/14917460/78845)。如果你想在子線性時間運行,使用'std :: lower_bound()'。 – Johnsyweb 2013-02-17 08:54:53

回答

1

你有對象的載體。每個對象都包含一個int。你試圖「找到」那個在那個int中給定值的向量中的對象。但編譯器不理解這一點,因爲STL只描述瞭如何在容器中查找值。而且它怎麼會是另外的呢?如果你有一個包含兩個整數的對象,哪一個會被比較?

既然你說過使用std::find()比舊式for-loop有更好的性能,那麼現在就可以停止嘗試並回到那個了。性能基本上都是一樣的,而且你已經說你已經沒有時間了。所以只要使用你的工作,因爲這不是一個性能問題。

如果你堅持使用迭代器,你可以使用std::find_if()一個自定義謂詞你定義,像這樣:

struct HasId { 
    HasId(int id) : _id(id) {} 
    bool operator()(node const& n) const { return n.nodeid == _id; } 
private: 
    int _id; 
} 

std::find_if(cNode.begin(), cNode.end(), HasId(id)); 

這樣一來,我們已經提供了足夠的信息來讓STL找到元素,我們'感興趣,而不創建一個臨時節點來搜索。

+0

嗨,與查找如果情況下,我如何得到返回結果作爲矢量[索引]索引是我正在尋找。 – user2017011 2013-02-17 08:51:22

+1

'find_if()'返回一個迭代器。如果它被稱爲'iter',那麼你可以說'* iter'來解引用它並得到與cNode [index]相同的結果。如果你想知道你找到了哪個索引,請執行'iter - cNode.begin()'。 – 2013-02-17 08:55:47

+1

你爲什麼需要索引? – Thomas 2013-02-17 08:55:47

0

cNode是node類型的載體,但你在ID(整型)通過,則需要一個隱式轉換函數id轉換爲node對象:

struct node { 
    int nodeid; 
    vector<fingerTable> fTable; 
    vector<string> data; 

    node(int id) 
    : nodeid(nodeid) 
    { 
    } 
}; 

bool operator==(const node& lhs, const node& rhs) 
{ 
    return lhs.nodeid == rhs.nodeid; 
} 

現在你可以調用標準: :找到整數類型上node向量:

std::vector<node>::iterator position = std::find(cNode.begin(),cNode.end(), id); 

其等於:

std::vector<node>::iterator position = std::find(cNode.begin(),cNode.end(), node(id)); 

用C++ 11,你可以用寫的std ::拉姆達爲find_if另一種方式:

auto pos = std::find_if(cNode.begin(), cNode.end(), 
      [id](const node& n){ return n.nodeid == id; }); 
0

nNode是一個向量,std::find搜索的值不是關鍵。使用類似 std::map<int,node>來查找您的節點。

int id = 0; 

typedef std::map<int,node> NodeMap; 
NodeMap cNode; 

NodeMap::iterator position = cNode.find(id); 

如果你正在做大量的插入/刪除的,並保持東西排序,選擇像地圖或設置適當的容器中。

這基本上是C++ How to speed up my prog design了。

struct node { 
    vector<fingerTable> fTable; 
    vector<string> data; 
}; 

和矢量改變映射

map<int,node> cNode; 

那麼你addPeer真的只有做到這一點:

void chord::addPeer(int id) 
{ 
    std::map<int, node>::iterator 
    pos = cNode.insert(std::make_pair(id, node())).first;; 

    if(pos != cNode.end()) 
    { 
     ++pos; 
     vector<string> data = pos->second.data; 
     pos->second.data.clear(); 
     dataShift(data, fIndex-1); 
    } 
}//end addPeer 

唯一剩下的問題是,如果你改變節點

dataShift做了什麼,它需要的索引?