开发者

How to implement std::set or std::hash_set to filter the duplicated item in an array list?

Given the below scenario, I'm having a list of item which might be having some duplicated item. I would like to filter the item, to print the only unique item.

Instead of duplicating a list which remove the duplicated item, I trie开发者_开发百科d to insert them into std::set and std::hash_set. Nevertheless, I found no useful example to perform the operation.

Hence I hope to seek your advice on the following code:

#include <list>
//--> A struct to store software name and version
typedef struct tagSWVERStruct{
  TCHAR szSWName[255];
  TCHAR szVersion[255];
}SWVERSIONSTRUCT, *LPSWVERSIONSTRUCT;

//--> A list of struct to store software name and version
typedef std::list<SWVERSIONSTRUCT>  LISTSWVERSION, *PLISTSWVERSION;

void main()
{
  LISTSWVERSION swList;
  SWVERSIONSTRUCT svSW1, svSW2, svSW3, svSW4;
  CString szVersion, szName;

  //Assign value
  _tcscpy_s(svSW1.szSWName, _T("Adapter 1"));
  _tcscpy_s(svSW2.szSWName, _T("Adapter 2"));
  _tcscpy_s(svSW3.szSWName, _T("Adapter 1"));
  _tcscpy_s(svSW4.szSWName, _T("Adapter 3"));
  _tcscpy_s(svSW1.szVersion, _T("1.0.0"));
  _tcscpy_s(svSW2.szVersion, _T("2.0.0"));
  _tcscpy_s(svSW3.szVersion, _T("1.0.0"));
  _tcscpy_s(svSW4.szVersion, _T("3.0.0"));
  swList.push_back(svSW1);
  swList.push_back(svSW2);
  swList.push_back(svSW3);
  swList.push_back(svSW4);

  //print all the item out
  LISTSWVERSION::iterator it = swList.begin();
  for(; it!=swList.end(); ++it){    
    _tprintf(_T("%s version = %s\n"), (*it).szSWName, (*it).szVersion);
  }
}

/*******
Output:  
Adapter 1 version = 1.0.0 
Adapter 2 version = 2.0.0 
Adapter 1 version = 1.0.0 
Adapter 3 version = 3.0.0

Expected Output: 
Adapter 1 version = 1.0.0 
Adapter 2 version = 2.0.0 
Adapter 3 version = 3.0.0
********/


I may not be understanding, but if you want to copy the elements of an std::list into an std::set, you can use a std::copy with an inserter.

std::list<int> list;
list.push_back(1);
list.push_back(2);
list.push_back(2);
list.push_back(3);

std::set<int> set;
std::copy(list.begin(), list.end(), std::inserter(set));


Removing duplicates is easy, assuming you can modify the sequence.

First you will need less-than and equality functors:

struct less_SWVERSIONSTRUCT
{
  bool operator () (SWVERSIONSTRUCT const & a, SWVERSIONSTRUCT const & b) const
  {
    // Replace this with the appropriate less-than relation for the structure.
    return std::memcmp (&a, &b, sizeof (a)) < 0;
  }
};

struct eq_SWVERSIONSTRUCT
{
  bool operator () (SWVERSIONSTRUCT const & a, SWVERSIONSTRUCT const & b) const
  {
    // Replace this with the appropriate equality relation for the structure.
    return std::memcmp (&a, &b, sizeof (a)) == 0;
  }
};

Then sort the list using the comparator:

swList.sort (less_SWVERSIONSTRUCT ());

And now remove sequences of duplicate members:

swList.erase (
    std::unique (swList.begin (), swList.end (), eq_SWVERSIONSTRUCT ()),
    swList.end ());

Done.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜