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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:26:02+00:00 2026-05-11T03:26:02+00:00

I know this questions has come up in various guises before, but this is

  • 0

I know this questions has come up in various guises before, but this is slightly different.

I have a class which contains a std::map. Although I wish to use the map for other purposes inside the class, externally I want to expose an iterator adapter to just the values inside the map (ie the second item in the std::pair).

For example in python I might do something like this:

def __iter__(self):     return self._dict.itervalues() 

How do I go about doing this in c++, hiding the implementation inside the class?

Thanks,

Dan

  • 1 1 Answer
  • 2 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. 2026-05-11T03:26:02+00:00Added an answer on May 11, 2026 at 3:26 am

    Have a look at Boost’s transform_iterator which provides exactly this kind of functionality:

    template <typename K, typename V> struct get_value {     const V& operator ()(std::pair<K, V> const& p) { return p.second; } };  class your_class {     typedef map<int, float> TMap;     TMap mymap;  public:     typedef get_value<TMap::key_type, TMap::data_type> F;     typedef         boost::transform_iterator<F, TMap::iterator>         value_iterator;      value_iterator begin() { return make_transform_iterator(mymap.begin(), F()); }      value_iterator end() { return make_transform_iterator(mymap.end(), F()); }      // TODO Same for const versions.     // Rest of the interface … }; 

    Now you can iterate over the values, e.g. like this:

    your_class c; // Fill c with some values … copy(c.begin(), c.end(), ostream_iterator<float>(cout, ' ')); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this question has been done but I have a slightly different twist
I know this question has been asked before, but non of the solutions solve
I know this question has been posted before... but I haven't found any answer
I know this question has been asked before, but I haven't been able to
I know this question has been asked a couple of times before. I m
I know this question has been asked before however the answers I found where
I know this question has been rised quite a lot of times, but then
I know this question has been asked a zillion times, but I still feel
I know this question has been asked before, and I took a look at
I'm sure this question HAS to have been asked, but after searching through the

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.