C++ Library Extensions 2022.12.09
To help learn modern C++ programming
014-ncr.cpp
Go to the documentation of this file.
1#include <tpf_ncrnpr.hpp>
2#include <tpf_output.hpp>
3
4using namespace tpf;
5using namespace tpf::ncrnpr;
6
8{
10 auto nl = output::nl();
11 auto nL = output::nL();
12
13 using ncr_t = combination;
14
15 ncr_t cb{7, 3};
16
17 stream << cb << " - " << ncr(7, 3) << nL;
18
19 stream << cb.increment_nr() << " - " << ncr(8, 4) << nl;
20
21 stream << cb.decrement_nr() << " - " << ncr(7, 3) << nl;
22
23 stream << cb.increment_n() << " - " << ncr(8, 3) << nl;
24
25 stream << cb.decrement_n() << " - " << ncr(7, 3) << nl;
26
27 stream << cb.increment_r() << " - " << ncr(7, 4) << nl;
28
29 stream << cb.decrement_r() << " - " << ncr(7, 3) << nl;
30
31}
33{
35 auto nl = output::nl();
36 auto nL = output::nL();
37
38 std::vector<std::string> vct_fruits{"apple", "banana", "cherry"};
39 // vct_fruits = tpf::reverse_order(vct_fruits);
40
41 std::deque<std::string> dqe_fruits{"apple", "banana", "cherry"};
42
43 std::list<std::string> lst_fruits{"apple", "banana", "cherry"};
44
45 int n = (int)vct_fruits.size();
46
47 for(int r = 0; r <= n; ++r)
48 {
49 auto max = ncr(n, r);
50
51 for(size_t m_th = 0; m_th < max; ++m_th)
52 {
53 stream << enumerate_combination<std::vector>(m_th, dqe_fruits, r) << " - "
54 << enumerate_combination<std::vector>(m_th, vct_fruits, r) << " - "
55 << enumerate_combination<std::vector>(m_th, lst_fruits, r) <<nl;
56 }
57 }
58}
59
61{
63 auto nl = output::nl();
64 auto nL = output::nL();
65
66 size_t n = 4;
67
68 for(size_t r = 0; r <= n; ++r)
69 {
70 auto max = npr(n, r);
71
72 for(size_t m_th = 0; m_th < max; ++m_th)
73 {
74 stream << enum_permutation(n, r, m_th)
75 << " - " << enum_permutation_list(n, r, m_th) << nl;
76 }
77 }
78}
79
81{
83 auto nl = output::nl();
84 auto nL = output::nL();
85
86 std::vector<std::string> vct_fruits{"apple", "banana", "cherry"};
87
88 std::deque<std::string> dqe_fruits{"apple", "banana", "cherry"};
89
90 size_t n = vct_fruits.size();
91
92 for(size_t r = 0; r <= n; ++r)
93 {
94 auto max = npr(n, r);
95
96 for(size_t m_th = 0; m_th < max; ++m_th)
97 {
98 stream << enumerate_permutation(m_th, vct_fruits, r)
99 << " - " << enumerate_permutation(m_th, dqe_fruits, r) << nl;
100 }
101 }
102}
103
104int main()
105{
107
108 auto nl = output::nl();
109 auto nL = output::nL();
110
112
113 // examples_enum_permutation();
114 // examples_enum_combination();
116
117 std::vector<std::string> v{"apple", "banana", "peach"};
118
119 std::vector<std::reference_wrapper<std::string>> vr{v[0], v[1], v[2]};
120
121 stream << vr <<" - 사랑해요!! 나는 김창희" << nl;
122
123}
void examples_enum_permutations()
Definition: 014-ncr.cpp:60
void examples_ncr_npr()
Definition: 014-ncr.cpp:7
void examples_enumerate_permutation()
Definition: 014-ncr.cpp:80
void examples_enum_combination()
Definition: 014-ncr.cpp:32
int main()
Definition: 014-ncr.cpp:104
auto nl
auto nL
Definition: 020-random.cpp:21
tpf::sstream stream
void load_default_locale(bool ShowLocaleName=false)
Load system default locale for string conversion.
Implements nCr, nPr.
Definition: tpf_ncrnpr.hpp:64
enable_if_all_in_list_t< types::type_list_t< Type1, Type2 >, integral_list_t, ReturnType > npr(Type1 nn, Type2 rr)
Definition: tpf_ncrnpr.hpp:428
enable_if_all_in_list_t< types::type_list_t< Type1, Type2 >, integral_list_t, ReturnType > ncr(Type1 nn, Type2 rr)
Definition: tpf_ncrnpr.hpp:390
std::vector< T > enum_permutation(T n, T r, T m_th)
std::vector< int > enumerate_permutation(std::vector< int > e, RType r, MType m_th)
std::vector< T > enum_permutation_list(T n, T r, T mth)
Includes subnamespace conversion.
Definition: 31-visit.cpp:7
Stream output operators << are implemented.