Porównanie 2 wektorów

0

Mam 2 wektory typu string
jeden ma 4 słowa
drugi ma 3
jak porównać czy to samo słowo jest w jednym i drugim, jak sprawdzam poprzez for/while to wywala out of range ;/
nie mam pomysłu ;/

void sprawdz(vector<string> w, vector<string> e) {
	
	for (int i = 0; i < w.size(); i++) {
		if (w[i] == e[i]) {

		}
		else {
			cout << w[i];
		}
	}
}
3

Sam powiedziałeś, że jeden z wektorów ma 3 elementy a drugi 4. Prawdopodobnie robisz pętle ze względu na .size() tego drugiego i masz oczywiste out of range. Powiedz konkretnie co chcesz zrobić, bo ja nie rozumiem nic.

4

http://ideone.com/ClXGEd

#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
#include <set>
using namespace std;

int main(int argc,char *argv[])
  {
   vector<string> a { "aaa","xxx","bbb","ccc"};
   vector<string> b { "aaa","bbb","yyy","ccc"};
   set<string> ab(a.begin(),a.end());
   for(auto s:b) ab.erase(s);
   set<string> ba(b.begin(),b.end());
   for(auto s:a) ba.erase(s);
   for(auto s:ba) ab.insert(s);
   copy(ab.begin(),ab.end(),ostream_iterator<string>(cout," "));
   return 0;
  }
5
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <iterator>
using namespace std;

template<typename Cont>
Cont diff(Cont const &a, Cont const &b) {
    using T = typename Cont::value_type;
    set<T> sa(begin(a), end(a)),
           sb(begin(b), end(b));

    Cont result;
    set_symmetric_difference(
        begin(sa), end(sa),
        begin(sb), end(sb),
        back_inserter(result));
    return result;
}

int main() {
	vector<int> a {1, 2, 3, 4, 5},
                b {1, 3, 6, 7, 4};

    for(int el: diff(a, b)) {
        cout << el << " ";
    }
	return 0;
}

http://ideone.com/oZZPaN

0

Pytacz ma różne rozmiary wektorów, a Wy pokazujecie kod z tym samym rozmiarem :) Pierwsza odpowiedź jest poprawna. Nic dodać, nic ująć.

3

@Juhas Ty tak na powaznie?

przeciez rozwiazanie od @spartanPAGE i od @_13th_Dragon sa uniwersalne. Polecam poczytac o iteratorach...

http://ideone.com/flFH3e

0

Dzięki wszystko zrozumiane do zamknięcia:D

1 użytkowników online, w tym zalogowanych: 0, gości: 1