這是我的迭代器位置代碼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’
Did you include?什麼是id的類型? –
billz
2013-02-17 08:33:47
@billz我確實包括,id是一個整數,它是唯一的,不同的,只在整個向量中出現一次。它按升序排列 –
user2017011
2013-02-17 08:34:19
這個問題看起來很像[我怎樣才能通過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