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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:04:44+00:00 2026-05-29T16:04:44+00:00

In my current C++-project I have an STL map which maps integer keys onto

  • 0

In my current C++-project I have an STL map which maps integer keys onto objects. An algorithm returns a set of entries. The returned data depends on the algorithm’s input and hence cannot be predicted:

  class MyClass
  {
     //...
  };

  int myAlgorithm(vector<int>::iterator inputIt)
  {
     // return a key for myMap which is calculated by the current value of inputData
  }

  int main(int argc, char *argv[])
  {
     vector<int> inputData;
     map<int, MyClass> myMap;
     //<fill map with some data>
     //<fill inputData>

     vector<MyClass> result;

     for (vector<int>::iterator it = inputData.begin(); it != inputData.end(); it++)
     {
        int myMapKey = myAlgorithm(*it);
        // count() > 0 means "check whether element exists. Performance can be improved by replacing
        //    the operator[] and count() calls by map::find(). However, I want to simplify things
        //    in this example.
        if (myMap.count(myMapKey) > 0)
        {
           // in some cases there is no entry in myMap
           result.push_back(myMap[myMapKey]);
        }
     }
  }

As mentioned in the example I can replace map::count() and operator[]-calls with find. The STL-reference says that map::find()’s complexity is logarithmic in size (O(log n)).

I discovered that in most cases the entries in myMap are very close for two sequent entries in the result. Therefore I came to the conclusion that I would achieve better performance if I replaced the map.find() calls by iterators:

     map<int, MyClass>::iterator myMapIt = myMap.begin();
     for (vector<int>::iterator it = inputData.begin(); it != inputData.end(); it++)
     {
        int myMapKey = myAlgorithm(*it);
        // just increment iterator
        while (myMapKey != myMapIt->first)
        {
           myMapIt++;
           // we didn't find anything for the current input data
           if (myMapIt == myMap::end() || myMapIt->first > myMapKey)
           {
              break;
           }
        }

        // I know that I'm checking this twice, but that's not the point of my
        //    question ;)
        if (myMapIt == myMap::end() || myMapIt->first > myMapKey)
        {
           // probably it would be better to move the iterator back to the position
           //    where we started searching, to improve performance for the next entry
           myMapIt = myMap.begin();
        }
        else
        {
           result.push_back(myMapIt.second);
        }
     }

This concept works but I have a big problem: Depending on the inputData, I have to search forward or backward. Consider that I call the code inside main() multiple times and the inputData changes for these calls. Instead of checking whether to increment or decrement the iterator inside the while-loop, I could decide that before entering the for-loop.

I thought that I’m fine with just switching the map<>::iterator to map<>::reverse_iterator and using rbegin()/rend() instead of begin()/end(). But then I realized that reverse_iterator and iterator have no common base class:

     map<int, MyClass>::base_iterator myIt;
     if (/* ... */)
     {
        myMapIt = myMap::begin();
        myMapEndIt = myMap::end();
     }
     else
     {
        myMapIt = myMap::rbegin();
        myMapEndIt = myMap::rend();
     }
     /* for (...) ... */

That would be great, but there is no base_iterator.

I know a simple workaround for this problem: I just need to copy the whole for-loop and adjust it for both cases:

     if (/* ... */)
     {
        /* for(...) which uses normal iterator in the while-loop */
     }
     else
     {
        /* for(...) which uses reverse iterator in the while-loop */
     }

Very bad … Do you know a better solution?

  • 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-29T16:04:46+00:00Added an answer on May 29, 2026 at 4:04 pm

    A common base type is unnecessary when the language allows Generic Programming.

    What you simply need to realize is that instead of having a long-winded linear functions with several choices along the way, you can have several nested function in which each choice lead to a different call.

    Taking your example:

    boost::any_iterator start, end;
    if (/* ... */) {
      start = map.begin(), end = map.end();
    } else {
      start = map.rbegin(), end = map.rend();
    }
    
    // do something with start and end
    

    You can transform the code into the following:

    // Define a free-function in the .cpp to help factor common stuff
    template <typename FwdIt>
    static void dosomething(FwdIt start, FwdIt end) {
      // do something with start and end
    }
    

    And then inject the call straight into the if/else body:

    if (/* ... */) {
      dosomething(map.begin(), map.end());
    } else {
      dosomething(map.rbegin(), map.rend());
    }
    

    And one good thing is that you reduce the number of changes of states within your functions and thus their complexity.

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

Sidebar

Related Questions

I have a requirement on my current project (a Flex app which will be
I have some classes in my current project which have the wrong package declaration
In my current project I have to decide which technique to use when branching.
In my current project, I have a nested set of regions composed as follows:
In my current project, I have quite a few objects I need to persist
In my current project set up I have a version with a set due
In my current project I have data about colors. Each color is either a
In my current project we have a large repository of content that was originally
I have a spec in my current project that requires us to advise the
I have a few classes in my current project where validation of Email/Website addresses

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.