aboutsummaryrefslogtreecommitdiff
path: root/challenge-180/deadmarshal/cpp
diff options
context:
space:
mode:
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;
+}