# Error when trying to join vectors

2

I want to join the vectors `vector1` and `vector2` into a single vector `vectorUnion`

``````#include<iostream>
using namespace std;
void muestraVector(int v[], int longitud);
void unionVectores(int vector1[], int longit1, int vector2[], int longit2,
int vectorUnion[],int longit3);
int main() {

const int MAXIMOV = 5;
const int MAXIMOV1 = 5;
const int MAXIMOV2 = 6;
const int O = 11;

int vector1[MAXIMOV][MAXIMOV1] = { { 1, 3, 5, 6, 9 },{ 1, 2, 3, 4, 5 },{ 7, 8, 9, 10, 11 },{ 1, 3, 5, 7, 9 },{ 10,17,25,29,32 } };
int vector2[MAXIMOV][MAXIMOV2] = { { 2, 4, 6, 7, 8, 13 },{ 6, 7, 8, 9, 10, 11 },{ 1, 2, 3, 4, 5, 6 },{ 2, 4, 6, 8, 10, 12 },{ 1,4,6,45,46,47 } };

int vectorUnion[MAXIMOV][O];

for (int v = 0; v < MAXIMOV; v++)
{
cout << "VECTOR1" << endl;
muestraVector(vector1[v], MAXIMOV1);
cout << "VECTOR2" << endl;
muestraVector(vector2[v], MAXIMOV2);
cout << "Union de los vectores : " << endl;
unionVectores(vector1[v], MAXIMOV1, vector2[v], MAXIMOV2,
vectorUnion[v], O);
muestraVector(vectorUnion[v], O);

}

system("pause");

return 0;
}

void muestraVector(int v[], int longitud)
{
cout << "[";
if (longitud > 0)
{
cout << v[0];
}
for (int i = 1; i < longitud; i++)
cout << "," << v[i];
cout << "]" << endl;
}

void unionVectores(int vector1[], int longit1, int vector2[], int longit2,
int vectorUnion[],int longit3) {

int x = 0;
int z = 0;
for (int i = 0; i < (longit1 + longit2); i+2) {

vectorUnion[i] = vector1[x];
x++;
}
for (int j = 1; j < (longit1 + longit2); j + 2) {

vectorUnion[j] = vector2[z];
z++;
}
}
``````

Then I thought about doing a function that would allow me to join the two vectors so that if for example

``````vector1 : {1,3,5,6,9}
vector2 : {2,4,6,7,8,13}
``````

the united vector would be:

``````{1,2,3,4,5,6,6,7,8,9,13}
``````

but you send me an error when I try to change the value of

``````vectorUnion[i] = vector1[x]  // se produjo una excepción:infracción de acceso de lectura.
``````

I guess this can not be done, but I do not know why you can not, could you guide me?

asked by Chariot 23.02.2018 в 20:25
source

2

In this loop:

``````for (int i = 0; i < (longit1 + longit2); i+2) {
vectorUnion[i] = vector1[x];
x++;
}
``````

We have that both `i` and `x` will move in the range (0,10). `vectorUnion[i]` is not going to give you problems because `vectorUnion` admits 11 integers, but `vector1` is a vector of only 5 elements and that implies that this loop performs 6 readings out of the memory of `vector1` .

With the preceding loop, exactly the same thing happens and modern Operating Systems have the bad habit of preventing a process from accessing memory that does not belong to it, that is why it prevents the corruption of the memory of other applications and the way it has to prevent this from happening is killing the process that accesses the memory ... in this case your application.

The solution is as simple as rearranging those loops:

• The first loop can only iterate in the range 0..4
• The second loop will iterate in the range 0..5.
• The index to `vectorUnion` must be shared by both loops:

Something like this:

``````int i;
for(i=0; i<longit1; ++i )
vectorUnion[i] = vector1[i];

for(int x=0; x<longit2; ++x, ++i )
vectorUnion[i] = vector2[x];
``````

Or, if you prefer with pointers:

``````int* ptr = vectorUnion;
for(int i=0; i<longit1; ++i, ++ptr )
*ptr = vector1[i];

for(int i=0; i<longit2; ++i, ++ptr )
*ptr = vector2[i];
``````

source
0

Since you are working with 1 formations of known size at compile time, I would advise you to use template functions to perform the copy, a join vector function could look like this:

``````template <typename tipo, std::size_t tamanyo_a, std::size_t tamanyo_b>
void unir_vectores(tipo (&vector_a)[tamanyo_a], tipo (&vector_b)[tamanyo_b], tipo (&destino)[tamanyo_a + tamanyo_b])
{
auto iterador = std::copy(std::begin(vector_a), std::end(vector_a), std::begin(destino));
``````int a[10]{}, b[11]{}, c[12]{};