-
November 26th, 2012, 08:03 AM
#1
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
-
November 26th, 2012, 08:23 AM
#2
Re: std::vector filtering
Originally Posted by laasunde
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.
Originally Posted by laasunde
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
-
November 26th, 2012, 09:39 AM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|