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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:29:13+00:00 2026-05-16T04:29:13+00:00

Goal is, I’ve multiple lists of elements available, and I want to be able

  • 0

Goal is, I’ve multiple lists of elements available, and I want to be able to store all of these elements into a resultant list in an ordered way.

Some of the ideas that comes to my mind are
a) Keep the result as a set (std::set), but the B-tree , needs to rebalanced every now and then.
b) Store all the elements in a list and sort the list at the end.

But, I thought, why not store them in a sorted fashion, as and when we add the items to the resultant list.

Here is my function, that does the job of maintaining the results in a sorted way. Is there an efficient way to do the same?

void findItemToInsertAt(std::list<int>& dataSet, int itemToInsert, std::list<int>::iterator& location)
{
    std::list<int>::iterator fromBegin = dataSet.begin();
    std::list<int>::iterator fromEnd = dataSet.end() ;
    // Have two pointers namely end and begin
    if ( !dataSet.empty() )
        --fromEnd;

    // Set the location to the beginning, so that if the dataset is empty, it can return the appropriate value
    location = fromBegin;
    while ( fromBegin != dataSet.end()  )
    {
        // If the left pointer points to lesser value, move to the next element
        if ( *fromBegin < itemToInsert )
        {
            ++fromBegin;
            // If the end is greater than the item to be inserted then move to the previous element
            if ( *fromEnd > itemToInsert )
            {
                --fromEnd;
            }
            else
            {
                // We move only if the element to be inserted is greater than the end, so that end points to the
                // right location
                if ( *fromEnd < itemToInsert )
                {
                    location = ++fromEnd;
                }
                else
                {
                    location = fromEnd;
                }
                break;
            }
        }
        else
        {
            location = fromBegin;
            break;
        }
    }

}

And, here is the caller of the function

void storeListToResults(const std::list<int>& dataset, std::list<int>& resultset)
{

    std::list<int>::const_iterator curloc;
    std::list<int>::iterator insertAt;

    // For each item in the data set, find the location to be inserted into
    // and insert the item.
    for (curloc = dataset.begin(); curloc != dataset.end() ; ++curloc)
    {
        // Find the iterator to be inserted at
        findItemToInsertAt(resultset,*curloc,insertAt);
        // If we have reached the end, then the element to be inserted is at the end
        if ( insertAt == resultset.end() )
        {
            resultset.push_back(*curloc);
        }
        else if ( *insertAt != *curloc ) // If the elements do not exist already, then insert it.
        {
            resultset.insert(insertAt,*curloc);
        }
    }
}
  • 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-16T04:29:14+00:00Added an answer on May 16, 2026 at 4:29 am

    At a glance, your code looks like it’s doing a linear search of the list in order to find the place to insert the item. While it’s true that std::set will have to balance its tree (I think it’s a Red-Black Tree) in order to maintain efficiency, chances are it’ll do so much more efficiently than what you’re proposing.

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

Sidebar

Related Questions

Goal: Retrieve all instance of List<> from interface GameFactory Problem: I retrieve error message
Goal: Rolling/Running total for all statements at the end of each month. Code: select
Goal I have a generic class GenericClass<T> and I want to pool instances. I'm
Goal: Parse a string from an input type text into TIME type to be
Goal : I have a view which displays multiple nodes in full view including
Goal: I want to implement a hard-coded lookup table for data that doesn't change
Goal I want to simulate with a request to a HTML-document just the way
Goal: I wish users to be able to directly connect to a RDBMS (e.g.,
Goal: Allow a user to authentication with Facebook into an iOS application which requires
Goal : I have a business database website full of businesses. Business owners want

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.