fork download
  1. // Copyright Evgeny Panasyuk 2012.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://w...content-available-to-author-only...t.org/LICENSE_1_0.txt)
  5.  
  6. #include <iostream>
  7. #include <ostream>
  8. #include <vector>
  9. #include <string>
  10. using namespace std;
  11.  
  12. // ____________________________________________________________________________________ //
  13.  
  14. struct record
  15. {
  16. string name;
  17. int age;
  18. };
  19. struct ref_record
  20. {
  21. string &name;
  22. int &age;
  23. };
  24. class scattered_record
  25. {
  26. vector<string> name;
  27. vector<int> age;
  28. public:
  29. unsigned size()
  30. {
  31. return unsigned(name.size() | age.size() | 0);
  32. }
  33. void push_back(const record &rec)
  34. {
  35. name.push_back(rec.name);
  36. age.push_back(rec.age);
  37. }
  38. ref_record operator[](unsigned index)
  39. {
  40. ref_record temp = { name[index], age[index], };
  41. return temp;
  42. }
  43. };
  44.  
  45. // ____________________________________________________________________________________ //
  46.  
  47. int main()
  48. {
  49. scattered_record vecs;
  50. {
  51. record temp={"One",1}; vecs.push_back(temp);
  52. }
  53. {
  54. record temp={"Two",2}; vecs.push_back(temp);
  55. }
  56. {
  57. record temp={"Three",3}; vecs.push_back(temp);
  58. }
  59. for(unsigned i=0,size=vecs.size();i!=size;++i)
  60. {
  61. cout << vecs[i].name << " " << vecs[i].age << endl;
  62. vecs[i].name+="_postfix";
  63. vecs[i].age+=10;
  64. }
  65. cout << string(16,'_') << endl;
  66. for(unsigned i=0,size=vecs.size();i!=size;++i)
  67. cout << vecs[i].name << " " << vecs[i].age << endl;
  68. }
  69.  
Success #stdin #stdout 0.02s 2860KB
stdin
Standard input is empty
stdout
One 1
Two 2
Three 3
________________
One_postfix 11
Two_postfix 12
Three_postfix 13