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

  • Home
  • SEARCH
  • 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 287037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:41:26+00:00 2026-05-12T05:41:26+00:00

I was using stl::merge to put two sorted collections into one. But my object

  • 0

I was using stl::merge to put two sorted collections into one.

But my object has a natural key; and a defined addition semantic, so what I am after is a merge_and_sum that would not just merge the two collections into a single N+M length collection, but if the operator== on the object returned true, would then operator+ them.

I have implemented it thus

template<class _InIt1, class _InIt2, class _OutIt> 
_OutIt merge_and_sum(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _InIt2 _Last2, _OutIt _Dest )
{   // copy merging ranges, both using operator<
    for (; _First1 != _Last1 && _First2 != _Last2; ++_Dest)
    {
        if ( *_First2 < *_First1 )
            *_Dest = *_First2, ++_First2;
        else if ( *_First2 == *_First1)
            *_Dest = *_First2 + *_First1, ++_First1, ++_First2;
        else
            *_Dest = *_First1, ++_First1;
    }
    _Dest = copy(_First1, _Last1, _Dest);   // copy any tail
    return (copy(_First2, _Last2, _Dest));
}

But was wondering if I have reinvented something that is composable from the other algorithms.

  • 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-12T05:41:26+00:00Added an answer on May 12, 2026 at 5:41 am

    It sounds like your collections are like multisets with duplicates collapsed by your + operator (maybe just summing the multiplicities instead of keeping redundant copies). I assume so, because you’re not changing the sorting order when you +, so + isn’t affecting your key.

    You should use your implementation. There’s nothing in STL that will do it as efficiently. The closest semantic I can think of is standard merge followed by unique_copy. You could almost get unique_copy to work with a side-effectful comparison operator, but that would be extremely ill advised, as the implementation doesn’t promise to only compare things directly vs. via a value-copied temporary (or even a given number of times).

    Your type and variable names are unpleasantly long 😉

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

Sidebar

Related Questions

There are two STL vectors, one with objects and second with pointers to objects.
I'm using an STL map with a struct for a key. This is the
I'm a bit confused about using STL set::find() for a set of my own
The following is a C++ program using STL vector container. Just wanted to know
Previously, I am using STL map to perform the mentioned task. struct ltstr {
I want to create in C++ an array of Objects without using STL. How
Using the STL C++ hash_map... class MyKeyObject { std::string str1; std::string str2; bool operator==(...)
When using C++ STL containers, under what conditions must reference values be accessed? For
I'm using the STL map data structure, and at the moment my code first
I want to print numbers to a file using the stl with the number

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.