Skip to content

Commit c2c68f5

Browse files
committed
add c++ language notes
1 parent bb3ac2b commit c2c68f5

File tree

7 files changed

+166
-1
lines changed

7 files changed

+166
-1
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
DIRS_UNSORTED= rpi \
22
linux \
33
brave_browser \
4-
c
4+
c \
5+
cpp
56

67
DIRS=$(sort $(DIRS_UNSORTED))
78

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ List
66

77
- [brave_browser](brave_browser/brave_browser_notes.md)
88
- [c](c/c_notes.md)
9+
- [cpp](cpp/cpp_notes.md)
910
- [linux](linux/linux_notes.md)
1011
- [rpi](rpi/rpi_notes.md)
1112

build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# AUTOGENERATED!! DO NOT EDIT
22
make NOTE_NAME=brave_browser_notes -C brave_browser -f brave_browser.mk --no-print-directory
33
make NOTE_NAME=c_notes -C c -f c.mk --no-print-directory
4+
make NOTE_NAME=cpp_notes -C cpp -f cpp.mk --no-print-directory
45
make NOTE_NAME=linux_notes -C linux -f linux.mk --no-print-directory
56
make NOTE_NAME=rpi_notes -C rpi -f rpi.mk --no-print-directory

cpp/cpp.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# AUTOGENERATED!! DO NOT EDIT
2+
include ../constants.mk
3+
include ../common.mk

cpp/cpp_notes.html

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width,initial-scale=1" />
6+
<meta name="author" content="devpogi" />
7+
<link rel="stylesheet" href="../style.css" />
8+
<title>devpogi notes</title>
9+
</head>
10+
<body>
11+
<header>
12+
<a href="../index.html">Home</a>
13+
</header>
14+
15+
<h1 id="c-notes">C++ notes</h1>
16+
17+
<h2 id="custom-compare-function-for-custom-objects">Custom compare function for custom objects</h2>
18+
19+
<pre><code class="language-cpp">&#47;&#47; using custom compare function for custom objects in priority_queue
20+
21+
#include &#60;iostream&#62;
22+
#include &#60;queue&#62;
23+
using namespace std;
24+
25+
struct Node
26+
{
27+
int a;
28+
int b;
29+
};
30+
31+
bool compare(const Node &#38;n, const Node &#38;m)
32+
{
33+
if (n.a &#60; m.a)
34+
return true;
35+
else if (n.a == m.a)
36+
{
37+
if (n.b &#60; m.b)
38+
return true;
39+
}
40+
return false;
41+
}
42+
43+
priority_queue&#60;Node, vector&#60;Node&#62;, decltype(&#38;compare)&#62; buff(compare);
44+
45+
int main()
46+
{
47+
Node arr[] = {{2, 3}, {2, 4}, {2, 1}, {3, 1}, {3, 5}, {1, 3}, {1, 4}, {1, 0}};
48+
for (int i = 0; i &#60; sizeof(arr) &#47; sizeof(arr[0]); i++)
49+
{
50+
buff.push(arr[i]);
51+
}
52+
while (!buff.empty())
53+
{
54+
cout &#60;&#60; "{" &#60;&#60; buff.top().a &#60;&#60; "," &#60;&#60; buff.top().b &#60;&#60; "}\n";
55+
buff.pop();
56+
}
57+
return 0;
58+
}
59+
60+
&#47;*
61+
OUTPUT -
62+
63+
{3,5}
64+
{3,1}
65+
{2,4}
66+
{2,3}
67+
{2,1}
68+
{1,4}
69+
{1,3}
70+
{1,0}
71+
72+
*&#47;
73+
</code></pre>
74+
75+
<h2 id="optimization-reserve-the-size-of-hash-table">Optimization: reserve the size of hash table</h2>
76+
77+
<pre><code class="language-cpp">#define SIZE_OF_HASH_TABLE 20000
78+
unordered_map&#60;string, int&#62; mymap;
79+
mymap.reserve(SIZE_OF_HASH_TABLE);
80+
</code></pre>
81+
82+
<p>This reserves the size of the hash table so that re-hashing doesn&#8217;t occur for at least till <code>SIZE_OF_HASH_TABLE</code> number of elememts. </p>
83+
</body>
84+
</html>

cpp/cpp_notes.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# C++ notes
2+
3+
4+
## Custom compare function for custom objects
5+
6+
```cpp
7+
// using custom compare function for custom objects in priority_queue
8+
9+
#include <iostream>
10+
#include <queue>
11+
using namespace std;
12+
13+
struct Node
14+
{
15+
int a;
16+
int b;
17+
};
18+
19+
bool compare(const Node &n, const Node &m)
20+
{
21+
if (n.a < m.a)
22+
return true;
23+
else if (n.a == m.a)
24+
{
25+
if (n.b < m.b)
26+
return true;
27+
}
28+
return false;
29+
}
30+
31+
priority_queue<Node, vector<Node>, decltype(&compare)> buff(compare);
32+
33+
int main()
34+
{
35+
Node arr[] = {{2, 3}, {2, 4}, {2, 1}, {3, 1}, {3, 5}, {1, 3}, {1, 4}, {1, 0}};
36+
for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
37+
{
38+
buff.push(arr[i]);
39+
}
40+
while (!buff.empty())
41+
{
42+
cout << "{" << buff.top().a << "," << buff.top().b << "}\n";
43+
buff.pop();
44+
}
45+
return 0;
46+
}
47+
48+
/*
49+
OUTPUT -
50+
51+
{3,5}
52+
{3,1}
53+
{2,4}
54+
{2,3}
55+
{2,1}
56+
{1,4}
57+
{1,3}
58+
{1,0}
59+
60+
*/
61+
```
62+
63+
64+
## Optimization: reserve the size of hash table
65+
66+
```cpp
67+
#define SIZE_OF_HASH_TABLE 20000
68+
unordered_map<string, int> mymap;
69+
mymap.reserve(SIZE_OF_HASH_TABLE);
70+
```
71+
72+
This reserves the size of the hash table so that re-hashing doesn't occur for at least till `SIZE_OF_HASH_TABLE` number of elememts.
73+
74+

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<ul>
3232
<li><a href="brave_browser/brave_browser_notes.html">brave_browser</a></li>
3333
<li><a href="c/c_notes.html">c</a></li>
34+
<li><a href="cpp/cpp_notes.html">cpp</a></li>
3435
<li><a href="linux/linux_notes.html">linux</a></li>
3536
<li><a href="rpi/rpi_notes.html">rpi</a></li>
3637
</ul>

0 commit comments

Comments
 (0)