aboutsummaryrefslogtreecommitdiff
path: root/challenge-180/deadmarshal/cpp
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-09-02 11:08:44 +0100
committerGitHub <noreply@github.com>2022-09-02 11:08:44 +0100
commit8162d93f3469587093f2c5a0d6e8cbdef9434e47 (patch)
treebe5db0f4e83e316b47fbcc702ebfa6979e8d395d /challenge-180/deadmarshal/cpp
parent91d665102ab1b10f30b41bcf87195be8565053ae (diff)
parent29b273fc146113e127bd1664dd8d198d4ad2dcbd (diff)
downloadperlweeklychallenge-club-8162d93f3469587093f2c5a0d6e8cbdef9434e47.tar.gz
perlweeklychallenge-club-8162d93f3469587093f2c5a0d6e8cbdef9434e47.tar.bz2
perlweeklychallenge-club-8162d93f3469587093f2c5a0d6e8cbdef9434e47.zip
Merge pull request #6683 from deadmarshal/challenge180
challenge180
Diffstat (limited to 'challenge-180/deadmarshal/cpp')
-rw-r--r--challenge-180/deadmarshal/cpp/ch-1.cpp37
-rw-r--r--challenge-180/deadmarshal/cpp/ch-2.cpp26
2 files changed, 63 insertions, 0 deletions
diff --git a/challenge-180/deadmarshal/cpp/ch-1.cpp b/challenge-180/deadmarshal/cpp/ch-1.cpp
new file mode 100644
index 0000000000..389fdd96e6
--- /dev/null
+++ b/challenge-180/deadmarshal/cpp/ch-1.cpp
@@ -0,0 +1,37 @@
+#include<cstdio>
+#include<vector>
+#include<string>
+#include<unordered_map>
+
+char *first_unique_character(const std::string& str)
+{
+ char *buffer = new char[39];
+ std::vector<char> chars{str.begin(), str.end()};
+ std::unordered_map<char, unsigned> hash{};
+ for(const char& c : str) hash[c]++;
+ for(std::size_t i = 0; i < chars.size(); ++i)
+ if(hash[chars[i]] == 1)
+ {
+ std::snprintf(buffer,
+ 39,
+ "%zu as '%c' is the first unique character",
+ i,
+ chars[i]);
+ break;
+ }
+ return buffer;
+}
+
+int main(int argc, char *argv[])
+{
+ if(argc < 2)
+ {
+ fprintf(stderr, "No arg(s) provided!");
+ exit(1);
+ }
+ char *buffer = first_unique_character(argv[1]);
+ printf("%s\n", buffer);
+ delete buffer;
+
+ return 0;
+}
diff --git a/challenge-180/deadmarshal/cpp/ch-2.cpp b/challenge-180/deadmarshal/cpp/ch-2.cpp
new file mode 100644
index 0000000000..1ae978920c
--- /dev/null
+++ b/challenge-180/deadmarshal/cpp/ch-2.cpp
@@ -0,0 +1,26 @@
+#include<iostream>
+#include<vector>
+#include<algorithm>
+
+int main()
+{
+ std::vector<int> n{1,4,2,3,5};
+ std::vector<int> n2{9,0,6,2,3,8,5};
+ int i = 3, i2 = 4;
+ auto removed = std::remove_if(n.begin(),
+ n.end(),
+ [&i](int num){return num <= i;});
+ n.erase(removed, n.end());
+ for(const int& item : n)
+ std::cout << item << ' ';
+ std::cout << '\n';
+
+ auto removed2 = std::remove_if(n2.begin(),
+ n2.end(),
+ [&i2](int num){return num <= i2;});
+ n2.erase(removed2, n2.end());
+ for(const int& item : n2)
+ std::cout << item << ' ';
+
+ return 0;
+}