Advertisement
AntonioVillanueva

Ejercico vectores

Jun 19th, 2025
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.82 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iomanip>
  3.  /*
  4. Antonio Villanueva Segura
  5. Array exercise
  6. You are given two arrays arr1 and arr2, where arr2 always contains integers.
  7. Write a function such that:
  8. For arr1 = ['a', 'a', 'a', 'a', 'a'], arr2 = [2, 4] the function returns ['a', 'a']
  9. For arr1 = [0, 1, 5, 2, 1, 8, 9, 1, 5], arr2 = [1, 4, 7] the function returns [1, 1, 1]
  10. For arr1 = [0, 3, 4], arr2 = [2, 6] the function returns [4]
  11. For arr1=["a","b","c","d"] , arr2=[2,2,2], the function returns ["c","c","c"]
  12. For arr1=["a","b","c","d"], arr2=[3,0,2] the function returns ["d","a","c"]
  13. Note that when an element inside arr2 is greater than the index of the last element of arr1 no element of arr1 should be added to the resulting array. If either arr1 or arr2 is empty, you should return an empty arr (empty list in python, empty vector in c++). Note for c++ use std::vector arr1, arr2.
  14.  
  15.   */
  16.  
  17. #include <iostream>
  18. #include <vector>
  19. using namespace std;
  20.  
  21. template <typename T,typename U>
  22. class Arrays {
  23.  
  24. public:
  25.  
  26.   // Constructeur
  27.   Arrays(const std::vector<T>& a , const std::vector<U>& b) : chaine(a),index(b) {}
  28.  
  29.   void printVector(){
  30.       printVector(chaine);
  31.       printVector(index);    
  32.   }
  33.  
  34.  std::vector<T> cutVector(){
  35.       std::vector<T> tmp;
  36.       for (const auto& i : index) {
  37.           try{
  38.                 chaine.at(i);
  39.                 tmp.push_back (chaine[i]);
  40.             }catch (const std::out_of_range& e){
  41.             }
  42.       }
  43.       return tmp;
  44.   }
  45.  
  46.   template <typename V>
  47.   void printVector (const std::vector<V>& v){ //Print vector  
  48.    
  49.     for (const auto& elem : v) {
  50.       std::cout << elem << ",";
  51.     }      
  52.     std::cout <<" - ";
  53.   }
  54.  
  55.  
  56. private:
  57.    std::vector<T> chaine;
  58.    std::vector<U> index;  
  59. };
  60.  
  61.  
  62.  
  63.  
  64. int main(int argc, char *argv[])
  65. {
  66.  
  67.   std::vector <char> arr1_a  {'a', 'a', 'a', 'a', 'a'};//arr2 = [2, 4] the function returns ['a', 'a']
  68.   std::vector<int>  arr2_a  {2, 4} ;
  69.  
  70.   std::vector <int> arr1_b  {0, 1, 5, 2, 1, 8, 9, 1, 5};// arr2 = [1, 4, 7] the function returns [1, 1, 1]
  71.   std::vector<int> arr2_b {1, 4, 7};
  72.  
  73.   std::vector <int> arr1_c {0, 3, 4};// arr2 = [2, 6] the function returns [4]
  74.   std::vector<int> arr2_c {2, 6};
  75.  
  76.   std::vector <char> arr1_d {'a','b','c','d'} ;// arr2=[2,2,2], the function returns ["c","c","c"]
  77.   std::vector<int> arr2_d {2,2,2};
  78.  
  79.   std::vector <char>  arr1_e {'a','b','c','d'};// arr2=[3,0,2] the function returns ["d","a","c"]
  80.   std::vector<int>  arr2_e {3,0,2};
  81.  
  82.  
  83.   Arrays <char,int> a (arr1_a,arr2_a);
  84.   a.printVector (a.cutVector());
  85.    
  86.   Arrays <int,int> b (arr1_b,arr2_b);
  87.   b.printVector (b.cutVector());  
  88.  
  89.   Arrays <int,int> c (arr1_c,arr2_c);
  90.   c.printVector (c.cutVector());  
  91.  
  92.   Arrays <char,int> d (arr1_d,arr2_d);
  93.   d.printVector (d.cutVector());  
  94.  
  95.   Arrays <char,int> e (arr1_e,arr2_e);
  96.   e.printVector (e.cutVector());  
  97.  
  98.  
  99.  
  100. }
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement