Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 17th, 2012  |  syntax: None  |  size: 2.91 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Vectors of pointers to other vector's elements
  2. void Function(std::vector<type>* aa)
  3.        
  4. void Function(std::vector<type>* aa)
  5. {
  6.     std::vector<type*> temp; //to this vector I filter out data and by changning
  7.     //values of this vector I want to autmatically change values of aa vector
  8. }
  9.        
  10. void Announce_Event(std::vector<Event>& foo)
  11. {
  12.     std::vector<Event> current;
  13.     tm current_time = {0,0,0,0,0,0,0,0,0};
  14.     time_t thetime;
  15.     thetime = time(NULL);
  16.     localtime_s(&current_time, &thetime);
  17.     for (unsigned i = 0; i < foo.size(); ++i) {
  18.         if (foo[i].day == current_time.tm_mday &&
  19.             foo[i].month == current_time.tm_mon &&
  20.             foo[i].year == current_time.tm_year+1900)
  21.         {
  22.             current.push_back(foo[i]);
  23.         }
  24.     }
  25.     std::cout << current.size() << std::endl;
  26.     current[0].title = "Changed"; //<-- this is suppose to change value.
  27. }
  28.        
  29. void Func(std::vector<type> & aa)
  30. {
  31.     std::vector<type*> temp;
  32.  
  33.     // I wish <algorithm> had a 'transform_if'    
  34.     for(int i=0; i<aa.size(); ++i)
  35.     {
  36.         if( some_test(aa[i]) )
  37.             temp.push_back(&aa[i])
  38.     }
  39.  
  40.     // This leaves temp with pointers to some of the elements of aa.
  41.     // Only those elements which passed some_test().  Now any modifications
  42.     // to the dereferenced pointers in temp will modify those elements
  43.     // of aa.  However, keep in mind that if elements are added or
  44.     // removed from aa, it may invalidate the pointers in temp.
  45. }
  46.        
  47. void Function(std::vector<type>& aa)
  48.        
  49. void Function(std::vector<type>& aa)
  50. {
  51.     std::vector<type>& temp = aa;
  52.  
  53.     // if you now append something to temp, it is also appended to aa
  54.     aa.push_back(type());
  55. }
  56.        
  57. void Oglos_Wydarzenie(std::vector<Wydarzenie>& zmienna)
  58. {
  59.     std::vector<Wydarzenie *> obecne;
  60.     tm AktualnyCzas = {0,0,0,0,0,0,0,0,0};
  61.     time_t czas;
  62.     czas = time(NULL);
  63.     localtime_s(&AktualnyCzas,&czas);
  64.     for (unsigned i = 0; i < zmienna.size(); ++i) {
  65.         if (zmienna[i].dzien == AktualnyCzas.tm_mday &&
  66.             zmienna[i].miesiac ==  AktualnyCzas.tm_mon &&
  67.             zmienna[i].rok == AktualnyCzas.tm_year+1900)
  68.         {
  69.             obecne.push_back(&zmienna[i]);
  70.         }
  71.     }
  72.     std::cout << obecne.size() << std::endl;
  73.     obecne[0]->tytul = "Changed"; //<-- this is suppose to change value.
  74. }
  75.        
  76. void Oglos_Wydarzenie(std::vector<Wydarzenie>* zmienna)
  77. {
  78.     std::vector<Wydarzenie *> obecne;
  79.     tm AktualnyCzas = {0,0,0,0,0,0,0,0,0};
  80.     time_t czas;
  81.     czas = time(NULL);
  82.     localtime_s(&AktualnyCzas,&czas);
  83.     for (unsigned i = 0; i < zmienna->size(); ++i) {
  84.         if ((*zmienna)[i].dzien == AktualnyCzas.tm_mday &&
  85.             (*zmienna)[i].miesiac ==  AktualnyCzas.tm_mon &&
  86.             (*zmienna)[i].rok == AktualnyCzas.tm_year+1900)
  87.         {
  88.             obecne.push_back(&((*zmienna)[i]));
  89.         }
  90.     }
  91.     std::cout << obecne.size() << std::endl;
  92.     obecne[0]->tytul = "Changed"; //<-- this is suppose to change value.
  93. }