Thread: Searching the contents of a vector

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    6

    Searching the contents of a vector

    How do I perform a search operation on the contents of a vector?

    My vector looks like this (the beginning of it):

    Code:
    int main(void)
    {
    	vector<Transaction> trans; //instantiates a vector called trans
    
    	// sample records in the vector
    	trans.push_back(Transaction(1365,"Norwich","20030901","1200",12,005,"Nobody present"));

    I need to search the vector for each occurence of a date, format it, and then output it to the screen.
    Thanks.

  2. #2
    root
    Join Date
    Sep 2003
    Posts
    232
    Use the find function in the STL, I think it's in <algorithm>.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    161
    You can use the find_if() algorithm with a predicate. Here is some code assuming that the date is stored as a std::string and that Transaction has a member function get_date() that returns the date.

    Predicate:
    Code:
    class DateMatcher
    {
    public:
      DateMatcher(const std::string& str)
      : date(str)
      {}
    
      bool operator()(const Transaction& trans)
      {
        if(trans.get_date() == date) return true;
        return false;
      }
    
    private:
      std::string date;
    };
    Using the algorithm:
    Code:
      std::vector<Transaction> transactions;
      std::vector<Transaction>::iterator found;
    
      found = std::find_if(transactions.begin(), transactions.end(), DateMatcher("20030901"));
    
      if(found == transactions.end())
      {
        std::cerr << "Not found!" << std::endl;
      }
      else
      {
        Transaction& trans = *found;
      }

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    6

    Question

    Originally posted by thefroggy
    You can use the find_if() algorithm with a predicate. Here is some code assuming that the date is stored as a std::string and that Transaction has a member function get_date() that returns the date.

    Predicate:
    Code:
    class DateMatcher
    {
    public:
      DateMatcher(const std::string& str)
      : date(str)
      {}
    
      bool operator()(const Transaction& trans)
      {
        if(trans.get_date() == date) return true;
        return false;
      }
    
    private:
      std::string date;
    };
    Using the algorithm:
    Code:
      std::vector<Transaction> transactions;
      std::vector<Transaction>::iterator found;
    
      found = std::find_if(transactions.begin(), transactions.end(), DateMatcher("20030901"));
    
      if(found == transactions.end())
      {
        std::cerr << "Not found!" << std::endl;
      }
      else
      {
        Transaction& trans = *found;
      }
    Thanks. I have tried this, but I get an error message saying 'get_date': is not a member of Transaction. Any ideas what I'm doing wrong?

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    161
    Here is some code assuming that the date is stored as a std::string and that Transaction has a member function get_date() that returns the date.
    Notice that I assumed a few things when I wrote the code. You'll either have to add a get_date() method to your Transaction class, or change the DateMatcher class to match the date in some other way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Visual C++ 2005 linking and file sizes
    By Rune Hunter in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2005, 10:41 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM