C++find_if (solve the problem of comparing pointers contained in the found object)

Background:
When doing course design, I encountered a problem that I need to find the type of vector < File * > and the name of a File is to be found. Then it was dealt with for a while. Finally found it. Record it

  • find_if the searched object contains pointers, it needs to be compared

  • find is more inclined to ordinary numerical value or character comparison

  • Both can be applied to custom classes, just overload the = = carrier in the class

  • Also note that it may be used with affine functions.

The so-called imitation function is an encapsulation of the function pointer of C language by C + +. That is, overloading the function caller "()". The function caller () simply means that instead of the function pointer, it calls the defining function of the corresponding overloaded (), and the () operator can only be overloaded in the class

Look at the scene:

If you are simply looking for a basic data type of an object, you can find it directly.

int main()
{
	vector<int>v;
	v.push_back(1);
	v.push_back(2);
	
	auto it=find(v.begin(),v,end(),2);
	if(it==v.end()){
			cout<<"not find"<<endl;
	}
	else{
			cout<<*it<<endl;
	}
}

Now, for example, if I store an object in the Vector container, I need to overload operator = = to tell the compiler to find the rule of object equality.

struct Person{
	Person(){}
	Person(int _name,int _age)
	:name(_name),
	age(_age)
	{	}
	bool operator==(const Person& p) const{
		return name==p.name;
	}
	int name;
	int age;
}
int main()
{
	vector<int>v;
	v.push_back(Person("Zhang San",18));
	v.push_back(Person("Li Si",19));
	
	Person cp_to_find; // Object to find
	
	cp_to_find.name="Zhang San";
	
	auto it=find(v.begin(),v,end(),cp_to_find);
	
	if(it==v.end()){
			cout<<"not find"<<endl;
	}
	else{
			cout<<*it<<endl;
	}
}

Now there is a vector < file * > of this type pointer in my class. How to find it?

Each element in this vector is a pointer to an object. We need to check it in this vector < >
Find the element with the specified file name. If it is found, you will get the pointer of the object.

Not only do you need to compare the equality of types, but the values corresponding to the pointers of two objects.

You need to use find here_ if.

We need to use find_ If function, and specify the predicate function itself (that is, the third parameter of the find_if function, please refer to the STL manual). Look at find first_ Definition of if function:
template<class InputIterator, class Predicate>

InputIterator find_if(InputIterator _First, InputIterator _Last, Predicate _Pred);
Parameters

_First
An input iterator addressing the position of the first element in the range to be searched.

_Last
    An input iterator addressing the position one past the final element in the range to be searched.
    
_Pred
    User-defined predicate function object that defines the condition to be satisfied by the element being searched for. A predicate takes single argument and returns true or false.

And here we need to use imitation functions.

We define the structure outside the File class, and carry out function overloading inside, which is more specific to the element pointed to by the pointer.

typedef struct finder_t
{
	finder_t(string _name)
		:name(_name)
	{}
	//Overloaded () --- > CPP's imitation function replaces the function pointer
	bool operator()(File* f)
	{
		return (name == f->File_name);
	}

	string name;
}finder_t;

Then you can use find_if function to find:

struct File {
	File() {}

	File(const string& _File_name , string _path="" , File* _parent=nullptr, vector<File*> _File_context=vector<File*>() )
		:File_name(_File_name),
		path(_path),
		parent(_parent),
		File_context(_File_context)
	{}

	char File_type;//file type
	string File_owner;//File owner
	string File_ownergroup;//File owning group
	unsigned size;//file size
	string _time;//Creation time
	string File_name;//file name
	string contain;//File content

	File* parent;//Parent file (directory)
	vector<File*> File_context;//List of files (directories)
	string path;//File path
};

typedef struct finder_t
{
	finder_t(string _name)
		:name(_name)
	{}
	//Overloaded () --- > CPP's imitation function replaces the function pointer
	bool operator()(File* f)
	{
		return (name == f->File_name);
	}

	string name;
}finder_t;

Tags: C++

Posted on Mon, 29 Nov 2021 09:47:53 -0500 by evan18h