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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:07:34+00:00 2026-06-12T19:07:34+00:00

I would like to know if there is an efficient way to remove objects

  • 0

I would like to know if there is an efficient way to remove objects from a container based on values of member fields of the objects. For example, I can do the following using stl::unique with a list of strings:

#include<iostream>
#include<list>
#include<string>
#include<algorithm>
using namespace std;

bool stringCompare(const string & l, const string & r)                                                                                   
{                                                                                                                                    
   return (l==r);                                                                                                                         
}

int main()                                                                                                                               
{                                                                                                                                        

  list<string> myStrings;                                                                                                                
  myStrings.push_back("1001");                                                                                                           
  myStrings.push_back("1001");                                                                                                           
  myStrings.push_back("81");                                                                                                             
  myStrings.push_back("1001");                                                                                                           
  myStrings.push_back("81");                                                                                                             

  myStrings.sort();                                                                                                                      
  myStrings.erase(unique(myStrings.begin(), myStrings.end(), stringCompare), myStrings.end());                                           

  list<string>::iterator it;                                                                                                             
  for(it = myStrings.begin(); it != myStrings.end(); ++it)                                                                               
  {                                                                                                                                      
    cout << *it << endl;                                                                                                                 
  }                                                                                                                                      

  return     0;                                                                                                                              
}

prints 1001, 81…

Is there a way I can do something similar with the following code, or do I need to perform the comparisons “manually” using operators and iterating through the containers. I couldn’t think of a more elegant solution and would like to know if this is possible without writing a lot of code. Any help will be much appreciated!

class Packet
{
public:
Packet(string fTime, string rID) : filingTime(fTime), recordID(rID)

  string getFilingTime() {return filingTime;}
  string getRecordId() {return recordID;}

private:
  string filingTime;
  string recordID;

};

int main()
{
vector<Packet*> pkts;
pkts.push_back(new Packet("10:20", "1004"));
pkts.push_back(new Packet("10:20", "1004")); // not unique (duplicate of the line above)
pkts.push_back(new Packet("10:20", "251"));
pkts.push_back(new Packet("10:20", "1006"));

// remove packet from vector if time and ID are the same

return 0;
}

Thanks

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

    Two options to be able to use std::unique:

    1. Define an operator== method for Packet and change the vector<Packet*> to vector<Packet>.

      bool Packet::operator==(const Packet& rhs) const
      {
          if (getFilingTime() != rhs.getFilingTime())
              return false;
          if (getSpid() != rhs.getSpid())
              return false;
          return true;
      }
      
      //etc.
      
      int main()
      {
          vector<Packet> pkts;
          pkts.push_back(Packet("10:20", "1004"));
          pkts.push_back(Packet("10:20", "1004")); // not unique (duplicate of the line above)
          pkts.push_back(Packet("10:20", "251"));
          pkts.push_back(Packet("10:20", "1006"));
      
          // remove packet from vector if time and ID are the same
      
           pkts.erase(unique(pkts.begin(), pkts.end()), pkts.end());                   
      
          return 0;
      }
      
    2. Keep the vector as vector<Packet*> and define a method to compare the elements.

      bool comparePacketPtrs(Packet* lhs, Packet* rhs)
      {
          if (lhs->getFilingTime() != rhs->getFilingTime())
              return false;
          if (lhs->getSpid() != rhs->getSpid())
              return false;
          return true;
      }
      
      //etc.
      
      int main()
      {
          vector<Packet*> pkts;
          pkts.push_back(new Packet("10:20", "1004"));
          pkts.push_back(new Packet("10:20", "1004")); // not unique (duplicate of the line above)
          pkts.push_back(new Packet("10:20", "251"));
          pkts.push_back(new Packet("10:20", "1006"));
      
          // remove packet from vector if time and ID are the same
      
           pkts.erase(unique(pkts.begin(), pkts.end(), comparePacketPtrs), pkts.end());                   
      
          return 0;
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know an efficient method to remove duplicate items from a
I would like to know if there's an efficient algorithm to find the greatest
I would like to know if there's a way in java to find out
I would like to know if there is a way to execute polygons queries
Is there a way to do this? I would like to know how many
I would like to know if there exists any kind of library or workaround
I would like to know if there is any kind of tool to monitor
I would like to know if there is a method to create share buttons
I would like to know is there any difference in performance between these two
I would like to know if there are any good resources (online or books)

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.