Skip to content

Instantly share code, notes, and snippets.

@orihomie
Last active September 15, 2017 10:29
Show Gist options
  • Save orihomie/e3792857c5ba568f3da74596b7849cde to your computer and use it in GitHub Desktop.
Save orihomie/e3792857c5ba568f3da74596b7849cde to your computer and use it in GitHub Desktop.
// TestWork.cpp: определяет точку входа для консольного приложения.
//
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <map>
#include <ctime>
#include <algorithm>
using namespace std;
std::vector<int> Vec;
std::map<int, int> Map;
void fillContainers(int _lowerB, int _upperB, int _conSize)
{
for (size_t i = 0; i < _conSize; i++)
{
Map.emplace(pair<int, int>(i, _lowerB + rand() % _upperB));
Vec.emplace_back(_lowerB + rand() % _upperB);
}
}
void removeFromContainers()
{
int removeCount = 1 + rand() % 14;
Vec.erase(Vec.begin(), Vec.begin() + removeCount);
removeCount = 1 + rand() % 5;
int mapSize = Map.size();
auto realRemoveCount = min(removeCount, mapSize);
auto mapIter = Map.begin();
advance(mapIter, realRemoveCount);
Map.erase(Map.begin(), mapIter);
}
void deleteNonDuplicated()
{
vector<int> hasDup;
for (int i = 0; i < Vec.size(); )
{
bool gotDup = false;
if (find(hasDup.begin(), hasDup.end(), Vec[i]) != hasDup.end())
{
i++;
continue;
}
for (auto mapIter = Map.begin(); mapIter != Map.end(); mapIter++)
{
if (Vec[i] == mapIter->second)
{
gotDup = true;
break;
}
}
if (gotDup)
{
hasDup.emplace_back(Vec[i]);
i++;
}
else
{
Vec.erase(Vec.begin() + i);
}
}
for (auto it = Map.cbegin(); it != Map.cend())
{
if (find(hasDup.begin(), hasDup.end(), it->second) == hasDup.end())
{
it = Map.erase(it);
}
else
{
++it;
}
}
//sort(hasDup.begin(), hasDup.end());
}
int main()
{
srand(time(NULL));
fillContainers(1, 9, 15 + rand() % 16);
removeFromContainers();
deleteNonDuplicated();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment