Create a dynamic matrix based on data from other C ++ functions

1

Good evening. I'm thinking about the vector library and one of the utilities that I see is great is that you can create a vector that is a variable

Example:

int main()
{
int a=5;
vector<double> vec(a,0);
}

That would be equivalent to doing:

vec[a];

Sentence is forbidden in C ++.

My doubt is that I do not find a similar idea to extend this concept to matrices like this example

int main()
{
int a=3;
int b=4;
int resultado;
resultado=suma(a,b);

//Crear una matriz a partir de estos resultados tipo m[resultado][a];
}
int suma(int a, int b)
{
return(a+b);
}

I know I could use "new" but the question is if there is another possibility of the same style as "vector". I have looked at CPPreference but I have not found what I'm looking for.

Greetings.

EDIT:

After this question I have another totally different one:

Imagine that I am running a function and there comes a time when I get a series of dynamic arrays and I want to return one of them. Example:

[...]
a=[1,1]
b=[2,2]
[...]
resultadovec=[1,1,2,2]
resultadomat=[1,1;2,2]

Can I pass this result to the main function? Keep in mind that the dimensions and values of this array (vectors and matrices) I have obtained in this function after several calculations. I think that it can not be done directly but I would like to confirm it. The idea if you can not finally return the size of the vector and matrix that can generate, create that vector and matrix (here comes the previous doubt) and then execute the function again (its second part) so that I return values numerics.

    
asked by Alvaro 31.05.2017 в 22:57
source

1 answer

1

To create matrices, you do not need anything more than a std::vector< > . It is not necessary to complicate more.

The humans , mentally, we think of a matrix of 4 * 4 like something like this:

  

A | A | A | A
   B | B | B | B
   C | C | C | C
   D | D | D | D

However, , that organization is not the one used internally by a computer. That same matrix, in memory, is organized like this:

  

A | A | A | A | B | B | B | B | C | C | C | C | D | D | D | D

In other words: for the computer, a matrix of 4 * 4 is exactly the same as a vector of 16 elements.

Therefore, to access the last element of the matrix, we could do

matriz[3][3] = XX

or this, which is the same:

vector[15] = XX

This is extendable to any number of dimensions ; it does not matter if it's 4 * 4 , or 3 * 3 * 3 , or 4 * 6 * 3 * 5 , or any other combination.

The only inconvenient is that some clarity is lost, because the indexes start at 0 and not at 1, but it's a matter of getting used to it.

Regarding your second question, the answer is just as simple: if you want to return a local fix to your function, you can not .

The fixes are actually pointers ; If you can return a pointer, but as it is a local variable to your function, the pointer you return is meaningless as soon as you execute the return XX; . It becomes a kind of ghost pointer , which points to a valid address (the memory addresses do not change), but the content ... becomes undefined ; your original content may remain there ... or it may not be there. Or, what's worse, it can continue there and disappear at any time.

    
answered by 01.06.2017 в 00:21