簡體   English   中英

如何通過迭代從向量中刪除重復項?

[英]How to remove duplicates from vector by iteratting though?

我正在為我的計算機工程課做一個作業,我們必須從向量中刪除重復項。 我在別處找到了解決方案,但我不知道如何在不包含算法庫的情況下進行迭代

#include <vector>

using namespace std;

vector<int> deleteRepeats(const vector<int>& nums); // Declaration
void print(const vector<int>& nums);

int main() {
    vector<int> nums;
    vector<int> res;
    // Test 1
    nums = {1, 2, 3, 3, 2};
    res = deleteRepeats(nums);
    print(res); // Should print 1, 2, 3
    // Test 2
    nums = {-1, 2, 4};
    res = deleteRepeats(nums); 
    print(res); // Should print -1, 2, 4
    // Test 3
    nums = {42, 42, 42,};
    res = deleteRepeats(nums); 
    print(res); // Should print 42
    // Test 4
    nums = {};
    res = deleteRepeats(nums); 
    print(res); // Should print a blank

    return 0;
}

vector<int> deleteRepeats(const vector<int>& nums) {
    vector<int> res;
    bool foundRepeat;
    //my code here

    return res;
}

void print(const vector<int>& nums) {
    for (int i = 0; i < nums.size(); i++)
        cout << nums[i] << " ";
    cout << endl;
}

最簡單的方法是創建一個第二std::vector通過然后循環std::vector ,你要從中刪除重復的,並加入到第二std::vector ,如果該項目沒有在第二個存在的std::vector

#include <stdio.h>
#include <vector>
bool DoesVectorContain(const std::vector<int> &vect, int item)
{
    for (int i = 0; i < vect.size(); i++)
    {
        if (vect.at(i) == item)
            return true;
    }
    return false;
}
std::vector<int> RemoveDuplicate(const std::vector<int> &vect)
{
    std::vector<int> newVect;
    for (int i = 0; i < vect.size(); i++)
    {
        //add to newVect if vect.at(i) doesn't exist in newVect
        if (!DoesVectorContain(newVect, vect.at(i)))
            newVect.push_back(vect.at(i));
    }
    return newVect;
}
int main()
{
    std::vector<int> vect = {0, 2, 4, 4, 3, 4, 4, 8, 8, 10, 12};
    std::vector<int> newVect = RemoveDuplicate(vect);
    for (int i = 0; i < newVect.size(); i++)
        printf("%d\n", newVect.at(i));

    return 0;
}

這打印出來

0
2
4
3
8
10
12

您可以使用unordered_set對象,它僅在元素不存在時才允許插入該元素。 插入后,您可以將集合轉換為向量。

該解決方案的平均復雜度為O(N) (但不是在最壞的情況下)

#include <vector>
#include <unordered_set>

std::vector<int> deleteRepeats(const std::vector<int>& nums) {
    std::unordered_set<int> uniq_set;

    for (const auto& element : nums) {
        uniq_set.insert(element);
    }

    return std::vector(uniq_set.begin(), uniq_set.end());
}

所以,要求是不使用算法庫並手動完成。

也沒有問題,因為你的老師已經給你寫了一個強有力的提示:

vector<int> deleteRepeats(const vector<int>& nums) {
    vector<int> res;
    bool foundRepeat;
    //my code here

    return res;
}

您有一個輸入向量nums和一個結果向量res 顯然,您需要將值從輸入向量復制到結果向量。 但沒有重復。

解決辦法是

  • 迭代輸入向量nums中的所有數字
  • 然后將每個數字與結果向量res中的所有數字進行比較
  • 如果您找不到重復 ( foundRepeat ),請將值復制到結果向量中。

你需要2個循環。 外循環將從輸入向量nums 中讀取所有值。 每個值都將與res向量中的所有現有值進行比較。 為此,我們創建了一個內部 llop 並迭代res向量中的所有現有值。 如果外循環中的數字等於內循環中的值,那么我們顯然有重復。 我們不會增加價值。 如果我們沒有重復,那么我們將外部循環中的數字添加到res向量中。

這可以看起來像下面的例子:

#include <iostream>
#include <vector>

using namespace std;

vector<int> deleteRepeats(const vector<int>& nums); // Declaration
void print(const vector<int>& nums);

int main() {
    vector<int> nums;
    vector<int> res;
    // Test 1
    nums = { 1, 2, 3, 3, 2 };
    res = deleteRepeats(nums);
    print(res); // Should print 1, 2, 3
    // Test 2
    nums = { -1, 2, 4 };
    res = deleteRepeats(nums);
    print(res); // Should print -1, 2, 4
    // Test 3
    nums = { 42, 42, 42, };
    res = deleteRepeats(nums);
    print(res); // Should print 42
    // Test 4
    nums = {};
    res = deleteRepeats(nums);
    print(res); // Should print a blank

    return 0;
}

vector<int> deleteRepeats(const vector<int>& nums) {
    vector<int> res;
    bool foundRepeat;
    //my code here

    // Iterate over all nums
    for (unsigned int i = 0; i < nums.size(); ++i) {

        // Get current num at index i
        int num = nums[i];

        // Here we will indicate, if we found a duplicate
        foundRepeat = false;

        // Now search, if this num is already in the result vector
        for (unsigned int k = 0; (k < res.size() && !foundRepeat); k++) {

            // This is the number in the res vector at the current index
            int numInRes = res[k];

            // Check for a repeating (duplicate value)
            if (numInRes == num) {

                // Remeber that we found a duplicate and also stop the loop
                foundRepeat = true;
            }
        }
        // If no duplicate has been found, then add the num to the result
        if (!foundRepeat) res.push_back(num);
    }
    return res;
}

void print(const vector<int>& nums) {
    for (int i = 0; i < nums.size(); i++)
        cout << nums[i] << " ";
    cout << endl;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM