FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpp-lib/src/alg.cpp at master · dst/cpp-lib · GitHub
dst
cpp-lib
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
cpp-lib
/
src
/
alg.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
83 lines (66 loc) · 1.97 KB
Breadcrumbs
cpp-lib
/
src
/
alg.cpp
Copy path
File metadata and controls
83 lines (66 loc) · 1.97 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* alglib.cpp
*
* Created on: Feb 25, 2012
* Author: dst
*/
#
include
"
common.h
"
#
include
<
algorithm
>
#
include
<
cassert
>
#
include
<
numeric
>
#
include
<
set
>
#
include
<
string
>
#
include
<
vector
>
using
namespace
std
;
/*
next_permutation
* Don’t forget to ensure that the elements in a container are sorted before
* your first call to next_permutation(...). Their initial state should form
* the very first permutation; otherwise, some permutations will not be checked
*
* Example:
* do {
*
* } while (next_permutation(v.begin(), v.end()));
*/
//
add some elements to set
//
vector<int> v;
//
set<int> s;
//
copy(v.begin(), v.end(), inserter(s, s.begin()));
//
search first not equal adjacent elements, return iter to first of them
//
1 1 1 1 2 2 2 3
//
*
//
vector<int> v;
//
vector<int>::iterator eltIter = adjacent_find(v.begin(), v.end(), not_equal_to<int>());
inline
void
replaceElts
(vector<
int
>& v,
int
oldVale,
int
newValue) {
replace
(v.
begin
(), v.
end
(), oldVale, newValue);
}
inline
long
long
sum
(
const
vector<
int
>& v) {
return
accumulate
(v.
begin
(), v.
end
(),
0LL
);
}
inline
long
long
product
(
const
vector<
int
>& v) {
return
accumulate
(v.
begin
(), v.
end
(),
1LL
, multiplies<
int
>());
}
//
Get rid of duplicates in vector and sort it
inline
vector<
int
>
removeDuplicates
(
const
vector<
int
>& v) {
set<
int
>
s
(v.
begin
(), v.
end
());
vector<
int
>
sorted
(s.
begin
(), s.
end
());
return
sorted;
}
//
TODO: remove one/all elts from vector
//
tests
inline
void
alg
() {
//
replaceElts
auto
v =
VECTOR
(
1
,
2
,
3
);
replaceElts
(v,
1
,
2
);
assert
(
count
(v.
begin
(), v.
end
(),
1
) ==
0
);
assert
(
count
(v.
begin
(), v.
end
(),
2
) ==
2
);
assert
(
count
(v.
begin
(), v.
end
(),
3
) ==
1
);
//
sum
assert
(
sum
(
VECTOR
(
1
,
2
,
3
,
4
)) ==
10
);
//
product
assert
(
product
(
VECTOR
(
1
,
2
,
3
,
4
)) ==
24
);
//
removeDuplicates
auto
duplicated =
VECTOR
(
1
,
1
);
auto
uniq =
removeDuplicates
(duplicated);
assert
(
count
(uniq.
begin
(), uniq.
end
(),
1
) ==
1
);
}
Back
|
FazBrowse Home
|
New Git URL