CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2003
    Posts
    615

    std::vector filtering

    The example enable a client to iterate the internal std::vector using being() and end().
    Code:
    class foo
    {
    public:
      typedef std::vector<std::string>const_iterator iter;
      iter begin () const;
      iter end () const;
    
    private:
      std::vector<std::string> m_Collecton;
    };
    In the future I see the need for this class to be able to control sequence (sorting) and also show a subset of the complete list based on a search parameter.

    Using std::sort appear to solve the ability to sort the collection.

    How can I return an iterator to the client which only iterates a sub-set of all items in the std::vector?

    An example would be, I add this method to the class;
    Code:
    void find(const std::string& st);
    So if the client performs (below) only items in std::vector that contains the character "a" should be possible to iterate.
    Code:
    foo f;
    f.search("a");
    One option would be to operate with two collection inside the foo class. One more static containing all items and the other containing the sorted and filtered items. This would lead to some copying but should work. Far from perfect. Any other ideas?
    Before post, make an effort yourself, try googling or search here.

    When posting, give a proper description of your problem, include code* and error messages.

    *All code should include code tags

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: std::vector filtering

    Quote Originally Posted by laasunde View Post
    Using std::sort appear to solve the ability to sort the collection.

    How can I return an iterator to the client which only iterates a sub-set of all items in the std::vector?
    Have a look at boost::filter_iterator. If the data is sorted, you can also use std::equal_range.
    Quote Originally Posted by laasunde View Post
    An example would be, I add this method to the class;
    Code:
    void find(const std::string& st);
    So if the client performs (below) only items in std::vector that contains the character "a" should be possible to iterate.
    Code:
    foo f;
    f.search("a");
    One option would be to operate with two collection inside the foo class. One more static containing all items and the other containing the sorted and filtered items. This would lead to some copying but should work. Far from perfect. Any other ideas?
    Instead of adding member functions for each algorithm, why not just reuse the STL algorithms. There is a std::find that takes a pair of iterators. From what I can tell, you can already use that with your current code.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Jan 2003
    Posts
    615

    Re: std::vector filtering

    Thank you. Will take a look at what you mentioned.
    Before post, make an effort yourself, try googling or search here.

    When posting, give a proper description of your problem, include code* and error messages.

    *All code should include code tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured