Assign a row from one vector to another in c ++

2

I'm doing a simple program in c ++. I need to sort a vector by a value in a column that I have in each row.

The sorting method is simple, but I can not do the row assignment:

void PruebaVector2::ordenar()
{
    int i, j;
    int* temp;
    int alumno[3][2] = { { 1, 3 }, { 3, 5 }, { 1, 2 } };

    for (i = 0; i < 3 - 1 ; i++)
    {
        for (j = i + 1; j < 3; j++)
        {
            if (alumno[i][1] > alumno[j + 1][1])
            {
                temp = alumno[i];               
                alumno[i] = alumno[j];
                alumno[j] = temp;
            }
        }
    }
} 

I'm sorting for a bubble method and assigning alumno[i] = alumno[j]; and alumno[j] = temp; I get an assignment error.

Edition

alumno[i] = alumno[j]; : error C2106: '=': left operand must be l-value
alumno[j] = temp; : error C2440: '=': can not convert from 'char *' to 'char [3]'

    
asked by Paula Kowaluk 07.10.2016 в 04:32
source

1 answer

1
int* temp;

In the previous line you are defining a pointer to int . This line is not too mysterious. We all know that a pointer is nothing more than a reference to a memory position.

C ++ pointers are not smart, they do not understand the world around them. They just know that they point to a memory address and that's it. In fact there is no way to know if the direction pointed to by the pointer has a correct value or not.

This last I mention to you because this line is incorrect:

alumno[j] = temp;

You are assuming that the pointer will be smart enough to force the entire row to be copied ... but the reality is that the pointer will not implicitly offer that functionality to you.

To copy the row you have to do it element by element:

for( int k=0; k<2; k++ )
  alumno[j][k] = temp[k];

The only way to force the automatic copying of the entire row is for it to be contained within a structure or class. In such a way that when copying said object the copy of the whole row was forced.

I do not put codes about this topic because I understand that you are learning and this part of C ++ is outside of what you are studying right now.

EDIT: I have not been too explicit with the original answer. What I have told you about the assignment is applicable to all the occasions in which you try to overwrite one row with another.

If you intend to replace one row with another you can use std::swap instead of resorting to storing the results in a temporary variable:

for( int k=0; k<2; k++)
  std::swap(alumno[i][k],alumno[j][k]);

And if not, there are also ways to make substitutions without using a temporary variable:

for( int k=0; k<2; k++)
{
  alumno[i][k] ^= alumno[j][k];
  alumno[j][k] ^= alumno[i][k];
  alumno[i][k] ^= alumno[j][k];
}

^ is the operator XOR. If you convert the numbers to binary and perform the 3 operations, you will understand:

A = 10 = 1010
B = 3 = 0011

A = A^B = 1010 ^ 0011 = 1001
B = A^B = 1001 ^ 0011 = 1010 -> B = 10
A = A^B = 1001 ^ 1010 = 0011 -> A = 3
    
answered by 07.10.2016 в 09:06