Advertisement
999ms

Untitled

Jul 17th, 2020
1,085
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <random>
  5. #include <algorithm>
  6. #include <cassert>
  7.  
  8. constexpr unsigned int MAX_N = 30; // number of adding elements in map and vector
  9.  
  10. constexpr int MIN_LIMIT_TO_GENERATE_VALUES = 1;
  11. constexpr int MAX_LIMIT_TO_GENERATE_VALUES = 9;
  12.  
  13. constexpr unsigned int MIN_LIMIT_TO_GENERATE_AMOUNT_OF_ITEMS_TO_DELETE = 0;
  14. constexpr unsigned int MAX_LIMIT_TO_GENERATE_AMOUNT_OF_ITEMS_TO_DELETE = 15;
  15.  
  16. constexpr bool DEBUG = false;
  17.  
  18. void LogResult(std::vector<int>& vector) {
  19.     if constexpr (DEBUG) {
  20.         std::cout << "vector constains next data: (index, value)\n";
  21.         for (unsigned int i = 0; i < std::size(vector); ++i) {
  22.             std::cout << "{" << i << ", " << vector[i] << "} ";
  23.         }
  24.         std::cout << '\n';
  25.     }
  26. }
  27.  
  28. void LogResult(std::map<unsigned int, int>& map) {
  29.     if constexpr (DEBUG) {
  30.         std::cout << "map constains next data: (key, value)\n";
  31.         for (const auto& [key, value] : map) {
  32.             std::cout << "{" << key << ", " << value << "} ";
  33.         }
  34.         std::cout << '\n';
  35.     }
  36. }
  37.  
  38. int main() {
  39.     std::vector<int> vector(MAX_N);
  40.     std::map<unsigned int, int> map;
  41.    
  42.     std::mt19937 mt19937_random(std::random_device{}()); // set random-device-seed for mt19937 generator
  43.     std::uniform_int_distribution<int> distributionForGenerating(
  44.                                                     MIN_LIMIT_TO_GENERATE_VALUES,
  45.                                                     MAX_LIMIT_TO_GENERATE_VALUES
  46.                                                 );
  47.     std::uniform_int_distribution<unsigned int> distributionForDeleting(
  48.                                                     MIN_LIMIT_TO_GENERATE_AMOUNT_OF_ITEMS_TO_DELETE,
  49.                                                     MAX_LIMIT_TO_GENERATE_AMOUNT_OF_ITEMS_TO_DELETE
  50.                                                 );
  51.                                                
  52.     auto generateRandomValue = [&mt19937_random] (auto& distribution) {
  53.         return distribution(mt19937_random);
  54.     };
  55.    
  56.     for (unsigned int i = 0; i < MAX_N; ++i) {
  57.         vector[i] = generateRandomValue(distributionForGenerating);
  58.         map[i] = generateRandomValue(distributionForGenerating);
  59.     }
  60.    
  61.     LogResult(vector);
  62.     LogResult(map);
  63.    
  64.     const unsigned int numberOfItemsToDeleteInVector = std::min(MAX_N, generateRandomValue(distributionForDeleting));
  65.     const unsigned int numberOfItemsToDeleteInMap = std::min(MAX_N, generateRandomValue(distributionForDeleting));
  66.    
  67.     std::cout << "Number of items to delete in vector = " << numberOfItemsToDeleteInVector << '\n';
  68.     std::cout << "Number of items to delete in map = " << numberOfItemsToDeleteInMap << '\n';
  69.    
  70.     for (unsigned int i = 0; i < numberOfItemsToDeleteInVector; ++i) { // remove last numberOfItemsToDeleteInVector elements from vector
  71.         vector.pop_back();
  72.     }
  73.    
  74.     for (unsigned int i = 0; i < numberOfItemsToDeleteInMap; ++i) { // remove last numberOfItemsToDeleteInMap elements from map
  75.         map.erase(std::size(map) - 1);
  76.     }
  77.    
  78.     LogResult(vector);
  79.     LogResult(map);
  80.    
  81.     std::vector<bool> indexToDeleteVector(std::size(vector), false);
  82.     std::vector<bool> indexToDeleteMap(MAX_N, false);
  83.    
  84.     std::vector<std::vector<unsigned int>> indexesOfValuesVector(MAX_LIMIT_TO_GENERATE_VALUES + 1); // [value] - contains all indexes i such as vector[i] == value
  85.     std::vector<std::vector<unsigned int>> indexesOfValuesMap(MAX_LIMIT_TO_GENERATE_VALUES + 1); // [value]- contains all indexes i such as map[i] == value
  86.    
  87.     for (unsigned int index = 0; index < std::size(vector); ++index) {
  88.         indexesOfValuesVector[vector[index]].push_back(index);
  89.     }
  90.     for (const auto& [index, value] : map) {
  91.         indexesOfValuesMap[value].push_back(index);
  92.     }
  93.    
  94.     for (int value = MIN_LIMIT_TO_GENERATE_VALUES; value <= MAX_LIMIT_TO_GENERATE_VALUES; ++value) {
  95.         auto& vectorIndexes = indexesOfValuesVector[value];
  96.         auto& mapIndexes = indexesOfValuesMap[value];
  97.        
  98.         while(std::size(vectorIndexes) < std::size(mapIndexes)) {
  99.             indexToDeleteMap[mapIndexes.back()] = true;
  100.             mapIndexes.pop_back();
  101.         }
  102.    
  103.         while(std::size(mapIndexes) < std::size(vectorIndexes)) {
  104.             indexToDeleteVector[vectorIndexes.back()] = true;
  105.             vectorIndexes.pop_back();
  106.         }
  107.     }
  108.        
  109.     {
  110.         unsigned int j = 0;
  111.         for (unsigned int i = 0; i < std::size(vector); ++i) {
  112.             if (!indexToDeleteVector[i]) {
  113.                 std::swap(vector[j], vector[i]);
  114.                 ++j;
  115.             }
  116.         }
  117.         vector.resize(j);
  118.     }
  119.    
  120.     for (unsigned int i = 0; i < MAX_N; ++i) {
  121.         if (indexToDeleteMap[i]) {
  122.             map.erase(i);
  123.         }
  124.     }
  125.    
  126.     LogResult(vector);
  127.     LogResult(map);
  128.    
  129.     {
  130.         std::vector<int> vectorValues = vector;
  131.         std::sort(std::begin(vectorValues), std::end(vectorValues));
  132.        
  133.         std::vector<int> mapValues;
  134.         mapValues.reserve(std::size(map));
  135.         for (auto& [key, value] : map) {
  136.             mapValues.push_back(value);
  137.         }
  138.         std::sort(std::begin(mapValues), std::end(mapValues));
  139.        
  140.         for_each(std::begin(vectorValues), std::end(vectorValues), [&] (const auto& value) { std::cout << value << ' '; });    
  141.         std::cout << '\n';
  142.        
  143.         for_each(std::begin(mapValues), std::end(mapValues), [&] (const auto& value) { std::cout << value << ' '; });    
  144.         std::cout << '\n';
  145.        
  146.        
  147.         assert(std::size(vectorValues) == std::size(mapValues));
  148.         assert(vectorValues == mapValues);
  149.         std::cout << "Correct\n";
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement