Created
January 14, 2025 14:27
-
-
Save ProfAndreaPollini/41ce64996128b47ca346aa7d561732e7 to your computer and use it in GitHub Desktop.
lezioni 2025 01 14
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// scrivere una funzione che dati due vettori, crei un terzo vettore | |
// i cui elementi sono gli elementi comuni dei vettori di partenza senza | |
// ripetizioni | |
#include <algorithm> | |
#include <iostream> | |
#include <vector> | |
bool contains(const std::vector<int> & v, int value) { | |
// ritorna true se value è contenuto in v, altrimenti false | |
for (int i = 0; i < v.size(); i++) { | |
if (v[i] == value) { | |
return true; | |
} | |
} | |
return false; | |
} | |
std::vector<int> merge_unique( | |
const std::vector<int>& nums1, | |
const std::vector<int>& nums2) { | |
std::vector<int> result; | |
for (int i = 0; i < nums1.size(); ++i) { | |
auto value = nums1[i]; | |
if (contains(nums2,value) and !contains(result,value)) { | |
result.push_back(value); | |
} | |
} | |
return result; | |
} | |
void print(const std::vector<int>& v) { | |
for (int i = 0; i < v.size(); i++) { | |
std::cout << v[i] << " "; | |
} | |
std::cout << std::endl; | |
} | |
void task1(const std::vector<int> & nums1, const std::vector<int> & nums2) { | |
std::vector<int> result = merge_unique(nums1, nums2); | |
print(result); | |
} | |
int menu_scelta() { | |
int s; | |
std::cout << "MENU\n"; | |
std::cout << "seleziona: "; | |
std::cin >> s; | |
return s; | |
} | |
void menu_scelta2(int& scelta) { | |
std::cout << "MENU\n"; | |
std::cout << "seleziona: "; | |
std::cin >> scelta; | |
} | |
int main() { | |
std::vector<int> nums1 = {1, 2, 3, 4, 5}; | |
std::vector<int> nums2 = {1, 2, 13, 14, 15}; | |
int scelta = 0; | |
do { | |
//scelta = menu_scelta(); | |
menu_scelta2(scelta); | |
switch (scelta) { | |
case 1: | |
task1(nums1,nums2); | |
break; | |
default: | |
break; | |
} | |
} while(scelta !=0); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment