Find Algorithm [STL]
-
Please consider this example:
#include <vector>
#include <list>
#include <algorithm>
using namespace std;
...
list <int> nums;
list <int> ::iterator nums_iter;nums.push_back (3);
nums.push_back (7);
nums.push_front (10);nums_iter = find(nums.begin(), nums.end(), 3); // Search the list.
if (nums_iter != nums.end())
{
cout << "Number " << (*nums_iter) << " found." << endl; // 3
}
else
{
cout << "Number not found." << endl;} Here the elements are of type "int". So we can directly make a "find". But what if the pushed elements are objects? or struct variables? for example:
typedef struct myStruct { int x; int y; }myStruct_t myStruct_t mySObj; mySObj.x=10;mySObj.y=20; list <myStruct_t> lst_mySObj; //Push a lot of mySobj into lst_mySObj list. list <myStruct_t>::iterator mySObj_iter;
How do I do the rest of the rest of the things? Like this :??? mySObj_iter = find(lst_mySObj.begin(), lst_mySObj.end(),mySObj.x ); Not sure :~*
-
Please consider this example:
#include <vector>
#include <list>
#include <algorithm>
using namespace std;
...
list <int> nums;
list <int> ::iterator nums_iter;nums.push_back (3);
nums.push_back (7);
nums.push_front (10);nums_iter = find(nums.begin(), nums.end(), 3); // Search the list.
if (nums_iter != nums.end())
{
cout << "Number " << (*nums_iter) << " found." << endl; // 3
}
else
{
cout << "Number not found." << endl;} Here the elements are of type "int". So we can directly make a "find". But what if the pushed elements are objects? or struct variables? for example:
typedef struct myStruct { int x; int y; }myStruct_t myStruct_t mySObj; mySObj.x=10;mySObj.y=20; list <myStruct_t> lst_mySObj; //Push a lot of mySobj into lst_mySObj list. list <myStruct_t>::iterator mySObj_iter;
How do I do the rest of the rest of the things? Like this :??? mySObj_iter = find(lst_mySObj.begin(), lst_mySObj.end(),mySObj.x ); Not sure :~*
Astricks wrote:
typedef struct myStruct { int x; int y; }myStruct_t
In this case,class/struct should have
operator ==
defined. Which takes care of rest of the things. Let me modify your code like this,typedef struct myStruct
{
int x;
int y;
bool operator == (const myStruct rhs)
{
return ((x==rhs.x) && (y== rhs.y));
}
}myStruct_t ;Now your code work with this structure.
Prasad Notifier using ATL | Operator new[],delete[][^]
-
Astricks wrote:
typedef struct myStruct { int x; int y; }myStruct_t
In this case,class/struct should have
operator ==
defined. Which takes care of rest of the things. Let me modify your code like this,typedef struct myStruct
{
int x;
int y;
bool operator == (const myStruct rhs)
{
return ((x==rhs.x) && (y== rhs.y));
}
}myStruct_t ;Now your code work with this structure.
Prasad Notifier using ATL | Operator new[],delete[][^]
Nice, it compiles now. Thanks. Now, the "find" doesn't work ! anyway thanks I've moved a step up. :)
list <myStruct_t> lst_mySObj; for(int i=1;i<=20;i++) { myStruct_t mySObj; mySObj.x =i; mySObj.y=i; } myStruct_t mySObjFind; mySObjFind.x =5; mySObjFind.y=5; list::iterator mySObj_iter; ; ; mySObj_iter = find(lst_mySObj.begin(), lst_mySObj.end(),mySObjFind ); ; // printf("\nHere%d",(*mySObj_iter).x);//iCrash! ; ; /*if (mySObj_iter != lst_mySObj.end()) { printf("\nHere%d",(*mySObj_iter).x); Never enters here.. }*/
*
-
Nice, it compiles now. Thanks. Now, the "find" doesn't work ! anyway thanks I've moved a step up. :)
list <myStruct_t> lst_mySObj; for(int i=1;i<=20;i++) { myStruct_t mySObj; mySObj.x =i; mySObj.y=i; } myStruct_t mySObjFind; mySObjFind.x =5; mySObjFind.y=5; list::iterator mySObj_iter; ; ; mySObj_iter = find(lst_mySObj.begin(), lst_mySObj.end(),mySObjFind ); ; // printf("\nHere%d",(*mySObj_iter).x);//iCrash! ; ; /*if (mySObj_iter != lst_mySObj.end()) { printf("\nHere%d",(*mySObj_iter).x); Never enters here.. }*/
*
How have you defined the operator == on your struct?
-
How have you defined the operator == on your struct?
-
Yes:
typedef struct myStruct
{
int x;
int y;
bool operator == (const myStruct rhs)
{
printf("\n%d,%d-%d,%d",rhs.x,rhs.y,x,y);return ((x==rhs.x) && (y== rhs.y));
}
}myStruct_t;*
It looks ok, but normally we would not pass a copy, try changin it to this
bool operator == (const myStruct**&** rhs)
not that it should make a difference. -
It looks ok, but normally we would not pass a copy, try changin it to this
bool operator == (const myStruct**&** rhs)
not that it should make a difference. -
Nice, it compiles now. Thanks. Now, the "find" doesn't work ! anyway thanks I've moved a step up. :)
list <myStruct_t> lst_mySObj; for(int i=1;i<=20;i++) { myStruct_t mySObj; mySObj.x =i; mySObj.y=i; } myStruct_t mySObjFind; mySObjFind.x =5; mySObjFind.y=5; list::iterator mySObj_iter; ; ; mySObj_iter = find(lst_mySObj.begin(), lst_mySObj.end(),mySObjFind ); ; // printf("\nHere%d",(*mySObj_iter).x);//iCrash! ; ; /*if (mySObj_iter != lst_mySObj.end()) { printf("\nHere%d",(*mySObj_iter).x); Never enters here.. }*/
*
I think you forgot to add the object to list, Following code works perfectly for me,
typedef struct myStruct
{
int x;
int y;
bool operator == (const myStruct rhs)
{
return ((x==rhs.x) && (y== rhs.y));
}
}myStruct_t ;int main()
{
list<myStruct_t> nums;
list<myStruct_t>::iterator nums_iter;
for(int i=1;i<=20;i++)
{
myStruct_t mySObj;
mySObj.x =i;
mySObj.y=i;
nums.push_back(mySObj);
}
myStruct_t mySObj;
mySObj.x =5;
mySObj.y=5;
nums_iter = find(nums.begin(), nums.end(), mySObj);
printf("\nHere%d\n",(*nums_iter).x);
// Search the list.
if (nums_iter != nums.end()){
cout << "Number " << "a" << " found." << endl;
// 3
}else
{ cout << "Number not found." << endl;}}
Prasad Notifier using ATL | Operator new[],delete[][^]
-
I think you forgot to add the object to list, Following code works perfectly for me,
typedef struct myStruct
{
int x;
int y;
bool operator == (const myStruct rhs)
{
return ((x==rhs.x) && (y== rhs.y));
}
}myStruct_t ;int main()
{
list<myStruct_t> nums;
list<myStruct_t>::iterator nums_iter;
for(int i=1;i<=20;i++)
{
myStruct_t mySObj;
mySObj.x =i;
mySObj.y=i;
nums.push_back(mySObj);
}
myStruct_t mySObj;
mySObj.x =5;
mySObj.y=5;
nums_iter = find(nums.begin(), nums.end(), mySObj);
printf("\nHere%d\n",(*nums_iter).x);
// Search the list.
if (nums_iter != nums.end()){
cout << "Number " << "a" << " found." << endl;
// 3
}else
{ cout << "Number not found." << endl;}}
Prasad Notifier using ATL | Operator new[],delete[][^]
-
OMG! That's it. I've missed it !:doh: I've commented the line for no reason X| . Thanks a lot prasad_som. :jig:
*
Often the biggest headaches come from the simplest of mistakes