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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:20:16+00:00 2026-05-11T09:20:16+00:00

say I have vector<class1a>,vector<class1b> how to remove the common entities from both of them

  • 0

say I have vector<class1a>,vector<class1b> how to remove the common entities from both of them I have defined ==operator for the class1 objects class1a,class1b

  • 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. 2026-05-11T09:20:17+00:00Added an answer on May 11, 2026 at 9:20 am

    The stl algorithms provide several functions to perform set operations, notably calculating the set symmetric difference, which is what you need.

    Here’s an example of use:

    #include <algorithm> #include <vector>  int main(int argc, char **argv) {      std::vector<int> v1;     v1.push_back(1);     v1.push_back(2);     v1.push_back(3);     v1.push_back(4);     v1.push_back(5);     v1.push_back(6);      std::vector<int> v2;     v2.push_back(2);     v2.push_back(4);     v2.push_back(6);     v2.push_back(8);      // Ranges must be sorted!     std::sort(v1.begin(), v1.end());     std::sort(v2.begin(), v2.end());      std::vector<int> res; // Will contain the symmetric difference     std::set_symmetric_difference(v1.begin(), v1.end(),                                    v2.begin(), v2.end(),                                    std::back_inserter(res));      // Copy result to the output     std::copy(res.begin(), res.end(), std::ostream_iterator<int>(cout, ' '));     // Prints '1 3 5'      return 0; } 

    std::set_symmetric_difference takes two range (i.e. two pairs of OutputIterators) and an InputIterator where it will put the result. It also returns an iterator to the end of the result range.


    EDIT

    I just read your comments on your question. If you want the two original vectors to be modified, you can use std::set_difference:

    vector<int>::iterator endRange; endRange = set_difference(v1.begin(), v1.end(),                            v2.begin(), v2.end(),                            v1.begin()); v1.erase(endRange, v1.end()); 

    Here, we put the result of the set difference v1 – v2 into v1. However, we can’t do the vice-versa since v1 is now modified. The solution is to calculate the intersection of v1 and v2, and then the difference with this intersection std::set_intersection :

    vector<int> inter; set_intersection(v1.begin(), v1.end(),                  v2.begin(), v2.end(),                  back_inserter(inter)); // inter is '2 4 6'  v1.erase(set_difference(v1.begin(), v1.end(),                         inter.begin(), inter.end(),                         v1.begin()),          v1.end()); // v1 is '1 3 5'  v2.erase(set_difference(v2.begin(), v2.end(),                         inter.begin(), inter.end(),                         v2.begin()),          v2.end()); // v2 is '8' 

    I guess there are much more performant solutions, but this one is clear, and really convey your intents by using widely known stl algorithms.

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

Sidebar

Related Questions

Let's say i have 2 classes: class Class1 { public: std::vector<CustomClass3*> mVec; public: Class1();
Say I have a list and a vector, and I want to zip them
I have a vector (order is important) of objects (lets call them myobj class)
Lets say I have vectors of different objects, say D is of type vector<
Say I have a vector of pointers to Order objects. Now I want to
Say I have a vector: (def data [Hello World Test This]) And I want
Say I have a vector which has thousands of elements. What is the R
Say I have a vector (x,y) and it is aligned to the origin by
Lets say if I have a vector V, which has 10 elements. If I
Say you have a simple class with some storage data structure (list, vector, queue,

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.