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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:19:49+00:00 2026-06-14T17:19:49+00:00

This question applies to both std::set and std::unsorted_set . I have an iterator to

  • 0

This question applies to both std::set and std::unsorted_set.

I have an iterator to an element in a set. I’d like to use the iterator to get an “index” for the element based on its location in the set.

For example, the indices for my set would be as follows:

int index = 0;

for(MySetType::iterator begin = mySet.begin(); begin != mySet.end(); begin++)
{
    cout << "The index for this element is " << index;
    index++;
}

I have tried doing arithmetic using iterators but it doesn’t work:

int index = mySetIterator - mySet.begin();

Is there any way to use the iterator to get an index value like this based on its location in the set?

  • 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-14T17:19:50+00:00Added an answer on June 14, 2026 at 5:19 pm

    Use STL distance, namely std::distance(set.begin(), mySetIterator)

    Please note that:

    Returns the number of elements between first and last. The behavior
    is undefined
    if last is not reachable from first by (possibly
    repeatedly) incrementing first.

    Remark : Complexity is linear;

    However, if InputIt additionally meets the requirements of
    LegacyRandomAccessIterator, complexity is constant.

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

Sidebar

Related Questions

I believe this question applies equally well to C# as to Java, because both
I'm using Mnesia with Erlang, but this question applies to any key-value db like
This question also applies to boost::function and std::tr1::function . std::function is not equality comparable:
I believe this question applies to any of the For Html helpers, but my
I am using PrimeFaces p:messages but I think this question applies equally to h:messages
I'm coming largely from a c++ background, but I think this question applies to
This question is kinda aimed towards PHP but it probably applies to other languages
In my work (which is 90% Java but I'm sure this question applies to
Although this is a Java-centric question it really applies to any system utilizing a
I've read the papers linked to in this question . I half get it.

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.