Rgd:获取索引位置的向量迭代器 [英] Rgd: Vector Iterator to get Index Position

查看:104
本文介绍了Rgd:获取索引位置的向量迭代器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的迭代器位置代码

This is my iterator position code

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。

I got about 100 objects, i am trying to find the index/element/position of e.g nodeid "80" assuming that my object is all sorted in ascending order by nodeid.

我关心的是速度和内存使用,我以前使用

my concern is speed and memory usage, i was previously using

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

现在我试图使用和迭代器,我听说它更快..任何建议,得到它修复或有一个更好的方式来找到我的向量索引的值nodeid

but now i am trying to use and iterator, i heard its faster.. any suggestion on getting it fix or is there a better way to find my vector index by its value "nodeid"

我知道地图是一个很好的std容器为我的情况,但ia位运行时间做的更改,所以我不得不坚持使用向量..

i know map is a good std container for my case but i a bit run out of time to do the changes so i got to stick with vector..

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

当我尝试编译上述迭代器行时出错。

Error output when i try compile the iterator line above.

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’


推荐答案

矢量的对象。每个对象包含一个int。你试图查找该向量中的对象,该对象在该int中有一个给定的值。但编译器不理解这一点,因为STL仅描述如何在容器中查找值。而它怎么会不然?

You have a vector of objects. Each object contains an int. You're trying to "find" the object in that vector which has a given value in that int. But the compiler doesn't understand this, because the STL only describes how to find values in containers. And how could it be otherwise? If you had an object containing two ints, which one would be compared?

因为你说使用 std :: find()

Since you said the use of std::find() was for better performance than the old-school for-loop, you can stop trying now and just go back to that. The performance will be basically the same either way, and you said already that you're out of time. So just use what you had working, because it's not a performance problem.

如果你坚持使用迭代器,你可以使用 std :: find_if ()使用您定义的自定义谓词,如下所示:

If you insist on using iterators, you could use std::find_if() with a custom predicate you'd define, like this:

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找到我们感兴趣的元素,而不创建要搜索的临时节点。

This way, we have provided enough information to let the STL find the element we're interested in, without creating a temporary node to search for.

这篇关于Rgd:获取索引位置的向量迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆