[英]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 。 显然,您需要将值从输入向量复制到结果向量。 但没有重复。
解决办法是
你需要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.