Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
J
jlib
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
captainwong
jlib
Commits
6cd157fb
Commit
6cd157fb
authored
Aug 25, 2020
by
captainwong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix std_util
parent
fe6e59e2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
10 deletions
+37
-10
std_util.h
jlib/util/std_util.h
+6
-6
test_stdutil.cpp
test/test_stdutil/test_stdutil.cpp
+31
-4
No files found.
jlib/util/std_util.h
View file @
6cd157fb
...
@@ -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
;
}
}
...
...
test/test_stdutil/test_stdutil.cpp
View file @
6cd157fb
...
@@ -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"
}));
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment