Advertisement
Guest User

Untitled

a guest
Feb 24th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. vector<int>v(3);
  9. vector<int>w(3);
  10. vector<int>x(3);
  11. for(int i = 0; i < 2; i++)
  12. {
  13. v[i] = i;
  14. w[i] = i;
  15. x[i] = i;
  16. cout << v[i] << " " << w[i] << " " << x[i] << endl;
  17.  
  18. }
  19. return 0;
  20. }
  21.  
  22. 0 0 0
  23. 1 1 1
  24. 2 2 2
  25.  
  26. for (int i = 1; i <= 3; ++i)
  27. std::cout << i << ' ' << i << ' ' << i << 'n';
  28.  
  29. std::vector<int> vec = { 1, 3, 5, 7, 9, 2, 4, 6, 8 };
  30. int elements_on_this_line = 0,
  31. elements_to_output_per_line = 3;
  32. for (int i: vec) {
  33. std::cout << i;
  34. elements_on_this_line++;
  35. if (elements_on_this_line == elements_to_output_per_line) {
  36. std::cout << 'n';
  37. elements_on_this_line = 0;
  38. } else {
  39. std::cout << ' ';
  40. }
  41. }
  42.  
  43. #include <iostream>
  44. #include <vector>
  45.  
  46. int main()
  47. {
  48. std::vector<int> vec { 1, 3, 5, 7, 9, 2, 4, 6, 8};
  49. auto it = vec.begin();
  50. for (int y = 0; y < 3; ++y)
  51. {
  52. for (int x = 0; x < 3; ++x)
  53. {
  54. std::cout << *it++ << ' ';
  55. }
  56. std::cout << 'n';
  57. }
  58. }
  59.  
  60. #include <iostream>
  61.  
  62. #include <vector>
  63. using namespace std;
  64. int main()
  65. {
  66. vector<vector<int> > v;
  67. int k = 0;
  68. for ( int i = 0; i < 5; i++ ) {
  69. v.push_back ( vector<int>() );
  70. for ( int j = 0; j < 5; j++ )
  71. v[i].push_back ( k++ );
  72. }
  73. for ( int i = 0; i < 5; i++ )
  74. {
  75. for ( int j = 0; j < 5; j++ )
  76. cout<<v[i][j] <<' ';
  77. cout<<'n';
  78. }
  79. }
  80.  
  81. int main() {
  82. std::vector<int>v = {1,2,3};
  83. for(auto& i : v)
  84. std::cout << i << " " << i << " " << i << std::endl;
  85. }
  86.  
  87. #ifndef SQUAREMATRIX_H
  88. #define SQUAREMATRIX_H
  89.  
  90. #include <vector>
  91.  
  92. class SquareMatrix
  93. {
  94. public:
  95. SquareMatrix(int rank);
  96.  
  97. int getValue(int xIndex, int yIndex);
  98. void setValue(int xIndex, int yIndex, int value);
  99.  
  100. void print();
  101.  
  102. private:
  103. int rank_;
  104. std::vector<int> data_;
  105. };
  106.  
  107. #endif
  108.  
  109. #include "SquareMatrix.h"
  110.  
  111. #include <iostream>
  112.  
  113. SquareMatrix::SquareMatrix(int rank): rank_(rank), data_(std::vector<int>(rank*rank, 0))
  114. {
  115. }
  116.  
  117. int SquareMatrix::getValue(int xIndex, int yIndex)
  118. {
  119. return data_[xIndex*rank_ + yIndex];
  120. }
  121.  
  122. void SquareMatrix::setValue(int xIndex, int yIndex, int value)
  123. {
  124. data_[xIndex*rank_ + yIndex] = value;
  125. }
  126.  
  127. void SquareMatrix::print()
  128. {
  129. // Print all the lines
  130. for(int y=0; y<rank_; y++)
  131. {
  132. // Print all values in one line
  133. for(int x=0; x<rank_; x++)
  134. {
  135. std::cout << this->getValue(x, y) << " ";
  136. }
  137. std::cout << std::endl;
  138. }
  139. }
  140.  
  141. #include "SquareMatrix.h"
  142.  
  143. int main(int argc, char* argv[])
  144. {
  145. SquareMatrix m(3);
  146. m.print();
  147.  
  148. system("pause");
  149. return 0;
  150. }
  151.  
  152. 0 0 0
  153. 0 0 0
  154. 0 0 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement