doubt about Vectors

0

By solving this problem

  

Do a function with header

     

vector flattens (vector const & v); who receives several   vectors and concatenates them in order. For example, if the function receives   Vector vector

     

[[1, 2, 3], [4, 5], [6, 7, 8, 9]] should return the vector

     

[1, 2, 3, 4, 5, 6, 7, 8, 9]

with the following code:

#include<iostream>
#include<vector>
using namespace std;

vector<int> aplana(const vector< vector<int> >& vv){
    vector<int> x;
    //vv[i][j]
    for(int i = 0; i< int(vv.size());i++){
        for(int j = 0;j < int(vv[i].size());j++){
            x.push_back(vv[i][j]);
        }
    }
    return x;
}

int main(){
    vector< vector<int> >vv(3);//V ector de V ectores

    vv[0].push_back(1);
    vv[0].push_back(2);
    vv[0].push_back(3);

    vv[1].push_back(4);
    vv[1].push_back(5);

    vv[2].push_back(6);
    vv[2].push_back(7);
    vv[2].push_back(8);
    vv[2].push_back(9);

    vector<int> v(aplana(vv));//V ector

    for(int i = 0;i <int(v.size());i++){
        cout<<v[i]<<' ';

    }
}

I saw the situation of using many times the function push_back() in the code, since I do not know much about vectors and I do not know another way to assign value to the "squares" of a vector that is not one by one with push_back() or as array (vv[0][0] = 1 ).

I wanted to ask them if they could explain to me what methods exist to valorise 2 or more squares of a vector at one time.

    
asked by bassily 28.11.2016 в 23:36
source

3 answers

3

If you can afford to work with the C ++ 11 (2011) or C ++ 14 (2014) standard, you have the initialization lists at your disposal. This mechanism allows assigning a series of values to an element without having to resort to push_back:

vv[0] = {1, 2, 3};
vv[1] = {4, 5};
vv[2] = {6, 7, 8, 9};

And even initialization lists can be nested to create a more compact structure:

std::vector<std::vector<int>> vv = { { 1,2,3}, {4,5}, {6,7,8,9} };

If you can not afford to use the most current standards, you have to hit with push_back . Of course, you can always use some function to avoid having to repeat so much code. A silly example:

std::vector<int> ToVector(int size, int* datos)
{
    std::vector<int> toReturn;
    for( int i=0; i<size; ++i,++datos)
      toReturn.push_back(*datos);

    return toReturn;
}

int main()
{
  int data1[] = {1,2,3}; 
  int data2[] = {4,5}; 
  int data3[] = {6,7,8,9}; 

  std::vector<std::vector<int> > mm;
  mm.push_back(ToVector(3,data1));
  mm.push_back(ToVector(2,data2));
  mm.push_back(ToVector(4,data3));
}
    
answered by 29.11.2016 / 00:38
source
1

You can use std::copy and the utility std::back_inserter , so assuming these vectors:

std::vector<int> uno = {1, 2, 3};
std::vector<int> otro = {4, 5};
std::vector<int> ultimo = {6, 7, 8, 9};

One possible solution would be:

std::vector<int> aplanado;
aplanado.reserve(uno.size() + otro.size() + ultimo.size());

std::copy(uno.begin(),    uno.end(),    std::back_inserter(aplanado));
std::copy(otro.begin(),   otro.end(),   std::back_inserter(aplanado));
std::copy(ultimo.begin(), ultimo.end(), std::back_inserter(aplanado));

for (const auto &v : aplanado)
    std::cout << v << ' ';

That will not prevent you from using the push_back but at least you do not see . By the way, it is not necessary to use the std::vector::reserve but I advise you to do it anyway.

We can also generalize the " flattening " of vectors by creating a concatenated operator:

template <typename T>
std::vector<T> operator+(const std::vector<T> &a, const std::vector<T> &b)
{
    std::vector<T> result(a);
    result.reserve(a.size() + b.size());
    std::copy(b.begin(), b.end(), std::back_inserter(result));
    return result;
}

for (const auto &v : uno + otro + ultimo)
    std::cout << v << ' ';

It is not necessary for operator + to be a template, but as I have proposed, you can concatenate any type of vector.

    
answered by 30.11.2016 в 10:02
0

Preferably paste the code here so you do not have to search elsewhere.

You can assign a vector with an initialization list, so your code could be:

vv[0] = {1, 2, 3};
vv[1] = {4, 5};
vv[2] = {6, 7, 8, 9};
    
answered by 29.11.2016 в 00:02