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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:15:00+00:00 2026-05-26T00:15:00+00:00

I have a vector of vector ( loops ) which contains integer values. Some

  • 0

I have a vector of vector (loops) which contains integer values. Some inside vectors are duplicating but their element order is not the same. Now, I want to get a vector of vector without having any duplicate inner vectors.
here is an example for my vec of vec;

loops = ((9 18 26 11 9), (9 11 26 18 9),(9 18 25 16 9),(11 45 26 11),( 11 26 45 11),( 16 49 25 16),( 16 25 49 16),(18 9 11 26 18),( 18 9 16 25 18),( 25 16 49 25),( 26 11 45 26))

To identify whether any inner vector is a duplicate of another inner vector; I have developed a function IsDuplicate. This tells me, (9 18 26 11 9) and (9 11 26 18 9) are duplicates then I can delete the second or all other duplicates.

To remove duplicate vectors inside my vector of vector, I have implemented following codes.

Vector<vector<int> > loops;
Vector<vector<int> > ::iterator no1, no2;
Int setno1, setno2;

for (no1=loops.begin(), setno1=0; no1!=loops.end(); no1++, setno1++){
       set1 = *no1;
       for (no2=loops.begin()+setno1, setno2=setno1; no2!=loops.end(); setno2++){
            set2 = *no2;
            if (set2.IsDuplicate(set1))  loops.erase(loops.begin()+setno2);
            else no2++;
       }

  }

it took very very long time and i thought my program is crasihing. so, Please help me to rectify this issue.

also, i tried with this. this works but i got a wrong answer. any help please.

01   int first=0; bool duplicates=false;  
02   do {     
03        set1 = loops[first];     
04        for (no2=loops.begin()+1, setno2=1;  no2!=loops.end();  setno2++){     
05             set2 = *no2;      
06             if (set2.IsPartOf(set1)){      
07                 loops.erase(loops.begin()+setno2);     
08                 duplicates = true;      
09             }      
10             else no2++;     
11        }      
12        first++;      
13       } while(!duplicates); 
  • 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-26T00:15:00+00:00Added an answer on May 26, 2026 at 12:15 am

    The idiomatic way is to use the Erase/Remove idiom with a custom predicate.
    To check for duplicate vectors and without modifying the contents of your vectors, write a predicate that takes its arguments by value, sort the vectors and use std::equal.

    bool equal_vector(std::vector<int> a, std::vector<int> b) {
      std::sort(a.begin(), a.end());
      std::sort(b.begin(), b.end());
    
      return std::equal(a.begin(), a.end(), b.begin());
    }
    
    // use it like this
    v.erase( remove_if(v.begin(), v.end(), equal_vector), v.end() );
    

    As to why your current code fails: Erasing an element from a vector invalidates all other iterators to that vector that are currently in existence thus vector::erase returns a valid iterator to the position after the element that has been removed.

    The stdlib also provides the set and multiset container which look like a much better fit for your purpose.

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

Sidebar

Related Questions

I have a 2D vector which contains the coordinates which I want to represent
I have a vector-like class that contains an array of objects of type T
I have a vector of pointers to a class. I need to call their
I have a std::vector that holds a Point struct (x,y,z and some other non-pointer
in the program I'm working on I have 3-element arrays, which I use as
we have a C++ class which basically reads and writes vectors from a binary
Have a vector of vectors that looks something like this 3 1 2 0
I have a vector array of triangles which basically consist of a bunch of
I have a std::vector<A*> which I need to deep copy to another vector using
I have a boost::variant , which contains various types and I have a string

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.