Commit 6cd157fb authored by captainwong's avatar captainwong

fix std_util

parent fe6e59e2
......@@ -12,7 +12,7 @@ namespace jlib {
* @note C must be a container of type 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) {
if (i == t) { return true; }
}
......@@ -24,7 +24,7 @@ inline bool is_contain(const C& c, const T& t) {
* @note All 和 Sub 必须为同样类型的容器(不支持initializer-list)
*/
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;
std::copy(Sub.begin(), Sub.end(), std::back_inserter(tmp));
for (const auto& i : All) {
......@@ -47,9 +47,9 @@ inline C get_other(const C & All, const C& Sub) {
* @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> get_other(const typename V& v, const std::wstring& t) {
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;
}
......@@ -57,9 +57,9 @@ std::vector<std::wstring> get_other(const V& v, const std::wstring& t) {
* @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> get_other(const typename V& v, const std::string& t) {
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;
}
......
......@@ -3,8 +3,35 @@
int main()
{
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}));
// 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" }));
}
}
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