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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. How to search elements in vector

How to search elements in vector

Scheduled Pinned Locked Moved ATL / WTL / STL
graphicstutorialquestion
3 Posts 2 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.
  • K Offline
    K Offline
    krishnakumartm
    wrote on last edited by
    #1

    Hi, I have a structure, i am keeping the structure in vector to store multiple elements, my structure looks like struct mystruct { id = 1; pid= 2; } i used vector to store like vector vectStruct; like this i have 100 elements, i need to find the elements whose pid = 2 How can i search structures in vector, how to iterate through result. Thanks in advance

    ---------------------------- KRISHNA KUMAR T M

    M 1 Reply Last reply
    0
    • K krishnakumartm

      Hi, I have a structure, i am keeping the structure in vector to store multiple elements, my structure looks like struct mystruct { id = 1; pid= 2; } i used vector to store like vector vectStruct; like this i have 100 elements, i need to find the elements whose pid = 2 How can i search structures in vector, how to iterate through result. Thanks in advance

      ---------------------------- KRISHNA KUMAR T M

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      Use a functor for that:

      struct find_pid
      {
      find_pid ( int pid ) : m_pid_to_find(pid) { }
      bool operator() ( const mystruct& s ) const { return m_pid_to_find == s.pid; }
      protected:
      int m_pid_to_find;
      };

      vector<mystruct>::iterator i = std::find_if ( vectStruct.begin(), vectStruct.end(), find_pid(2) );

      --Mike-- Visual C++ MVP :cool: LINKS~! CP SearchBar v3.0 | C++ Forum FAQ "That's what's great about doing user interface work. No matter what you do, people will say that what you did was idiotic." -- Raymond Chen

      K 1 Reply Last reply
      0
      • M Michael Dunn

        Use a functor for that:

        struct find_pid
        {
        find_pid ( int pid ) : m_pid_to_find(pid) { }
        bool operator() ( const mystruct& s ) const { return m_pid_to_find == s.pid; }
        protected:
        int m_pid_to_find;
        };

        vector<mystruct>::iterator i = std::find_if ( vectStruct.begin(), vectStruct.end(), find_pid(2) );

        --Mike-- Visual C++ MVP :cool: LINKS~! CP SearchBar v3.0 | C++ Forum FAQ "That's what's great about doing user interface work. No matter what you do, people will say that what you did was idiotic." -- Raymond Chen

        K Offline
        K Offline
        krishnakumartm
        wrote on last edited by
        #3

        Hi, Thanks for your replay, i tried what you explained in ur replay but when i pass integer to find_pid(4) but it is giving error like cannot convert from int to mystruct& ---------------------------- KRISHNA KUMAR T M

        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