Skip to content

[Snippets-C++] Added two snippets from #209 #225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
e93bcac
[C++] added snippet
majvax Jan 1, 2025
2a6b1e2
Merge remote-tracking branch 'origin/snippets-c++' into snippets-c++
majvax Jan 1, 2025
aad3f79
[C++] added 3 snippets for string.
majvax Jan 1, 2025
f472a29
Added missing chrono header
majvax Jan 2, 2025
31b43b5
fixed tags
majvax Jan 2, 2025
29112d8
Merge remote-tracking branch 'origin/snippets-c++' into snippets-c++
majvax Jan 2, 2025
4638879
fixed exemple needing std=c++20
majvax Jan 2, 2025
98ddffc
Fixed Usage to accommodate to new guidelines
majvax Jan 2, 2025
aebefde
Fixed all tags + type
majvax Jan 3, 2025
d76dbfd
Revert "Fixed all tags + type"
majvax Jan 3, 2025
c188670
Merge remote-tracking branch 'upstream/main' into snippets-c++
majvax Jan 3, 2025
9364abb
Fixed type and all tags
majvax Jan 3, 2025
3a99176
fixed gh check failing
majvax Jan 3, 2025
c0d6f04
Update consolidated snippets
actions-user Jan 3, 2025
1dd6a8f
Merge branch 'dostonnabotov:main' into snippets-c++
majvax Jan 3, 2025
fce087f
Update consolidated snippets
actions-user Jan 3, 2025
a959e95
Update consolidated snippets
actions-user Jan 3, 2025
4708bd9
Merge remote-tracking branch 'origin/main' into snippets-c++
majvax Jan 4, 2025
112302e
Revert "Merge remote-tracking branch 'origin/main' into snippets-c++"
majvax Jan 6, 2025
60496ec
Merge remote-tracking branch 'origin/main' into snippets-c++
majvax Jan 6, 2025
812842f
Update consolidated snippets
actions-user Jan 6, 2025
1fbaec8
Merge remote-tracking branch 'origin/main' into snippets-c++
majvax Jan 10, 2025
7c0ca05
added two snippets from #209
majvax Jan 10, 2025
da7b75c
resolve check not passing
majvax Jan 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
Title: Filter Vector
Title: Filter
Description: Filters a vector using a predicate function.
Author: majvax
Tags: array,filter,c++23
Expand Down
29 changes: 29 additions & 0 deletions snippets/cpp/vector-manipulation.md/remove-duplicates.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
title: Remove duplicates
description: Removes duplicates from an vector of ints
author: AnkushRoy-code
tags: vector,remove,duplicate
contributor: majvax
---

```cpp
#include <algorithm>
#include <vector>

bool removeDuplicates(std::vector<int> &input) noexcept {
if (input.empty()) return false;
const auto size = input.size();
std::sort(input.begin(), input.end());

auto last = std::unique(input.begin(), input.end()); // remove duplicates
input.erase(last, input.end()); // resize vector and delete the undefined elements

return size != input.size();
}



// Usage:
std::vector<int> vec = {4, 2, 2, 8, 5, 6, 9, 9, 9, 8, 8, 4};
removeDuplicates(vec); // returns {2, 4, 5, 6, 8, 9}
```
39 changes: 39 additions & 0 deletions snippets/cpp/vector-manipulation.md/remove-n-occurences.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
title: Remove n Occurences
description: Removes duplicates from an vector of ints
author: AnkushRoy-code
tags: vector,remove
contributor: majvax
---

```cpp
#include <algorithm>
#include <unordered_map>
#include <vector>

bool removeOccurrences(std::vector<int>& vec, const unsigned int n) noexcept {
if (vec.empty() || n == 0) return false;

const auto size = vec.size();
std::unordered_map<int, int> frequency; // count frequencies
for (int num : vec) {
frequency[num]++;
}

vec.erase( // remove elements with n number of occurrences
std::remove_if(vec.begin(), vec.end(), [&frequency, n](const int x) {
return frequency[x] == n;
}),
vec.end()
);
return size != vec.size();
}



// Usage:
std::vector<int> vec = {4, 2, 4, 8, 5, 6, 8, 8, 4, 3 };

int n = 3; // Remove elements that occur exactly 3 times
removeOccurrences(vec, n); // returns {2, 5, 6, 3}
```
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
Title: Transform Vector
Title: Transform
Description: Transforms a vector using a function.
Author: majvax
Tags: array,transform,c++23
Expand Down
Loading