How can I push_back data in a 2d vector of type int

前端 未结 6 867
借酒劲吻你
借酒劲吻你 2021-02-02 03:03

I have a vector and want to store int data in to it at run time can I store the data in a 2D vector in this manner ?

std::vector> n         


        
相关标签:
6条回答
  • 2021-02-02 03:18

    You are manipulating a vector of vectors. As such, when declaring normal it is empty and does not contain any element.

    You can either :

    Resize the vector prior to inserting elements

    std::vector<std::vector<int> > normal;
    normal.resize(20);
    
    for (size_t i = 0; i < normal.size(); ++i)
    {
        for (size_t j = 0; j < 20; ++j)
            normal[i].push_back(j);
    }
    

    This may be slightly more efficient than pushing an empty vector at each step as proposed in other answers.

    Use a flat 2D array

    If you want to store a 2D array, this is not the optimal solution, because :

    1. Your array data is spread across N different dynamically allocated buffers (for N lines)
    2. Your array can have a different number of columns per line (because nothing enforces that normal[i].size() == normal[j].size()

    Instead, you can use a vector of size N * M (where N is the number of lines and M the number of columns), and access an element at line i and columns j using the index i + j * N :

    size_t N = 20;
    size_t M = 20;
    std::vector<int> normal;
    normal.resize(N * M);
    
    for (size_t i = 0; i < N; ++i)
        for (size_t j = 0; j < M; ++j)
            normal[i + j * N] = j;
    
    0 讨论(0)
  • 2021-02-02 03:20

    Here is one more approach.

    #include <iostream>
    #include <iomanip>
    #include <vector>
    #include <numeric>
    
    int main() 
    {
        std::vector<std::vector <int> > normal;
        normal.resize( 10, std::vector<int>( 20 ) );
    
        for ( auto &v : normal ) std::iota( v.begin(), v.end(), 0 );
    
        for ( const auto &v : normal )
        {
            for ( int x : v ) std::cout << std::setw( 2 ) << x << ' ';
            std::cout << std::endl;
        }
    }
    

    The program output is

     0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 
     0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 
     0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 
     0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 
     0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 
     0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 
     0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 
     0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 
     0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 
     0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 
    

    You can write a corresponding function

    #include <iostream>
    #include <iomanip>
    #include <vector>
    #include <numeric>
    
    template <typename T>
    T & init_2d( T &container, size_t m, size_t n )
    {
        container.resize( m, typename T::value_type( n ) );
    
        for ( auto &item : container ) std::iota( item.begin(), item.end(), 0 );
    
        return container;
    }
    
    int main() 
    {
        std::vector<std::vector<int>> v;
    
        for ( const auto &v : init_2d( v, 10, 20 ) )
        {
            for ( int x : v ) std::cout << std::setw( 2 ) << x << ' ';
            std::cout << std::endl;
        }
    
    }   
    
    0 讨论(0)
  • 2021-02-02 03:23

    You cannot directly assign to [i] without allocating the outer and inner vectors first. One solution to this would be to create the inner vectors inside your for loop, then once those are populated, push_back to the outer vector.

    std::vector<std::vector<int>> normal;
    for(i=0;i<10;i++)
    {
        std::vector<int> temp;
        for(j=0;j<20;j++)
        {
            temp.push_back(j);
        }
        normal.push_back(temp);
    }
    
    0 讨论(0)
  • 2021-02-02 03:28

    Yes, but you also need to push each of the sub-vectors:

    std::vector<std::vector<int>> normal;
    for(int i=0; i<10; i++)
    {
        normal.push_back(std::vector<int>());
        for(int j=0; j<20; j++)
        {    
            normal[i].push_back(j);    
        }
    }
    
    0 讨论(0)
  • 2021-02-02 03:39

    You have a vector of vectors.

    normal[i] Does not exist because you have not created it.

    std::vector<std::vector <int> > normal:
    for(i=0;i<10;i++){
        normal.emplace_back();
        for(j=0;j<20;j++){
            normal.back().push_back(j);
        }
    }
    
    for(i=0;i<10;i++){
        for(j=0;j<20;j++){
            std::cout << normal[i][j] << " ";
        }
        std::cout << std::endl;
    }
    
    0 讨论(0)
  • 2021-02-02 03:43

    Allocate n empty vectors, that is, empty vector for each index. Then push_back() can be applied.

    int main()
    {
        int n = 10;
        std::vector<std::vector<int>> normal;
        normal.resize(n);   //Allocating 'n' empty vectors
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < 20; j++)
            {
                 normal[i].push_back(j);
            }
        }
        return 0;
    }
    
    0 讨论(0)
提交回复
热议问题