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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:51:19+00:00 2026-06-02T17:51:19+00:00

I have a STL vector of STL vector which contains integer values. Some inside

  • 0

I have a STL vector of STL vector 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. I’ve been seen the following method:

std::vector<std::vector<int>> myVec;
std::sort(myVec.begin(), myVec.end());
myVec.erase(std::unique(myVec.begin(), myVec.end()), myVec.end());

the problem is that i want to eleminate duplicates keeping the order of elements of each order(original order or without sort it), what is the best way to do this? is there another way more efficient?

Example:

1 6 4 5
3 1 5 2----> result of elimination: 1 6 4 5
2 1 3 5                             3 1 5 2  

Thanks in advance

vacing

  • 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-02T17:51:23+00:00Added an answer on June 2, 2026 at 5:51 pm

    The question is not exactly clear. So I am going to give two answers.

    (1) If you wish to remove duplicates but retaining 1 copy while maintaining myVec‘s order, you need to use a set.

    std::vector< std::vector<int> > myVec;
    //or std::unordered_set if you expect mostly unique sorted inner vectors
    std::set< std::vector<int> > exists; 
    std::vector< std::vector<int> > tmpVec;
    
    for (std::size_t i=0, N=myVec.size(); i<N; ++i)
    {
        std::vector<int> key(myVec[i]);
        std::sort(key.begin(), key.end());
        if (exists.find(key) == exists.end())
        {
            exists.insert(key);
            tmpVec.push_back(std::vector<int>());
            std::swap(myVec[i], tmpVec.back());
         }
    }
    
    std::swap(tmpVec, myVec);
    

    (2) If you wish to remove all elements that appear more than once in myVec you need a map of counters.

    std::vector< std::vector<int> > myVec;
    //or std::unordered_map if you expect mostly unique sorted inner vectors
    std::map< std::vector<int>, unsigned > counters; 
    
    // first loop to count
    for (std::size_t i=0, N=myVec.size(); i<N; ++i)
    {
        std::vector<int> key(myVec[i]);
        std::sort(key.begin(), key.end());
        ++counters[key];
    }
    
    // second loop to filter
    std::vector< std::vector<int> > tmpVec;
    for (std::size_t i=0, N=myVec.size(); i<N; ++i)
    {
        std::vector<int> key(myVec[i]);
        std::sort(key.begin(), key.end());
        if (counters[key] == 1)
        {
            tmpVec.push_back(std::vector<int>());
            std::swap(myVec[i], tmpVec.back());
         }
    }
    
    std::swap(tmpVec, myVec);
    

    Both solutions respects the order of elements in myVec and retains the original order in the inner vectors’ elements.

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

Sidebar

Related Questions

I have some questions about vector in STL to clarify..... Where are the objects
In my current C++-project I have an STL map which maps integer keys onto
If you have an STL vector which has been resized, is it safe to
We have a C++ library which uses a struct containing an STL vector of
If i have a myVector which is a STL vector and execute a loop
Suppose I have a STL map where the values are pointers, and I want
I'm trying to write a class which contains several std::vectors as data members, and
I often have classes that are mostly just wrappers around some STL container, like
I have a DLL which needs to access data stored in STL containers in
Which STL container would fit my needs best? I basically have a 10 elements

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.