C++ 如何在 int 类型的二维向量中 push_back 数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42249303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:29:05  来源:igfitidea点击:

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

c++multidimensional-arrayvector

提问by saru

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 ?

我有一个向量,想在运行时将 int 数据存储到其中,我可以以这种方式将数据存储在 2D 向量中吗?

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

回答by SingerOfTheFall

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);    
    }
}

回答by A. Monti

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

您正在操纵向量的向量。因此,当声明normal它为空并且不包含任何元素时。

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()
  1. 您的数组数据分布在 N 个不同的动态分配缓冲区(对于 N 行)
  2. 您的数组每行可以有不同数量的列(因为没有强制要求 normal[i].size() == normal[j].size()

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

相反,您可以使用大小向量N * M(其中N是行M数和列数),并使用索引访问行i和列处的元素:ji + 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;

回答by Vlad from Moscow

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;
    }

}   

回答by Umang Kalra

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

为每个索引分配n个空向量,即空向量。然后可以应用 push_back()。

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;
}

回答by Cory Kramer

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.

如果不先[i]分配外部和内部向量,就不能直接分配给。对此的一种解决方案是在 for 循环内创建内部向量,然后在填充这些向量后,将 push_back 推回到外部向量。

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);
}

回答by user3853544

You have a vector of vectors.

你有一个向量的向量。

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

normal[i] 不存在,因为您尚未创建它。

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;
}