Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Added C++ vector manipulation snippets. #209

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

Closed
AnkushRoy-code wants to merge 4 commits into quicksnip-dev:main from AnkushRoy-code:main
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
Fixed problems
  • Loading branch information
AnkushRoy-code committed Jan 7, 2025
commit aa74801803bc43a3f6141c468e2ca6910230340c
6 changes: 5 additions & 1 deletion snippets/cpp/vector-manipulation/remove-duplicates.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ tags: vector,remove,duplicate
#include <algorithm>
#include <vector>

void removeDublicates(std::vector<int> &input)
void removeDuplicates(std::vector<int> &input)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Add noexcept since it won't throw exceptions.

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

// 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}
```
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to include an usage section as described in the our guidelines. See other snippets for reference.

Copy link
Author

@AnkushRoy-code AnkushRoy-code Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry. I didn't see that I will do that

7 changes: 7 additions & 0 deletions snippets/cpp/vector-manipulation/remove-n-occurrences.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,11 @@ void removeOccurrences(std::vector<int>& vec, int n) {
vec.end()
);
}

// 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}

```
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to include an usage section as described in the our guidelines. See other snippets for reference.

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /