92 lines
3.2 KiB
C++
92 lines
3.2 KiB
C++
|
// Unique.cpp : This file contains the 'main' function. Program execution begins and ends there.
|
|||
|
//
|
|||
|
|
|||
|
#include <iostream>
|
|||
|
#include <string>
|
|||
|
#include <algorithm>
|
|||
|
#include <vector>
|
|||
|
#include <set>
|
|||
|
using namespace std;
|
|||
|
|
|||
|
/* <20><>Ӧ<EFBFBD>̳<EFBFBD>
|
|||
|
* STL <EFBFBD>и<EFBFBD> vector ȥ<EFBFBD>ص<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>: https://xmuli.blog.csdn.net/article/details/126322011
|
|||
|
* STL <EFBFBD><EFBFBD> std::set <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ṹ<EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϸ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ıȽϺ<EFBFBD><EFBFBD><EFBFBD>: https://xmuli.blog.csdn.net/article/details/126354597
|
|||
|
*/
|
|||
|
|
|||
|
struct MyData
|
|||
|
{
|
|||
|
wstring name;
|
|||
|
wstring md5;
|
|||
|
|
|||
|
// <20><>ʽһ<CABD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> < <20><><EFBFBD><EFBFBD>
|
|||
|
//bool operator<(const MyData& d) const noexcept {
|
|||
|
// return md5 < d.md5;
|
|||
|
//}
|
|||
|
};
|
|||
|
|
|||
|
// <20><>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD>ṹ<EFBFBD><E1B9B9><EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD><CDA3>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD>ȽϺ<C8BD><CFBA><EFBFBD> _Pr <20><><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><CEB2>õķ<C3B5>ʽ<EFBFBD><CABD>
|
|||
|
bool cmpSort(const MyData& d1, const MyData& d2) {
|
|||
|
// Simple example
|
|||
|
return d1.md5 < d2.md5;
|
|||
|
|
|||
|
// Complex sample
|
|||
|
//if (d1.md5 != d2.md5)
|
|||
|
// return d1.md5 < d2.md5;
|
|||
|
//else
|
|||
|
// return d1.name < d2.name;
|
|||
|
};
|
|||
|
|
|||
|
// <20><>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD>֣<EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Lambda <20><><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD>ijЩ<C4B3><D0A9><EFBFBD><EFBFBD><EFBFBD>㷨<EFBFBD>л<EFBFBD>ʧЧ<CAA7><D0A7>
|
|||
|
auto cmpSortLambda = [](const MyData& d1, const MyData& d2) {
|
|||
|
return d1.md5 < d2.md5;
|
|||
|
};
|
|||
|
|
|||
|
// <20><>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԡ<EFBFBD><D4A1><EFBFBD><EFBFBD>£<EFBFBD><C2A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD>Եģ<D4B5><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˴<EFBFBD><CBB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ջ<EFBFBD>
|
|||
|
struct cmpSortObj
|
|||
|
{
|
|||
|
bool operator()(const MyData& d1, const MyData& d2) const {
|
|||
|
return d1.md5 < d2.md5;
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
// ************* std::unique <20><><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD>ȽϺ<C8BD><CFBA><EFBFBD> *************
|
|||
|
bool cmpUnique(const MyData& d1, const MyData& d2) {
|
|||
|
return d1.md5 == d2.md5; // <20><><EFBFBD>ϸ<EFBFBD><CFB8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȣ<EFBFBD><C8A3><EFBFBD><EFBFBD>ԴﵽĿ<EFB5BD>꣬<EFBFBD><EAA3AC><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>й©
|
|||
|
//return d1.md5 == d2.md5 && d1.name == d2.name; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͱ<EFBFBD><CDB1>룬<EFBFBD><EBA3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
};
|
|||
|
|
|||
|
int main()
|
|||
|
{
|
|||
|
vector<MyData> vec = { { L"a1.exe", L"B9204B6362A65C248950D5A41F341DCC"},
|
|||
|
{ L"a2.exe", L"B9204B6362A65C248950D5A41F341DCC"},
|
|||
|
{ L"b1.exe", L"C96D1AA5D314954417C28A645ED72665"},
|
|||
|
{ L"b2.exe", L"C96D1AA5D314954417C28A645ED72665"} };
|
|||
|
|
|||
|
wcout.imbue(locale("", LC_CTYPE));
|
|||
|
wcout << L"----------<2D><><EFBFBD><EFBFBD>ǰ--------" << endl;
|
|||
|
for (const auto& it : vec)
|
|||
|
wcout << L"[it.name]" << it.name << L"[it.md5]" << it.md5 << endl;
|
|||
|
|
|||
|
// ************* <20><>һ<EFBFBD><D2BB>vector, sort + unique *************
|
|||
|
//sort(vec.begin(), vec.end(), cmpSort);
|
|||
|
//auto ite = std::unique(vec.begin(), vec.end(), cmpUnique);
|
|||
|
//vec.erase(ite, vec.end());
|
|||
|
|
|||
|
// ************* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>vector + set<65><74><EFBFBD>ֶ<EFBFBD><D6B6><EFBFBD>ֵ<EFBFBD><D6B5> *************
|
|||
|
//set<MyData, cmpSortObj> s; // "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"<22><><EFBFBD><EFBFBD> set <20><><EFBFBD><EFBFBD> --> OK
|
|||
|
set<MyData, decltype(cmpSort)*> s(&cmpSort); // "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD> + decltype"<22><><EFBFBD><EFBFBD> set <20><><EFBFBD><EFBFBD> --> OK: <20><><EFBFBD>롢<EFBFBD><EBA1A2><EFBFBD>гɹ<D0B3><C9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD>ɹ<EFBFBD>
|
|||
|
//set<MyData, decltype(cmpSort)*> s; // --> Error: <20><><EFBFBD>롢<EFBFBD><EBA1A2><EFBFBD>гɹ<D0B3><C9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ի<EFBFBD>ʧ<EFBFBD><CAA7>
|
|||
|
|
|||
|
for (unsigned i = 0; i < vec.size(); ++i)
|
|||
|
s.insert(vec[i]); // <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD>п<EFBFBD><D0BF><EFBFBD>֤<EFBFBD><D6A4><EFBFBD>洴<EFBFBD><E6B4B4> std::set <20>Ľ<EFBFBD><C4BD><EFBFBD>
|
|||
|
vec.assign(s.begin(), s.end());
|
|||
|
|
|||
|
// ************* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>vector + set<65><74><EFBFBD><EFBFBD><EFBFBD>캯<EFBFBD><ECBAAF><EFBFBD><EFBFBD> *************
|
|||
|
// Ч<>ʲ<EFBFBD><CAB2>硺<EFBFBD><E7A1BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
wcout << L"----------<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>--------" << endl;
|
|||
|
for (const auto& it : vec)
|
|||
|
wcout << L"[it.name]" << it.name << L"[it.md5]" << it.md5 << endl;
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|