Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7413469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:54:58+00:00 2026-05-29T06:54:58+00:00

I have a custom read-only data structure that I need to transverse. I would

  • 0

I have a custom read-only data structure that I need to transverse. I would like to create a custom iterator that need to skip certain values.

A simple but equivalent example would be the following. I have a vector of numbers and I want to loop through all skipping the negative values. Normally I would do something like:

vector<int> v;
for (vector<int>::iterator it = v.begin(); it!=v.end(); ++it) {
    if (*it > 0) {
       dosomething(*it);
    }
}

But I would like to do something like:

vector<int> v;
for (vector<int>::my_iterator it = v.my_begin(); it!=v.my_end(); ++it) {
    dosomething(*it);
}

What is the right way to achieve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:54:59+00:00Added an answer on May 29, 2026 at 6:54 am

    This is easily achieved with a boost::filter_iterator, if your data structure already stores a container under the hood. Here’s a simple example:

    #include <vector>
    #include <iostream>
    #include <boost/iterator/filter_iterator.hpp>
    
    class X{
        typedef std::vector<int> container;
        struct Pred{
            bool operator()(int i){
                return i % 2 == 0;
            }
        };
    
    public:
        typedef boost::filter_iterator<Pred, container::iterator> iterator;
    
        void add(int val){ nums.push_back(val); }
        iterator begin(){ return iterator(nums.begin(), nums.end()); }
        iterator end(){ return iterator(nums.end(), nums.end()); }
    
    private:
        container nums;
    };
    
    int main(){
        X x;
        for(int i=0; i < 10; ++i)
            x.add(i);
        for(X::iterator it = x.begin(), ite = x.end(); it != ite; ++it)
            std::cout << *it << ' ';
    }
    

    Live example at Ideone. Output:

    0 2 4 6 8

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few models that need to have custom find conditions placed on
My iPhone app will have read-only system data AND read/write user data (stored either
I have a set of custom data types that can be used to manipulate
I wrote a custom XML reader because I needed something that would not read
I have a custom class that implements ICollection , and this class is readonly,
I have read several documentations about building custom ErrorHandler (by inheriting from IErrorHandler). Unfortunately,
I have custom classes that I currently instantiate within App.xaml as resources. I want
I have created a list using Custom List in Data Sheet View . After
I am using piston and I would like to spit out a custom format
I am using ExtJS 3.4 . I have a structure with data for combobox

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.