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 9168299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:35:43+00:00 2026-06-17T15:35:43+00:00

I have a class with an standard container member, and I’m wondering is that

  • 0

I have a class with an standard container member, and I’m wondering is that possible that I make an own iterator with a specific route, for example it goes back and forth, and after that stops.

template<class T>
class compressed_string {
    vector<T> v;
public:
    typedef typename std::vector<T>::iterator iterator;
    iterator begin() { return v.begin(); }
    iterator end() { return v.end(); }

    compressed_string& add(const T& elem) {
        v.push_back(elem);
        return *this;
    }
    basic_string<T> not_nice_way_to_make_real_string() {
        basic_string<T> tmp;
        for(iterator i = v.begin(); i < v.end(); ++i)
            tmp += *i;
        for(iterator i = --v.end(); i >= v.begin(); --i)
            tmp += *i;
        return tmp;
    }
};

main:

compressed_string<char> s;
s.add('q').add('w').add('e').add('w');

cout << s.not_nice_way_to_make_real_string(); // q w e w w e w q

cout << endl

for ( compressed_string<char>::iterator i = s.begin(); i < s.end(); ++i )
    cout << *i;

So with this iterator member the output would be the same in this two lines.

How is this possible?

  • 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-06-17T15:35:44+00:00Added an answer on June 17, 2026 at 3:35 pm

    You’ll need an iterator that stores a bit of state:

    • where it is (e.g. an iterator v_it over v)
    • where it’s going (e.g. bool forward)
    • where it’s bounded (e.g. iterators v_begin = v.begin() and v_end = v.end())

    and some otherwise invalid iterator to represent the end, such as {v_end, backward}).

    Then implement the increment operator along the lines of:

    if (forward) {
        if (++v_it == v_end) {
            forward = false;
            --v_it;
        }
    } else {
        if (v_it-- == v_begin) {
            v_it = v_end;
        }
    }
    

    and similarly for decrement, if you want a bidirectional iterator; in which case, it would be polite to provide a reverse_iterator too. You should provide both pre- and post-increment forms.

    You’ll also need == and != comparisons, comparing both v_it and forward, and dereference operators * and -> that dereference v_it, and suitable begin and end functions; for bonus points, a const_iterator would be nice.

    Note that you’ll need random access if you really want the code in your question (i < s.end() rather than the more generic i != s.end()) to work; that’s entirely possible, but rather excessive if you don’t otherwise need it.

    UPDATE: as noted in the comments, this particular implementation could probably be improved a bit; for example, it’s possible to remove the need to store v_begin if you’re a bit careful about how you define the end iterator.

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

Sidebar

Related Questions

I have a class that adapts std::vector to model a container of domain-specific objects.
I have a standard active record model with an attributes that is required: class
My problem: require_once '/includes/aws-sdk-1.5.2/sdk.class.php'; My environment: I have a pretty standard PHP site that
I have class grades that I need to check if a specific grade is
Using Observer pattern. I have a class, called Monitor for example, that is monitoring
I have the following situation: I have class A that contains a stack member
I have a container class for adding some properties to standard data types like
I have a standard ActiveRecord model with the following: class MyModel < ActiveRecord::Base custom_method
I have created an ASP.NET class derived from the standard WebControls.TextBox , with the
I have class that extend FragmentActivity in it I add fragment to layout as

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.