Commit 6cd157fb authored by captainwong's avatar captainwong

fix std_util

parent fe6e59e2
...@@ -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 C, class T> template <class C, class T>
inline bool is_contain(const C& c, const T& t) { inline bool is_contain(const typename C& c, const typename T& 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 @@ inline bool is_contain(const C& c, const T& t) { ...@@ -24,7 +24,7 @@ inline bool is_contain(const C& c, const T& t) {
* @note All 和 Sub 必须为同样类型的容器(不支持initializer-list) * @note All 和 Sub 必须为同样类型的容器(不支持initializer-list)
*/ */
template <class C> template <class C>
inline C get_other(const C & All, const C& Sub) { inline typename C get_other(const typename C & All, const typename C& Sub) {
C res, tmp; C 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,9 +47,9 @@ inline C get_other(const C & All, const C& Sub) { ...@@ -47,9 +47,9 @@ inline C get_other(const C & All, const C& Sub) {
* @brief t 是 v 的子集,返回 v 内 t 的补集 * @brief t 是 v 的子集,返回 v 内 t 的补集
*/ */
template <class V> template <class V>
std::vector<std::wstring> get_other(const V& v, const std::wstring& t) { std::vector<std::wstring> get_other(const typename V& v, const std::wstring& t) {
std::vector<std::wstring> ret = {}; std::vector<std::wstring> ret = {};
for (auto i : v) { if (i != t) { ret.push_back(t); } } for (const auto& i : v) { if (i != t) { ret.push_back(i); } }
return ret; return ret;
} }
...@@ -57,9 +57,9 @@ std::vector<std::wstring> get_other(const V& v, const std::wstring& t) { ...@@ -57,9 +57,9 @@ std::vector<std::wstring> get_other(const 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 V& v, const std::string& t) { std::vector<std::string> get_other(const typename V& v, const std::string& t) {
std::vector<std::string> ret = {}; std::vector<std::string> ret = {};
for (auto i : v) { if (i != t) { ret.push_back(t); } } for (const auto& i : v) { if (i != t) { ret.push_back(i); } }
return ret; return ret;
} }
......
...@@ -3,8 +3,35 @@ ...@@ -3,8 +3,35 @@
int main() int main()
{ {
std::vector<int> All{ 1, 2, 3,4,5 }; // is_contain
std::vector<int> Sub{ 1,2,3 }; {
auto other = jlib::get_other(All, Sub); std::string s = "12345";
assert(other == std::vector<int>({4, 5})); 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" }));
}
} }
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