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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:01:40+00:00 2026-06-07T12:01:40+00:00

I was solving a programming problem, which wants to find the SYMMETRIC DIFFERENCE between

  • 0

I was solving a programming problem, which wants to find the SYMMETRIC DIFFERENCE between two sets. I have solved it using STL’s set_symmetric_difference. I am given two vector<int>s, A and B:

A = {342,654,897,312,76,23,78}

B = {21,43,87,98,23,756,897,234,645,876,123}

Sould return (correct answer):

{ 21,43,76,78,87,98,123,234,312,342,645,654,756,876 }

But I get:

{ 21,43,76,78,87,98,123,234,312,342,645,65,756,876}

What is the problem ? Here is my code:

sort(A.begin(), A.end());
sort(B.begin(), B.end());
// allocate the smallest size of A,B as maximum size
vector<int> c(A.size() < B.size() ? B.size() : A.size());
vector<int>::iterator i;
i = set_symmetric_difference(A.begin(), A.end(), B.begin(), B.end(), c.begin());
return vector<int>(c.begin(), i);

NOTE:
I get correct answers for the rest of examples. This example only gives me this wrong answer.

I have tested it in Visual Studio, and got an error message: "Iterator not incrementable"

  • 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-07T12:01:40+00:00Added an answer on June 7, 2026 at 12:01 pm

    The problem is in the initialization of vector c. The logic is slightly wrong in that the maximum size of the output range could be as large as the sum of the two input ranges. Since you don’t know the size a priori, you could be better off by starting with an empty output vector, and using push_back with an std::back_inserter instead:

    sort(A.begin(), A.end());
    sort(B.begin(), B.end());
    std::vector<int> c;
    set_symmetric_difference(A.begin(), A.end(), 
                             B.begin(), B.end(), std::back_inserter(c));
    return c;
    

    This produces

    21 43 76 78 87 98 123 234 312 342 645 654 756 876

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

Sidebar

Related Questions

I was practising the algorithm based programming problem.I am having difficulty,in solving this problem.I
I have a question about solving UI freezing. Introduction: I am currently programming an
I was practicing the dynamic programming problem on SPOJ. But I have no idea
I watched Dynamic Programming - Kapsack Problem (YouTube) . However, I am solving a
I'm currently solving a programming problem to enhance my skills (I'm still a newbie)
While solving any programming problem, what is your modus operandi ? How do you
I'm solving UVA's Edit Step Ladders on an uva sub-site named programming-challenges.com, but since
I have a trouble solving out this fiddle. When i put limits i am
I'm solving Sphere's Online Judge Prime Generator using the Sieve of Eratosthenes. My code
I am solving problem 9 on the Project Euler . In my solution 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.