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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:17:25+00:00 2026-06-02T06:17:25+00:00

I am using a std::map as data holder for my algorithm but for some

  • 0

I am using a std::map as data holder for my algorithm but for some reason, I need to control the access of map element in some way. We know that one can access the element of the map by calling operator[key] directly. However, if the key doesn’t not exist and whenever you call operator[key] it will create that key with the value initialized as ‘ZERO’ automatically. But in my algorithm, I will control the access by limiting that one can only modify the element when the exist key and when the value is non-zero. For example, if the map has the following elements (3, 2), (1, 0), (4, 0), (2, 7), one can only modify (3,2) and (2,7). I know that I can add some code with map::find(key) or map::count(key) anywhere before I modify the element, but it is too many so I would like to write my own container as follows

class MyContainer;

template <typename T> class myiterator :public iterator<forward_iterator_tag, T>
{
  friend class MyContainer;
  private:
    T *pointer;
    myiterator(T *pointer):pointer(pointer) {}

  public:
    T& operator*() {return (*pointer);}

    const myiterator<T>& operator++()
    {
      pointer->current_iterator++;
  return *this;
    }

    bool operator!=(const myiterator<T>& other) const
    {
      return pointer->current_iterator != other.pointer->current_iterator;
    }

    bool isEnd(void) const
    {
      return pointer->current_iterator == pointer->end_iterator;
    }
  };

  class MyContainer
  {
    friend class myiterator<MyContainer>;
    public:
      typedef myiterator<MyContainer> iterator;

    private:
      map<int, int> data;
      map<int, int>::iterator current_iterator, end_iterator;

    public:
      MyContainer() {current_iterator = data.begin(); }

      void addDataPair(int key, int value) {data[key] = value;}

      int first() {return (*current_iterator).first;}
      int second() {return (*current_iterator).second;}

      // initialize the current_iterator to the begin of the data (map) and set the end iterator too
      iterator begin() 
      {
        current_iterator = data.begin();
        end_iterator = data.end();
        return myiterator<MyContainer>(this);
      }

      // return the container w/ current_iterator point to where the key is
      MyContainer &operator[](int key)
      {
        current_iterator = data.find(key);
        return (*this);
      }

      // only increase the value by one when the key does exist and with initial value non-zero
      void operator++(void)
      {
        if ( (current_iterator != data.end()) && 
           ((*current_iterator).second>0) ) 
        {
          ((*current_iterator).second)++;
        }
      }
   };

As you can see, instead of using map::iterator, I inherit one from std::iterator such that, the iterator refer to the MyContainer itself instead of the value type of the map. I can visit all element by

MyContainer h;

h.addDataPair(1, 3);
h.addDataPair(2, 4);
h.addDataPair(3, 0);
h.addDataPair(7, 9);
h.addDataPair(11, 2);

for (MyContainer::iterator it=h.begin(); !it.isEnd(); ++it)
{
  cout << (*it).first() << " " << (*it).second() << endl;
}

With this idea, whenever the iterator was looped, it will return a referent the the container so I can add some code (like operator[], operator++) to control the behavior of updating the map element. For example, in this code,

void operator++(void)

will ignore any operation on non-exist key or on the element with value initialized as zero. However, there are still some doubts in the code which I am looking for your suggestions

1) if you read the code carefully, you will see that I use current_iterator to store the current map::iterator and use end_iterator to store the end iterator for the map. Those iterators will be set when MyContainer.begin() was called. The reason I need end_iterator is that if instead I set the current_iterator as map.end() then, it will change the current_iterator during the loop. For example, the following code won’t work

iterator begin() 
{
  current_iterator = data.begin();
  return myiterator<MyContainer>(this);
}

iterator end() 
{
  current_iterator = data.end();   // here we set current_iterator as data.end(), but this will change the current iterator of data too
  return myiterator<MyContainer>(this);
}

so when you loop the container with the following code, it won’t run correctly

for (MyContainer::iterator it=h.begin(); it!=h.end(); ++it)
{
  cout << (*it).first() << " " << (*it).second() << endl;
}

that’s why I write a isEnd() function in the iterator instead. But this doesn’t look elegant, any better idea to get around this?

2) for the ‘limited’ ++ operation, if we modify the map element from the container as follows, it goes without any problem

// assuming the map initially contains (2, 4), (3, 0), (7, 9), (11, 2)

h[4]++; // modify the element with key==4, won't do anything, no such key
h[3]++; // modify the element with key==3, won't do anything, value=0
h[11]++; // modify the element with key==11, then we have (11, 3)
(*(h.begin()))++; // modify the first element, works, we have (2,5)

But if you modify that while iterating all elements, the loop will never end, why’s that

for (MyContainer::iterator it=h.begin(); !it.isEnd(); ++it)
{
  (*it)++;    // it works
  (*it)[3]++; // it will cause the loop run and never stop
  cout << (*it).first() << " " << (*it).second() << endl;
}

Any idea?

  • 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-02T06:17:30+00:00Added an answer on June 2, 2026 at 6:17 am

    But if you modify that while iterating all elements, the loop will never end, what’s that

    Then don’t, instead build a list of objects to delete, then remove them after the loop.

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

Sidebar

Related Questions

I have a function that translates data using std::map struct HistoParameter { int nbins;
I'm using std::map to store a lot of elements (pairs of elements) and I
using c++ std's unordered_map i want to map an integer triplet to a single
I have a std::map that I'm using to store values for x and y
i have a std::map and i am using iterator to find a certain key,value
I am having problems using my custom class with a std::map. The class dynamically
using namespace std; class A { public: A() {} ~A() {} map<int, string*>& getMap()
I'm looking for a STL container that works like std::multimap, but has constant access
I have a program that load data from a file using std::ifstream and store
I'm using a std::map (VC++ implementation) and it's a little slow for lookups via

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.