Returning C ++ Iterators

I have a function that returns an iterator if an object is found.

Now I have a problem. How to fix the problem of informing the object that called this function so that the object was not found?

vector<obj>::iterator Find(int id, int test)
{
        vector<obj>::iterator it;
            aClass class;

            for(it = class.vecCont.begin(); it != class.vecCont.end(); ++it)
            {
               if(found object) //currently in psuedo code
               return it;
            }

            return ???? // <<< if not found what to insert here?

}

Do I need to change the data structure instead?

Thanks in advance!:)

+5
source share
7 answers

Do not return the iterator to a hidden container. Just return what you want, namely the means of accessing the object, if it exists. In this example, I store objects in a container using a pointer. If your objects exist only temporarily, then a new one and copy the object!

class AClass;

//...some time later
std::vector<AClass*> vecCont; //notice, store pointers in this example!

//..some time later
AClass * findAClass(int id, int test)
{
  vector<AClass*>::iterator it;

  for(it = class.vecCont.begin(); it != class.vecCont.end(); ++it)
  {
     if(found object) //currently in psuedo code
     return it;
  }

  return NULL;
}

//later still..

AClass *foundVal = findAClass(1, 0);
if(foundVal)
{
  //we found it!
}
else
{
  //we didn't find it
}

edit: - std . , .

0

vector::end(), - ,

, Find. <algorithm>. psudocode , , std::find std::find_if. find_if , operator==. lambda [++ 11] ++ 11 , .

, :

#include <cstdlib>
#include <string>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;

class Person
{
public:
    Person(const string& name, unsigned age) : name_(name), age_(age) {};

    string name_;
    unsigned age_;
};

class match_name : public unary_function <bool, string>
{
public:
  match_name(const string& rhs) : name_(rhs) {};
  bool operator()(const Person& rhs) const
  {
    return rhs.name_ == name_;
  }
private:
    string name_;
};

#include <iostream>

int main()
{
    vector<Person> people;
    people.push_back(Person("Hellen Keller", 99));
    people.push_back(Person("John Doe", 42));

    /** C++03 **/
    vector<Person>::const_iterator found_person = std::find_if( people.begin(), people.end(), match_name("John Doe"));

    if( found_person == people.end() )
        cout << "Not FOund";
    else
        cout << found_person->name_ << " is " << found_person->age_;
}

found_person , "John Doe", people_.end(), .

++ 11 lambda - , / . :

string target = "John Doe";
vector<Person>::const_iterator found_person = std::find_if(people.begin(), people.end(), [&target](const Person& test) { return it->name_ == target; });
+7

, .. return class.vecCont.end(), .

+5

?

: -

vector<obj>::iterator Find(int id, int test)
{
   vector<obj>::iterator it;
   aClass class;

   for(it = class.vecCont.begin(); it != class.vecCont.end(); ++it)
   {
     if(found object) //currently in psuedo code
       break;
   }

   return it;
}

std::find.

+2

class.vecCont.end(), . @chris - , std::find.

+1

-

std::vector<obj>::iterator pos;
pos = find(coll.begin(),coll.end(), val);

if (pos != coll.end()) 
+1

You cannot emulate functions std::algorithminside a class. They are free functions for some reason. Usually this is enough to show begin, and enda member function that returns the correct iterators (and perhaps boost::iterator_range). If you need to make a fancy find with a functor, expose the functor as well.

0
source

All Articles