to compare data_members of a objects store in std::vector

Overview of problem : I am using std::vector to hold objects of Subject. Now this vector contains lots of objects( with lots I mean 10-20 objects at max) .

These objects have string member values like category and sub_category.

Both category and sub_category can have string which can be same of other objects's sub_category & category.

Issue: Now I want my std::vector to have only those objects whose's sub_category are unique. If category is not unique that's not a problem .

Secondly if we found 2 objects having same sub_category then we have to delete one of them from the vector. we will delete it based on some rules example

Rules for deleting are if
i) instance of Subject ->category = " Land " OR if category = "Jungle" then delete other duplicate object ,
ii) if above condition doesn't match then delete either of them.

I am wondering , how would I compare the sub-items from the vector . For example

I have class say Subject


1
2
3
4
5
6
7
8
9
10
11
12
class Subject
{
public :
// some constructors,
// functions to get ., set category and sub category
   std::String get_sub_category()   
   std::string get_category();

 private:
   std::string category;
  std::string sub_category;
}


I have vector which stores object of Subjects. example
vector<Subject> copy_vector;

Now what I want is to delete the object from vector that has same sub_category I am not looking for source code buT i need a starting point,? example

say
1
2
3
copy_vector[0] = Animal  object that has sub_category Tiger
    copy_vector [1] = Animal object   with Lion as sub category 
    copy_vector[2] = Forest object with sub_category Tiger


o what I want is to based on some conditions(which I can do ) remove either Forest or Animal object containing Tiger. But for that how would I do comparison?

Thanks everyone for the help. I have written the function and have checked it but I am sure there is a room for hell lot of improvement.
May you guys please pin out out my pitfalls.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
std::vector< Subject >copy_vector;
 // copy_vector contains all the objects of Subject with redundant sub_category


  for( std::vector< Subject >::iterator ii = copy_vector.begin() ; ii != copy_vector.end() ; ++ii )
  {
      sub_category = ii->get_sub_category();

      std::cout <<" sub_category-- in main for loop " << sub_category  << std::endl ;
      std::vector< Subject >::iterator it = ii+1;
      while( it != copy_vector.end() )
      {
            std::cout <<" the  size of copy _vector is = " << copy_vector.size() << std::endl ; // for debug purpose
          if( it->get_sub_category() == sub_category )
          {
              std::cout <<" we got a match here" << std::endl ;
              // since both are duplicate , we have to delete one of them. Rules for deleting  are if 
              i) instance of Subject ->category = " Land " OR if category = "Jungle"   then delete other duplicate object , 
              ii) if above condition doesn't match then delete either of them.

              if( ( it->get_category == "Land" ) || (  it->get_category == "Jungle" )  )
              {
                 std::cout <<" we are deleting it reference value  " << std::endl ;
                 it =  copy_vector.erase(ii);

                 // increment the counter 
                 ++ii;
              }
              else if( ( ii->get_category == "Land" ) || (  ii->get_category == "Jungle" )  )
              {
                 std::cout <<" we are deleting from copy_vector  " << std::endl ;
                 it =  copy_vector.erase(it);
              }

              else
              {
                     std::cout <<" we are deleting from copy_vector  when there is no match for rules " << std::endl ;
                      it =  copy_vector.erase(it);
              }
              std::cout <<" the size of copy _vector is = " << copy_vector.size() << std::endl ;

          }
          else
          {
              std::cout <<" No Match" << std::endl;
              // increase main iterator 
              if( it != copy_vector.end() )
             {
                     ++it;
             }
          }
      }

  }
  //print value
    for( std::vector< Subject >::iterator ii = copy_vector.begin() ; ii != copy_vector.end() ; ++ii )
    {

        std::cout <<" New list = " << ii->get_category <<" \t " << ii->get_sub_category() << std::endl;
    }  
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

struct subject
{
    std::string category;
    std::string sub_category;
};

struct less
{
    bool operator() ( const subject& a, const subject& b ) const
    {
        // order by sub_category
        if( a.sub_category < b.sub_category ) return true ;
        if( a.sub_category > b.sub_category ) return false ;

        // Rules for deleting are if
        // instance of Subject ->category = " Land " OR if category = "Jungle" 
        // then delete other duplicate object
        // therefore, if sub_caegory is the same, subject with category
        // "Land" or "Jungle" should appear earlier in the sorted sequence
        return ( ( a.category == "Jungle" ) || ( a.category == "Land" ) ) >
               ( ( b.category == "Jungle" ) || ( b.category == "Land" ) ) ;

    }
};

struct equal
{
    // I want my std::vector to have only those objects whose's sub_category are unique.
    bool operator() ( const subject& a, const subject& b ) const
    { return a.sub_category == b.sub_category ; }
};


int main()
{
    std::vector<subject> seq {  { "Animal", "Tiger" },
                                { "Citizenship", "Russia" },
                                { "Online shop", "Amazon" },
                                { "Jungle", "Amazon" },
                                { "Animal", "Lion" },
                                { "Stuffed toy", "Tiger" },
                                { "Language", "English" },
                                { "Language", "American" },
                                { "Language", "French" },
                                { "Animal", "Cow" },
                                { "Citizenship", "American" },
                                { "Land", "Russia" } } ;
    
    // http://en.cppreference.com/w/cpp/algorithm/sort
    std::sort( std::begin(seq), std::end(seq), less() ) ;
    
    // http://en.cppreference.com/w/cpp/algorithm/unique
    seq.erase( std::unique( std::begin(seq), std::end(seq), equal() ), seq.end() ) ;

    for( const subject& s : seq ) std::cout << s.category << " - " << s.sub_category << '\n' ;
}

http://coliru.stacked-crooked.com/a/7fd64c1d628f9b06
Topic archived. No new replies allowed.