title: Search a Vector of Classes by a member of the Class
body: <p><strong>Background:</strong></p>
  
  <p>Actual Code: <a href="https://github.com/lmwunder/ElectorateProgram" rel="nofollow">https://github.com/lmwunder/ElectorateProgram</a></p>
  
  <p>I'm writing a basic program that stores election information as part of an assignment for Data Structures. It is supposed to allow users to track candidates and the votes that they've received. Users can add, edit, or remove candidates from the election information list, and the list itself can be saved or loaded. Users can also create new lists.</p>
  
  <p><strong>Data Structures:</strong></p>
  
  <p>Candidates are represented as follows:
  </p>
  
  <pre><code>class candidate
  {
      // Data Members
      private: std::string fullName;
      private: unsigned votes;
      // Function Members
      /* Constructors and Destructors */
      /* Accessors and Mutators */
  };
  </code></pre>
  
  <p>Candidates are further stored in a vector of another class:</p>
  
  <pre><code>class electorateList
  {
      private: bool isValid;
      private: bool isModified;
      private: std::vector&lt;candidate&gt; electorateData;
  }
  </code></pre>
  
  <p><strong>Question:</strong></p>
  
  <p>When the users wants to edit or remove a candidate, I prompt them for the name of the candidate, which is a <code>std::string</code>. However, I don't exactly see how I can search the <code>std::vector</code> of candidates using only the name of the candidate. The most obvious way for me is to instantiate temporary "match" candidate object with the name data provided inside, and use that to compare against all <code>std::vector</code> members.</p>
  
  <p>Ideally, I'm going for something like this:</p>
  
  <pre><code>// Find a candidate by name and return their position in the vector
  std::vector&lt;candidate&gt;::iterator electorateList::find( std::string &amp;name )
  {
      std::vector&lt;candidate&gt;::iterator position = electorateData.end();
      // Binary search over all the vector elements
      // If candidate is matched ( found ), set the iterator to the position in the vector the match is
      /* position = // electorateData.at( whereEver );
      // Else, return the end iterator of the array
      return position;
  }
  </code></pre>