简体   繁体   English

如何使用迭代器查找对象在矢量中的位置?

[英]How to use iterator for finding position of object in vector?

I have method which chec is client objects exists (by its name) and returns its position: 我有哪个方法是客户端对象存在(按其名称)并返回其位置:

   int Program::checkClient(string userName){
      vector<Client> *ptr = &(impl->clients);
      int i;
      for (i = 0; i < ptr->size(); i++){
         if(ptr->at(i).getName() == userName){
            return i;
         }
      }
      return -1;
   }

The problem is that I get warning, because of i < ptr->size() . 问题是由于i < ptr->size() ,我得到警告。 I believe I need to use iterator for this. 我相信我需要为此使用迭代器。 But how to do it correctly, because not only do I need to run through loop, but also return int as a result, of where it was found. 但是如何正确执行操作,因为不仅需要遍历循环,而且还需要返回int所在位置的int值。 Any ideas? 有任何想法吗? i tried doing something like this, but had no luck: 我试图做这样的事情,但没有运气:

   int Program::checkClient(string userName){
      vector<Client> *ptr = &(impl->clients);
      vector<Client>::iterator it;
      for (it.ptr->begin(); it < it.ptr->end(); it++){
         if(ptr->at(it).getName() == userName){
            return it;
         }
      }
      return -1;
   }

I get an error to similar places like this: it.ptr->begin() . 我在类似的地方遇到错误: it.ptr->begin()

You don't need to use iterators; 您不需要使用迭代器。 you haven't mentioned what warning you're getting, but it's probably because you're comparing an int to vector<Client>::size_type , which is, in all likelihood, an unsigned type. 您没有提到收到的警告,但这可能是因为您正在将intvector<Client>::size_type ,后者很可能是无符号类型。 Change the declaration of i to i的声明更改为

std::vector<Client>::size_type i;

Now, you're getting errors in your iterator code because its syntax is incorrect. 现在,迭代器代码中出现错误,因为其语法不正确。 The for loop should look like this: for循环应如下所示:

for(it = ptr->begin(), it != ptr->end(); ++it) { ... }

You can find the index of the item the iterator is currently pointing at using std::distance 您可以使用std::distance找到迭代器当前指向的项目的索引

return std::distance(ptr->begin(), it);

Additionally, as mentioned in the comments above, it'd be better to use std::find_if for this, especially if you have a compiler that supports lambda expressions. 另外,如上面的评论所述,最好使用std::find_if ,特别是如果您的编译器支持lambda表达式。

auto it = std::find_if(impl->clients.begin(),
                       impl->clients.end(),
                       [&]( Client const& c ) {
                          return c.getName() == userName;
                       } );
return (it != impl->clients.end()) ? 
         std::distance(impl->clients.begin(), it) : -1;

Use this: 用这个:

int Program::checkClient(string userName)
{
    vector<Client> * ptr = &(impl->clients);
    vector<Client>::iterator it;
    int count=0; //for counting the position
    for (it=ptr->begin(); it!=ptr->end(); ++it)
    {
        ++count;
        if( (*it).getName() == userName )
        {
            return count;
        }
    }
    return -1;
}

For searching/finding I prefer using while , it's easier to understand. 对于搜索/查找,我更喜欢使用while ,这更容易理解。
You'd rather use const& for your method parameter. 您宁愿使用const&作为方法参数。

So it'd give something like that: 因此,它会给出类似的信息:

int Program::checkClient(string const& userName) const
{
    vector<Client> const& clients = impl->clients;
    vector<Client>::const_iterator it = clients.begin();
    while (it != clients.end() && it->getName() != userName)
        ++it;
    return it != clients.end() ? it - clients.begin() : -1;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM