From 1892ba3acfd9edd6cfda3fff72a97271e830947d Mon Sep 17 00:00:00 2001 From: XMuli Date: Fri, 12 Aug 2022 17:19:24 +0800 Subject: [PATCH] feat: std::vector DeDuplication --- DeDuplication/DeDuplication.sln | 41 +++++ DeDuplication/DeDuplication/DeDuplication.cpp | 1 + DeDuplication/DeDuplication/DeDuplication.h | 54 +++++++ .../DeDuplication/DeDuplication.vcxproj | 151 ++++++++++++++++++ .../DeDuplication.vcxproj.filters | 30 ++++ DeDuplication/DeDuplication/main.cpp | 13 ++ DeDuplication/Unique/Unique.cpp | 92 +++++++++++ DeDuplication/Unique/Unique.vcxproj | 148 +++++++++++++++++ DeDuplication/Unique/Unique.vcxproj.filters | 22 +++ 9 files changed, 552 insertions(+) create mode 100644 DeDuplication/DeDuplication.sln create mode 100644 DeDuplication/DeDuplication/DeDuplication.cpp create mode 100644 DeDuplication/DeDuplication/DeDuplication.h create mode 100644 DeDuplication/DeDuplication/DeDuplication.vcxproj create mode 100644 DeDuplication/DeDuplication/DeDuplication.vcxproj.filters create mode 100644 DeDuplication/DeDuplication/main.cpp create mode 100644 DeDuplication/Unique/Unique.cpp create mode 100644 DeDuplication/Unique/Unique.vcxproj create mode 100644 DeDuplication/Unique/Unique.vcxproj.filters diff --git a/DeDuplication/DeDuplication.sln b/DeDuplication/DeDuplication.sln new file mode 100644 index 0000000..a02f777 --- /dev/null +++ b/DeDuplication/DeDuplication.sln @@ -0,0 +1,41 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.32802.440 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "DeDuplication", "DeDuplication\DeDuplication.vcxproj", "{5FAA2373-0F7C-4CD9-BA01-564733CAF15C}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Unique", "Unique\Unique.vcxproj", "{E65BD110-0E5A-44EE-B304-314596C38A9F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {5FAA2373-0F7C-4CD9-BA01-564733CAF15C}.Debug|x64.ActiveCfg = Debug|x64 + {5FAA2373-0F7C-4CD9-BA01-564733CAF15C}.Debug|x64.Build.0 = Debug|x64 + {5FAA2373-0F7C-4CD9-BA01-564733CAF15C}.Debug|x86.ActiveCfg = Debug|Win32 + {5FAA2373-0F7C-4CD9-BA01-564733CAF15C}.Debug|x86.Build.0 = Debug|Win32 + {5FAA2373-0F7C-4CD9-BA01-564733CAF15C}.Release|x64.ActiveCfg = Release|x64 + {5FAA2373-0F7C-4CD9-BA01-564733CAF15C}.Release|x64.Build.0 = Release|x64 + {5FAA2373-0F7C-4CD9-BA01-564733CAF15C}.Release|x86.ActiveCfg = Release|Win32 + {5FAA2373-0F7C-4CD9-BA01-564733CAF15C}.Release|x86.Build.0 = Release|Win32 + {E65BD110-0E5A-44EE-B304-314596C38A9F}.Debug|x64.ActiveCfg = Debug|x64 + {E65BD110-0E5A-44EE-B304-314596C38A9F}.Debug|x64.Build.0 = Debug|x64 + {E65BD110-0E5A-44EE-B304-314596C38A9F}.Debug|x86.ActiveCfg = Debug|Win32 + {E65BD110-0E5A-44EE-B304-314596C38A9F}.Debug|x86.Build.0 = Debug|Win32 + {E65BD110-0E5A-44EE-B304-314596C38A9F}.Release|x64.ActiveCfg = Release|x64 + {E65BD110-0E5A-44EE-B304-314596C38A9F}.Release|x64.Build.0 = Release|x64 + {E65BD110-0E5A-44EE-B304-314596C38A9F}.Release|x86.ActiveCfg = Release|Win32 + {E65BD110-0E5A-44EE-B304-314596C38A9F}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {95AA5051-90E8-4FA2-8BC0-4437FF90275A} + EndGlobalSection +EndGlobal diff --git a/DeDuplication/DeDuplication/DeDuplication.cpp b/DeDuplication/DeDuplication/DeDuplication.cpp new file mode 100644 index 0000000..352dc7c --- /dev/null +++ b/DeDuplication/DeDuplication/DeDuplication.cpp @@ -0,0 +1 @@ +#include "DeDuplication.h" diff --git a/DeDuplication/DeDuplication/DeDuplication.h b/DeDuplication/DeDuplication/DeDuplication.h new file mode 100644 index 0000000..f9e36dd --- /dev/null +++ b/DeDuplication/DeDuplication/DeDuplication.h @@ -0,0 +1,54 @@ +#pragma once +#include +#include +#include +#include +#include +using namespace std; + +struct MyData +{ + wstring name; + wstring md5; +}; + +class DeDuplication +{ +private: + static 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; + }; + +public: + void finally() + { + wcout.imbue(locale("", LC_CTYPE)); + wcout << L"----------µ÷ÕûÇ°--------" << endl; + for (const auto& it : m_vec) + wcout << L"[it.name]" << it.name << L"[it.md5]" << it.md5 << endl; + + + set s(&DeDuplication::cmpSort); + for (unsigned i = 0; i < m_vec.size(); ++i) + s.insert(m_vec[i]); + + m_vec.assign(s.begin(), s.end()); + + wcout << L"----------µ÷Õûºó--------" << endl; + for (const auto& it : m_vec) + wcout << L"[it.name]" << it.name << L"[it.md5]" << it.md5 << endl; + }; + +private: + vector m_vec = { { L"a1.exe", L"B9204B6362A65C248950D5A41F341DCC"}, + { L"a2.exe", L"B9204B6362A65C248950D5A41F341DCC"}, + { L"b1.exe", L"C96D1AA5D314954417C28A645ED72665"}, + { L"b2.exe", L"C96D1AA5D314954417C28A645ED72665"} }; +}; diff --git a/DeDuplication/DeDuplication/DeDuplication.vcxproj b/DeDuplication/DeDuplication/DeDuplication.vcxproj new file mode 100644 index 0000000..443273f --- /dev/null +++ b/DeDuplication/DeDuplication/DeDuplication.vcxproj @@ -0,0 +1,151 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {5faa2373-0f7c-4cd9-ba01-564733caf15c} + DeDuplication + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + + \ No newline at end of file diff --git a/DeDuplication/DeDuplication/DeDuplication.vcxproj.filters b/DeDuplication/DeDuplication/DeDuplication.vcxproj.filters new file mode 100644 index 0000000..b1b13d6 --- /dev/null +++ b/DeDuplication/DeDuplication/DeDuplication.vcxproj.filters @@ -0,0 +1,30 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + + + Header Files + + + \ No newline at end of file diff --git a/DeDuplication/DeDuplication/main.cpp b/DeDuplication/DeDuplication/main.cpp new file mode 100644 index 0000000..cd3e109 --- /dev/null +++ b/DeDuplication/DeDuplication/main.cpp @@ -0,0 +1,13 @@ +// DeDuplication.cpp : This file contains the 'main' function. Program execution begins and ends there. +// + +#include +#include "DeDuplication.h" + +int main() +{ + DeDuplication deDup; + deDup.finally(); + + return 0; +} \ No newline at end of file diff --git a/DeDuplication/Unique/Unique.cpp b/DeDuplication/Unique/Unique.cpp new file mode 100644 index 0000000..8ca94e4 --- /dev/null +++ b/DeDuplication/Unique/Unique.cpp @@ -0,0 +1,92 @@ +// Unique.cpp : This file contains the 'main' function. Program execution begins and ends there. +// + +#include +#include +#include +#include +#include +using namespace std; + +/* ¶ÔÓ¦½Ì³Ì + * STL Öиø vector È¥ÖصÄÈýÖÖ·½·¨: https://xmuli.blog.csdn.net/article/details/126322011 + * STL µÄ std::set ´´½¨×Ô¶¨Òå½á¹¹ÌåµÄ¶ÔÏ󣬶¨ÒåÑϸñÈõÐòµÄ±È½Ïº¯Êý: https://xmuli.blog.csdn.net/article/details/126354597 +*/ + +struct MyData +{ + wstring name; + wstring md5; + + // ·½Ê½Ò»£ºÖØÔØÀà < º¯Êý + //bool operator<(const MyData& d) const noexcept { + // return md5 < d.md5; + //} +}; + +// ·½Ê½¶þ£º¶Ô×Ô¶¨Òå½á¹¹ÌåÀàÐÍ£¬×Ô¶¨Òå±È½Ïº¯Êý _Pr ¡¾±¾´Î²ÉÓõķ½Ê½¡¿ +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; +}; + +// ·½Ê½¶þ±äÖÖ£ºÍ¬¶þ£¬µ«²ÉÓà Lambda ±í´ïʽ£¨Ä³Ð©²¿·ÖËã·¨ÖлáʧЧ£© +auto cmpSortLambda = [](const MyData& d1, const MyData& d2) { + return d1.md5 < d2.md5; +}; + +// ·½Ê½Èý£ºº¯Êý¶ÔÏó£¬ÂÔ¡£¸üУ¬±¾À´ÊÇдÁËÂԵģ¬½á¹ûÒâÍâ´Ë´¦ÓÐÒâÍâµÄÊÕ»ñ +struct cmpSortObj +{ + bool operator()(const MyData& d1, const MyData& d2) const { + return d1.md5 < d2.md5; + } +}; + +// ************* std::unique µÄ×Ô¶¨Òå±È½Ïº¯Êý ************* +bool cmpUnique(const MyData& d1, const MyData& d2) { + return d1.md5 == d2.md5; // ·ÇÑϸñ¶ÔÏóÏàµÈ£¬¿ÉÒԴﵽĿ±ê£¬µ«»áÄÚ´æй© + //return d1.md5 == d2.md5 && d1.name == d2.name; // Âú×ãÓï·¨ºÍ±àÒ룬µ«²»Âú×ãʵ¼ÊÐèÇó +}; + +int main() +{ + vector 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"----------µ÷ÕûÇ°--------" << endl; + for (const auto& it : vec) + wcout << L"[it.name]" << it.name << L"[it.md5]" << it.md5 << endl; + + // ************* ¡ºÒ»¡»vector, sort + unique ************* + //sort(vec.begin(), vec.end(), cmpSort); + //auto ite = std::unique(vec.begin(), vec.end(), cmpUnique); + //vec.erase(ite, vec.end()); + + // ************* ¡º¶þ¡»vector + set£¨ÊÖ¶¯¸³Öµ£© ************* + //set s; // "º¯Êý¶ÔÏó"´´½¨ set ¶ÔÏó --> OK + set s(&cmpSort); // "¶¨ÒåÆÕͨº¯Êý + decltype"´´½¨ set ¶ÔÏó --> OK: ±àÒë¡¢ÔËÐгɹ¦£»µ÷ÊÔÒ²³É¹¦ + //set s; // --> Error: ±àÒë¡¢ÔËÐгɹ¦£»µ÷ÊÔ»áʧ°Ü + + for (unsigned i = 0; i < vec.size(); ++i) + s.insert(vec[i]); // ±àÒëʱ£¬±¾ÐпÉÑéÖ¤ÉÏÃæ´´½¨ std::set µÄ½á¹û + vec.assign(s.begin(), s.end()); + + // ************* ¡ºÈý¡»vector + set£¨¹¹Ô캯Êý£© ************* + // ЧÂʲ»È硺¶þ¡»£¬¹ÊÂÔ + + wcout << L"----------µ÷Õûºó--------" << endl; + for (const auto& it : vec) + wcout << L"[it.name]" << it.name << L"[it.md5]" << it.md5 << endl; + + return 0; +} \ No newline at end of file diff --git a/DeDuplication/Unique/Unique.vcxproj b/DeDuplication/Unique/Unique.vcxproj new file mode 100644 index 0000000..e6f6ce3 --- /dev/null +++ b/DeDuplication/Unique/Unique.vcxproj @@ -0,0 +1,148 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {e65bd110-0e5a-44ee-b304-314596c38a9f} + Unique + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + stdcpp17 + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/DeDuplication/Unique/Unique.vcxproj.filters b/DeDuplication/Unique/Unique.vcxproj.filters new file mode 100644 index 0000000..5302142 --- /dev/null +++ b/DeDuplication/Unique/Unique.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + \ No newline at end of file