Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- int main()
- {
- vector<int>v(3);
- vector<int>w(3);
- vector<int>x(3);
- for(int i = 0; i < 2; i++)
- {
- v[i] = i;
- w[i] = i;
- x[i] = i;
- cout << v[i] << " " << w[i] << " " << x[i] << endl;
- }
- return 0;
- }
- 0 0 0
- 1 1 1
- 2 2 2
- for (int i = 1; i <= 3; ++i)
- std::cout << i << ' ' << i << ' ' << i << 'n';
- std::vector<int> vec = { 1, 3, 5, 7, 9, 2, 4, 6, 8 };
- int elements_on_this_line = 0,
- elements_to_output_per_line = 3;
- for (int i: vec) {
- std::cout << i;
- elements_on_this_line++;
- if (elements_on_this_line == elements_to_output_per_line) {
- std::cout << 'n';
- elements_on_this_line = 0;
- } else {
- std::cout << ' ';
- }
- }
- #include <iostream>
- #include <vector>
- int main()
- {
- std::vector<int> vec { 1, 3, 5, 7, 9, 2, 4, 6, 8};
- auto it = vec.begin();
- for (int y = 0; y < 3; ++y)
- {
- for (int x = 0; x < 3; ++x)
- {
- std::cout << *it++ << ' ';
- }
- std::cout << 'n';
- }
- }
- #include <iostream>
- #include <vector>
- using namespace std;
- int main()
- {
- vector<vector<int> > v;
- int k = 0;
- for ( int i = 0; i < 5; i++ ) {
- v.push_back ( vector<int>() );
- for ( int j = 0; j < 5; j++ )
- v[i].push_back ( k++ );
- }
- for ( int i = 0; i < 5; i++ )
- {
- for ( int j = 0; j < 5; j++ )
- cout<<v[i][j] <<' ';
- cout<<'n';
- }
- }
- int main() {
- std::vector<int>v = {1,2,3};
- for(auto& i : v)
- std::cout << i << " " << i << " " << i << std::endl;
- }
- #ifndef SQUAREMATRIX_H
- #define SQUAREMATRIX_H
- #include <vector>
- class SquareMatrix
- {
- public:
- SquareMatrix(int rank);
- int getValue(int xIndex, int yIndex);
- void setValue(int xIndex, int yIndex, int value);
- void print();
- private:
- int rank_;
- std::vector<int> data_;
- };
- #endif
- #include "SquareMatrix.h"
- #include <iostream>
- SquareMatrix::SquareMatrix(int rank): rank_(rank), data_(std::vector<int>(rank*rank, 0))
- {
- }
- int SquareMatrix::getValue(int xIndex, int yIndex)
- {
- return data_[xIndex*rank_ + yIndex];
- }
- void SquareMatrix::setValue(int xIndex, int yIndex, int value)
- {
- data_[xIndex*rank_ + yIndex] = value;
- }
- void SquareMatrix::print()
- {
- // Print all the lines
- for(int y=0; y<rank_; y++)
- {
- // Print all values in one line
- for(int x=0; x<rank_; x++)
- {
- std::cout << this->getValue(x, y) << " ";
- }
- std::cout << std::endl;
- }
- }
- #include "SquareMatrix.h"
- int main(int argc, char* argv[])
- {
- SquareMatrix m(3);
- m.print();
- system("pause");
- return 0;
- }
- 0 0 0
- 0 0 0
- 0 0 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement