Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Find Algorithm [STL]

Find Algorithm [STL]

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++graphicsalgorithmsjson
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Astricks
    wrote on last edited by
    #1

    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 :~

    *

    P 1 Reply Last reply
    0
    • A Astricks

      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 :~

      *

      P Offline
      P Offline
      prasad_som
      wrote on last edited by
      #2

      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[][^]

      A 1 Reply Last reply
      0
      • P prasad_som

        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[][^]

        A Offline
        A Offline
        Astricks
        wrote on last edited by
        #3

        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.. }*/

        *

        W P 2 Replies Last reply
        0
        • A Astricks

          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.. }*/

          *

          W Offline
          W Offline
          Waldermort
          wrote on last edited by
          #4

          How have you defined the operator == on your struct?

          A 1 Reply Last reply
          0
          • W Waldermort

            How have you defined the operator == on your struct?

            A Offline
            A Offline
            Astricks
            wrote on last edited by
            #5

            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;

            *

            W 1 Reply Last reply
            0
            • A Astricks

              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;

              *

              W Offline
              W Offline
              Waldermort
              wrote on last edited by
              #6

              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.

              A 1 Reply Last reply
              0
              • W Waldermort

                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.

                A Offline
                A Offline
                Astricks
                wrote on last edited by
                #7

                I changed it to reference type, but still doesn't work :( I tried to print them :

                for (mySObj_iter=lst_mySObj.begin(); mySObj_iter != lst_mySObj.end(); mySObj_iter++)
                {
                printf("\n%d",(*mySObj_iter).x);
                }

                Even this doesn't work:|

                *

                1 Reply Last reply
                0
                • A Astricks

                  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.. }*/

                  *

                  P Offline
                  P Offline
                  prasad_som
                  wrote on last edited by
                  #8

                  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[][^]

                  A 1 Reply Last reply
                  0
                  • P prasad_som

                    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[][^]

                    A Offline
                    A Offline
                    Astricks
                    wrote on last edited by
                    #9

                    OMG! That's it. I've missed it !:doh: I've commented the line for no reason X| . Thanks a lot prasad_som. :jig:

                    *

                    W 1 Reply Last reply
                    0
                    • A Astricks

                      OMG! That's it. I've missed it !:doh: I've commented the line for no reason X| . Thanks a lot prasad_som. :jig:

                      *

                      W Offline
                      W Offline
                      Waldermort
                      wrote on last edited by
                      #10

                      Often the biggest headaches come from the simplest of mistakes

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups