#include <algorithm>
#include <iostream>
using namespace std;
int main(void) {
int table1[] = { 3, 5, 1, 2, 4, 7 };
int table2[] = { 8, 9, 11, 1, 14, 33 };
auto it = find_first_of(begin(table1), end(table1), begin(table2), end(table2));
if(it == end(table1)) {
cout << "Nie znaleziono takiego samego elementu w obu tablicach" << endl;
} else {
cout << "Znaleziono element o wartości " << *it << " w pierwszej tablicy." << endl;
}
}
- Jeśli masz wyjście to nie upieraj się na tablicę (lepiej vector)
- Usilnie radzę dokładnie przejrzeć nagłówek algorithm. Tam są rozwiązania (prawie) wszystkich tego typu problemów.
- Jak chcesz szukać następnych powtórzonych elementów to można także tak:
#include <algorithm>
#include <iostream>
using namespace std;
int main(void) {
int table1[] = { 3, 5, 1, 2, 4, 7 };
int table2[] = { 8, 9, 11, 1, 14, 2 };
auto it = find_first_of(begin(table1), end(table1), begin(table2), end(table2));
while(it != end(table1)) {
cout << "Znaleziono element o wartości " << *it << " w pierwszej tablicy." << endl;
it = find_first_of(++it, end(table1), begin(table2), end(table2));
}
}
Oczywiście kompilować C++11 :-)