I have created an iterator for a vector and i need to create a return type for an object found in the vector.

vector<Student>::iterator StudentRecord::find(const string& studentName){
   vector<Student>::iterator find;
   for(find = students.begin(); find != students.end(); find++) {
      if(find->getName() == studentName){
         return find;                 
// Not sure if i am allowed to return find or if i need to specify where abouts in the vector it is located.
      }
      else { 
      cout << "Message: student studentName is not found " << endl;
      }
   }
}

And if i am to call this function in another function can i use:

Found = vector<Student>::iterator StudentRecord::find(const string& studentName);

where found is an object type or is it a string type? Im not sure.

Recommended Answers

All 2 Replies

returning iterator is perfectly valid C++ code. but if you need an object you can derefer the iterator. but the thing is that how will you identify where the object is found or not ( either by returning "" --> empty string) . The semantics of iterator define not found if it returned end ().
Again its upto to you can define the function returning bool stating found / not found. or integer returning -1 if not found and valid index if the string is found in the vector (it defines random access iterator).

StudentRecord r;
vector<string>::iterator found = r.find ("LaiqAhmed");

if (found != r.End ())
{
     // process the found record 
     // string ob = (*found); 
}

// terminates successfully.

// now lets code the End () member function.

vector<string>::iterator End () const
{
     return students.end ();
}

or try returning the index.

int StudentRecord::find(const string& studentName) {
     for (size_t i=0; i<students.size (); ++i)
            if (students[i].compare (studentName) == 0)
                  return i;

          return -1;     // not found
}

The best solutions is using the STL find Algorithm. make the iterator class for your students name, and provide one predicate function. (intermediate solution).

Hope the above things help.

I have decided to use the folling function and will return find.

vector<Student>::iterator StudentRecord::find(const string& studentName){
   vector<Student>::iterator find;
   for(find = students.begin(); find != students.end(); find++) {
      if(find->getName() == studentName){
	     return find; 
	  }
	  else { 
	  cout << "Message: student studentName is not found " << endl;
	  }
   }
}

I now wish to call the function and update the found object with a new object. The compiler has an error stating no match for operator== in newStudentInfo == Found.

bool StudentRecord::update(const Student& newStudentInfo){
   string Name = newStudentInfo.getName();
   vector<Student>::iterator Found = StudentRecord::find(Name);
   if (newStudentInfo == Found) {      //can't compare these as they are different types
   //   students.erase();
	//  students.insert() 
	//  return true;
   }
   else {
      return false;
   }
}

Is it because find in the first function is returned as a wrong type or do i need to change found type so they can be compared with the == operator.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.