在 C++ 中從向量中刪除重複項
本文將介紹如何在 C++ 中去除向量中的重複項。
使用 std::unique
和 std::vector::erase
函式從 C++ 向量中刪除重複項
std::unique
函式是 STL 演算法的一部分,它本質上實現了對排序範圍的重複元素刪除操作。每次刪除後元素都會移動,並且該函式返回新形成的範圍的最後迭代器。我們在以下示例程式碼中使用的 std::unique
過載採用兩個迭代器引數來表示範圍的開始和結束。在這種情況下,我們還使用 std::sort
演算法對給定的 vector
進行排序,然後再使用 std::unique
對其進行處理。最後,呼叫 erase
成員函式來修改原始陣列大小以適應新形成的向量。
#include <iostream>
#include <vector>
#include <iterator>
#include <iomanip>
#include <algorithm>
using std::cout; using std::endl;
using std::vector; using std::copy;
using std::setw; using std::left;
int main() {
vector<int> int_vec = {10, 23, 10, 324, 10, 10, 424,
649, 110, 110, 129, 40, 424};
cout << left << setw(10) << "vec: ";
copy(int_vec.begin(), int_vec.end(),
std::ostream_iterator<int>(cout,"; "));
cout << endl;
std::sort(int_vec.begin(), int_vec.end());
auto last = std::unique(int_vec.begin(), int_vec.end());
int_vec.erase(last, int_vec.end());
cout << left << setw(10) << "vec: ";
copy(int_vec.begin(), int_vec.end(),
std::ostream_iterator<int>(cout,"; "));
cout << endl;
return EXIT_SUCCESS;
}
輸出:
vec: 10; 23; 10; 324; 10; 10; 424; 649; 110; 110; 129; 40; 424;
vec: 10; 23; 40; 110; 129; 324; 424; 649;
實現上述解決方案的另一種方法是使用 resize
函式而不是 erase
呼叫。resize
函式修改向量的元素計數。因此,我們可以使用 distance
呼叫傳遞新形成的向量元素的計算計數,而 resize
將縮小向量。
#include <iostream>
#include <vector>
#include <iterator>
#include <iomanip>
#include <algorithm>
using std::cout; using std::endl;
using std::vector; using std::copy;
using std::setw; using std::left;
int main() {
vector<int> int_vec = {10, 23, 10, 324, 10, 10, 424,
649, 110, 110, 129, 40, 424};
cout << left << setw(10) << "vec: ";
copy(int_vec.begin(), int_vec.end(),
std::ostream_iterator<int>(cout,"; "));
cout << endl;
std::sort(int_vec.begin(), int_vec.end());
auto last = std::unique(int_vec.begin(), int_vec.end());
int_vec.resize(std::distance(int_vec.begin(), last));
cout << left << setw(10) << "vec: ";
copy(int_vec.begin(), int_vec.end(),
std::ostream_iterator<int>(cout,"; "));
cout << endl;
return EXIT_SUCCESS;
}
輸出:
vec: 10; 23; 10; 324; 10; 10; 424; 649; 110; 110; 129; 40; 424;
vec: 10; 23; 40; 110; 129; 324; 424; 649;
使用 std::set
容器從 C++ 向量中刪除重複項
或者,std::set
容器可用於從向量中刪除重複元素。請注意,std::set
在內部儲存給定型別的唯一物件,因此我們需要從向量元素構造一個。一旦 set
用需要排序的向量元素初始化,我們可以從原始 vector
物件呼叫 assign
函式來儲存來自 set
物件的唯一元素。
#include <iostream>
#include <vector>
#include <iterator>
#include <iomanip>
#include <algorithm>
#include <set>
using std::cout; using std::endl;
using std::vector; using std::copy;
using std::setw; using std::left;
using std::set;
int main() {
vector<int> int_vec = {10, 23, 10, 324, 10, 10, 424,
649, 110, 110, 129, 40, 424};
cout << left << setw(10) << "vec: ";
copy(int_vec.begin(), int_vec.end(),
std::ostream_iterator<int>(cout,"; "));
cout << endl;
set<int> int_set(int_vec.begin(), int_vec.end());
int_vec.assign(int_set.begin(), int_set.end());
cout << left << setw(10) << "vec: ";
copy(int_vec.begin(), int_vec.end(),
std::ostream_iterator<int>(cout,"; "));
cout << endl;
return EXIT_SUCCESS;
}
輸出:
vec: 10; 23; 10; 324; 10; 10; 424; 649; 110; 110; 129; 40; 424;
vec: 10; 23; 40; 110; 129; 324; 424; 649;
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn