C ++ 2D vector and operations

How to create 2D vectorin C ++ and find it lengthand coordinates?

In this case, how are vector elements filled with values?

Thank.

+3
source share
3 answers

If your goal is to do math, use Boost :: uBLAS . This library has many linear algebra functions and is likely to be much faster than anything you create manually.

If you are a masochist and want to stick std::vector, you need to do something like the following:

std::vector<std::vector<double> > matrix;
matrix.resize(10);
matrix[0].resize(20);
// etc
+5
source

You have several options. The simplest is a primitive two-dimensional array:

int *mat = new int[width * height];

, std::fill():

std::fill(mat, mat + width * height, 42);

, std::generate() std::generate_n():

int fn() { return std::rand(); }

// ...
std::generate(mat, mat + width * height, fn);

delete , :

delete[] mat;

, , :

struct matrix {
    matrix(int w, int h);
    matrix(const matrix& m);
    matrix& operator=(const matrix& m);
    void swap(const matrix& m);
    ~matrix();
};

// ...
matrix mat(width, height);

, , - . boost::multi_array.

+3

(S) He wants vectors, like in physics.

either roll your own as an exercise:

class Vector2d
{
  public:
    // basic math (length: pythagorean theorem, coordinates: you are storing those)
  private: float x,y;
};

or use libraries like Eigen that have Vector2f defined

+1
source

Source: https://habr.com/ru/post/1788842/


All Articles