Created
February 7, 2025 13:30
-
-
Save OriLiMu/0424b009e5b80e2140f92338e24560a2 to your computer and use it in GitHub Desktop.
uniqueRemoveDuplicated
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <algorithm> // 包含 std::unique | |
#include <iostream> | |
#include <vector> | |
int main() { | |
// 初始化向量 | |
std::vector<int> vec = {1, 2, 3, 4, 5, 1}; | |
// 对 vect 进行排序(虽对于 std::unique 不是必需的,但通常来讲最好先排序) | |
std::sort(vec.begin(), vec.end()); | |
// 使用 std::unique 移除重复元素 | |
auto last = std::unique(vec.begin(), vec.end()); | |
// 删除重复后面的元素 | |
vec.erase(last, vec.end()); | |
// 输出结果 | |
for (int num : vec) { | |
std::cout << num << " "; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment