Commit 3c934946 authored by captainwong's avatar captainwong

fix std_util

parent d5dff368
...@@ -12,7 +12,7 @@ namespace jlib { ...@@ -12,7 +12,7 @@ namespace jlib {
* @note C must be a container of type T * @note C must be a container of type T
*/ */
template <class Container, class ElementType> template <class Container, class ElementType>
bool is_contain(const typename Container& c, const typename ElementType& t) { inline bool is_contain(const Container& c, const ElementType& t) {
for (const auto& i : c) { for (const auto& i : c) {
if (i == t) { return true; } if (i == t) { return true; }
} }
...@@ -24,7 +24,7 @@ bool is_contain(const typename Container& c, const typename ElementType& t) { ...@@ -24,7 +24,7 @@ bool is_contain(const typename Container& c, const typename ElementType& t) {
* @note All 和 Sub 必须为同样类型的容器(不支持initializer-list) * @note All 和 Sub 必须为同样类型的容器(不支持initializer-list)
*/ */
template <class Container> template <class Container>
typename Container get_other(const typename Container& All, const typename Container& Sub) { Container get_other(const Container& All, const Container& Sub) {
Container res, tmp; Container res, tmp;
std::copy(Sub.begin(), Sub.end(), std::back_inserter(tmp)); std::copy(Sub.begin(), Sub.end(), std::back_inserter(tmp));
for (const auto& i : All) { for (const auto& i : All) {
...@@ -47,7 +47,7 @@ typename Container get_other(const typename Container& All, const typename Conta ...@@ -47,7 +47,7 @@ typename Container get_other(const typename Container& All, const typename Conta
* @brief t 是 v 的子集,返回 v 内 t 的补集 * @brief t 是 v 的子集,返回 v 内 t 的补集
*/ */
template <class V> template <class V>
std::vector<std::wstring> get_other(const typename V& v, const std::wstring& t) { std::vector<std::wstring> get_other(const V& v, const std::wstring& t) {
std::vector<std::wstring> ret = {}; std::vector<std::wstring> ret = {};
for (const auto& i : v) { if (i != t) { ret.push_back(i); } } for (const auto& i : v) { if (i != t) { ret.push_back(i); } }
return ret; return ret;
...@@ -57,7 +57,7 @@ std::vector<std::wstring> get_other(const typename V& v, const std::wstring& t) ...@@ -57,7 +57,7 @@ std::vector<std::wstring> get_other(const typename V& v, const std::wstring& t)
* @brief t 是 v 的子集,返回 v 内 t 的补集 * @brief t 是 v 的子集,返回 v 内 t 的补集
*/ */
template <class V> template <class V>
std::vector<std::string> get_other(const typename V& v, const std::string& t) { std::vector<std::string> get_other(const V& v, const std::string& t) {
std::vector<std::string> ret = {}; std::vector<std::string> ret = {};
for (const auto& i : v) { if (i != t) { ret.push_back(i); } } for (const auto& i : v) { if (i != t) { ret.push_back(i); } }
return ret; return ret;
......
This diff is collapsed.
#include "../../jlib/util/std_util.h" #include "std_util.h"
#include <assert.h> #include <assert.h>
int main() int main()
......
...@@ -76,6 +76,7 @@ ...@@ -76,6 +76,7 @@
<Optimization>Disabled</Optimization> <Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode> <ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(DEVLIBS)\jlib;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Console</SubSystem> <SubSystem>Console</SubSystem>
......
#pragma once
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
namespace jlib {
/**
* @brief check if Container contains ElementType t
* @note C must be a container of type T
*/
template <class Container, class ElementType>
inline bool is_contain(const Container& c, const ElementType& t) {
for (const auto& i : c) {
if (i == t) { return true; }
}
return false;
}
/**
* @brief Sub 是 All 的子集,返回 All 内 Sub 的补集
* @note All 和 Sub 必须为同样类型的容器(不支持initializer-list)
*/
template <class Container>
Container get_other(const Container& All, const Container& Sub) {
Container res, tmp;
std::copy(Sub.begin(), Sub.end(), std::back_inserter(tmp));
for (const auto& i : All) {
bool in_Sub = false;
for (auto iter = tmp.begin(); iter != tmp.end(); iter++) {
if (*iter == i) {
tmp.erase(iter);
in_Sub = true;
break;
}
}
if (!in_Sub) {
res.push_back(i);
}
}
return res;
}
/**
* @brief t 是 v 的子集,返回 v 内 t 的补集
*/
template <class V>
std::vector<std::wstring> get_other(const V& v, const std::wstring& t) {
std::vector<std::wstring> ret = {};
for (const auto& i : v) { if (i != t) { ret.push_back(i); } }
return ret;
}
/**
* @brief t 是 v 的子集,返回 v 内 t 的补集
*/
template <class V>
std::vector<std::string> get_other(const V& v, const std::string& t) {
std::vector<std::string> ret = {};
for (const auto& i : v) { if (i != t) { ret.push_back(i); } }
return ret;
}
}
#include "std_util.h"
#include <assert.h>
int main()
{
// is_contain
{
std::string s = "12345";
for (auto c : s) {
assert(jlib::is_contain(s, c));
}
}
// get_other
{
std::vector<int> All{ 1, 2, 3, 4, 5 };
std::vector<int> Sub{ 1,2,3 };
auto other = jlib::get_other(All, Sub);
assert(other == std::vector<int>({ 4, 5 }));
}
// get_other
{
std::vector<std::wstring> all{ L"123", L"abc", L"def", L"ghi" };
std::wstring sub(L"123");
auto other = jlib::get_other(all, sub);
assert(other == std::vector<std::wstring>({ L"abc", L"def", L"ghi" }));
}
// get_other
{
std::vector<std::string> all{ "123", "abc", "def", "ghi" };
std::string sub("123");
auto other = jlib::get_other(all, sub);
assert(other == std::vector<std::string>({ "abc", "def", "ghi" }));
}
}
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|ARM">
<Configuration>Debug</Configuration>
<Platform>ARM</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|ARM">
<Configuration>Release</Configuration>
<Platform>ARM</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|ARM64">
<Configuration>Debug</Configuration>
<Platform>ARM64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|ARM64">
<Configuration>Release</Configuration>
<Platform>ARM64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x86">
<Configuration>Debug</Configuration>
<Platform>x86</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x86">
<Configuration>Release</Configuration>
<Platform>x86</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClInclude Include="std_util.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="test_stdutil.cpp" />
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{c5d81d57-c53b-4571-9a42-9620c5be7919}</ProjectGuid>
<Keyword>Linux</Keyword>
<RootNamespace>test_stdutil_linux</RootNamespace>
<MinimumVisualStudioVersion>15.0</MinimumVisualStudioVersion>
<ApplicationType>Linux</ApplicationType>
<ApplicationTypeRevision>1.0</ApplicationTypeRevision>
<TargetLinuxPlatform>Generic</TargetLinuxPlatform>
<LinuxProjectType>{2238F9CD-F817-4ECC-BD14-2524D2669B35}</LinuxProjectType>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'" Label="Configuration">
<UseDebugLibraries>true</UseDebugLibraries>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM'" Label="Configuration">
<UseDebugLibraries>false</UseDebugLibraries>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x86'" Label="Configuration">
<UseDebugLibraries>true</UseDebugLibraries>
<RemoteRootDir>~/vsprojects</RemoteRootDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x86'" Label="Configuration">
<UseDebugLibraries>false</UseDebugLibraries>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<UseDebugLibraries>true</UseDebugLibraries>
<RemoteRootDir>~/vsprojects</RemoteRootDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<UseDebugLibraries>false</UseDebugLibraries>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'" Label="Configuration">
<UseDebugLibraries>false</UseDebugLibraries>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'" Label="Configuration">
<UseDebugLibraries>true</UseDebugLibraries>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings" />
<ImportGroup Label="Shared" />
<ImportGroup Label="PropertySheets" />
<PropertyGroup Label="UserMacros" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x86'">
<ClCompile>
<AdditionalIncludeDirectories>/root/jlib;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<CppLanguageStandard>c++17</CppLanguageStandard>
</ClCompile>
</ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets" />
</Project>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x86'">
<RemoteTarget>-2055866198;192.168.1.166 (username=, port=22, authentication=Password)</RemoteTarget>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<RemoteTarget>-2055866198;192.168.1.166 (username=, port=22, authentication=Password)</RemoteTarget>
</PropertyGroup>
</Project>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment