Advertisement
AlexandraCatana

STL

Dec 16th, 2017
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <cstring>
  5.  
  6. using namespace std;
  7.  
  8. template <class Value>
  9. class Network
  10. {
  11. vector<Value> networkVector;
  12. size_t rotationPivot;
  13.  
  14. public:
  15. Network(const Value* arr, const int & offset)
  16. {
  17. const size_t sizeArr = sizeof(arr) / sizeof(arr[0]);
  18.  
  19. for(size_t it = 0; it < sizeArr; ++it)
  20. networkVector.push_back(arr[it]);
  21.  
  22. rotateWithOffset(offset);
  23. CalculateRotationPivot(offset);
  24. }
  25.  
  26. void rotateWithOffset(size_t offset)
  27. {
  28. offset %= networkVector.size();
  29. rotate(networkVector.begin(), next(networkVector.begin(), offset), networkVector.end());
  30. }
  31.  
  32. void CalculateRotationPivot(const size_t & offset)
  33. {
  34. (offset % networkVector.size() == 0) ? rotationPivot = 0 : rotationPivot = networkVector.size() - offset % networkVector.size();
  35. }
  36.  
  37. void DisplayNetwork() const
  38. {
  39. for (const auto& val : networkVector)
  40. cout << val << " ";
  41.  
  42. cout << "\n";
  43. }
  44.  
  45. void DisplayRotationPivot()
  46. {
  47. cout << rotationPivot << "\n";
  48. }
  49.  
  50. size_t Count() const
  51. {
  52. return networkVector.size();
  53. }
  54.  
  55. const Value & GetData(size_t index) const
  56. {
  57. return networkVector[index];
  58. }
  59.  
  60. auto BeginNodes() const
  61. {
  62. return networkVector.cbegin();
  63. }
  64.  
  65. auto EndNodes() const
  66. {
  67. return networkVector.cend();
  68. }
  69.  
  70. size_t GetNode(const Value & data) const
  71. {
  72. if (networkVector[rotationPivot] == data)
  73. return (size_t)rotationPivot;
  74.  
  75. auto lw1 = lower_bound(networkVector.begin(), networkVector.begin() + rotationPivot, data);
  76. if ( lw1 != networkVector.end() && *lw1 == data)
  77. return distance(networkVector.begin(), lw1);
  78.  
  79. auto lw2 = lower_bound(networkVector.begin() + rotationPivot, networkVector.end(), data);
  80. if (lw2 != networkVector.end() && *lw2 == data)
  81. return distance(networkVector.begin(), lw2);
  82.  
  83. cout << "The node was not found, we return the position of the end of the vector\n";
  84. return networkVector.size();
  85. }
  86.  
  87. };
  88.  
  89. int main()
  90. {
  91. char* str = "ABCDEFGH";
  92. const size_t offset = 20;
  93. Network<char> *p = new Network<char>(str, offset);
  94.  
  95. p->DisplayNetwork();
  96. auto it1 = p->BeginNodes();
  97. auto it2 = p->EndNodes();
  98. //std::cout << *(p->BeginNodes()) << " " << *(p->EndNodes()) << "\n"; - eroare
  99. //cout << *it1 << " " << *it2 << "\n"; -eroare
  100.  
  101. p->DisplayRotationPivot();
  102. cout << p->GetNode('1') << "\n";
  103.  
  104. system("pause");
  105. return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement