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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:08:05+00:00 2026-05-15T10:08:05+00:00

If a std::set or std::list contains a sequence of natural numbers (1, 2, 3..).

  • 0

If a std::set or std::list contains a sequence of natural numbers (1, 2, 3..). would there be a function in standard library to find the missing number?

  • 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-15T10:08:06+00:00Added an answer on May 15, 2026 at 10:08 am

    You can find all missing numbers using set_difference and a custom iterator:

    class seqIter : public std::iterator<std::input_iterator_tag, int> {
    public:
        seqIter(int n) : num(n) {}
        seqIter(const seqIter & n) : num(n.num) {}
        int & operator *() {return num;}
        seqIter & operator ++() { ++num; return *this; }
        bool operator !=(const seqIter & n) { return n.num != num; }
    private:
        int num;
    };
    
    int main(int argc, char *argv[])
    {
        int n[] = { 1, 3, 4, 7, 10 };
        std::set<int>   numbers(n, n + sizeof(n)/sizeof(n[0]));
        std::set<int>   missing;
        std::set_difference(    seqIter(*numbers.begin()+1), seqIter(*numbers.rbegin()), 
                                numbers.begin(), numbers.end(),
                                std::insert_iterator<std::set<int> >(missing, missing.begin())
                            );
    }
    

    It’s probably not any faster than going thru the numbers with a for loop though.

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

Sidebar

Related Questions

#include <list> #include <set> #include <iterator> #include <algorithm> using namespace std; class MyContainer {
I've created a set list in c++ and fullfilled with elements std::set<Unit*> myUnits; for(std::set<Unit*>::iterator
Imagine you have an std::list with a set of values in it. For demonstration's
I'm adding two different elements to both std::list and std::set and I want the
Is there any Compareable<Collection<T extends Compareable<T>>> implementation in Java (which behaves as C++'s std::list<T>::operator<()
Do erase call in std::set invalidate iterator ? As i have done below 5th
I am using an std::set to hold members of type Board , and I
I have a set of shared pointers: std::set<boost::shared_ptr<T>> set; And a pointer: T* p;
For data types such as std::set and std::map where lookup occurs in logarithmic time,
If I change an element of an std::set, for example, through an iterator, I

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.