Advertisement
Guest User

C++ dereferencing problem

a guest
Apr 26th, 2011
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. using namespace std;
  4.  
  5.  
  6. int main() {
  7.  
  8.     list<int> L1;
  9.     list<int> L2;
  10.  
  11.     L1.push_back(1);
  12.     L1.push_back(3);
  13.     L1.push_back(8);
  14.     L1.push_back(12);
  15.  
  16.     L2.push_back(3);
  17.     L2.push_back(4);
  18.     L2.push_back(8);
  19.  
  20.     listIntersection(L1, L2);
  21.  
  22.     system("pause");
  23.     return 0;
  24.  
  25.  
  26. }
  27.  
  28.  
  29.  
  30. // ALTHOUGH IT'S A TEMPLATE, I'M JUST USING INTS, SO I CAN USE < > etc.
  31. template<typename T>
  32. list<T> listIntersection(const list<T>& L1, const list<T>& L2) {
  33.  
  34.     if ( L1.empty() && L2.empty() ) {
  35.         cout << "Returning an empty list because the two arugment lists were empty\n\n";
  36.         list<int> L3;
  37.         return L3;
  38.     }
  39.  
  40.  
  41.     list<T>::const_iterator itr1 = L1.begin();
  42.     list<T>::const_iterator itr2 = L2.begin();
  43.  
  44.     // Create New List
  45.     list<int> L3;
  46.  
  47.     for (itr1; itr1 != L1.end(); itr1++) {
  48.  
  49.         while ( *itr2 < *itr1 ) {
  50.                itr2++;
  51.         }
  52.         if ( (*itr1 == *itr2) && (itr2 != L2.end()) ) {
  53.             int tmp = *itr1;
  54.             L3.push_back(tmp);
  55.         }
  56.     }
  57.     return L3;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement