Posted By

dirkchang on 08/13/12


Tagged


Versions (?)

iterator example 2


 / Published in: C++
 

example for iterator

  1. #include <iostream>
  2. #include <vector>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. struct Tuple {
  7. double attr1;
  8. double attr2;
  9. };
  10.  
  11. int main() {
  12. double a = 1.0;
  13. vector<Tuple> vec(50, Tuple());
  14.  
  15. for(vector<Tuple>::iterator it = vec.begin(); it != vec.end(); ++it) {
  16. it->attr1 = it->attr2 = a++;
  17. }
  18.  
  19. Tuple arr[5][10];
  20. vector<Tuple>::iterator it = vec.begin();
  21. for(int i = 0; i < 5; ++i) {
  22. for(int j = 0; j < 10; ++j) {
  23. arr[i][j] = *it;
  24. ++it;
  25. cout << "(" << setw(2) << arr[i][j].attr1 << "," << setw(2) << arr[i][j].attr2 << ") ";
  26. }
  27. cout << '\n';
  28. }
  29. cout << endl;
  30. }

Report this snippet  

You need to login to post a comment.