C++ 使用 boost 分配初始化具有固定大小的向量的向量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13121469/
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-27 16:59:26  来源:igfitidea点击:

Initializing a vector of vectors having a fixed size with boost assign

c++boostvectorrepeatassign

提问by saloua

Having a vector of vector with a fixed size,

具有固定大小的向量向量,

vector<vector<int> > v(10);

I would like to initialize it so that it has in all elements a one dimensional vector with initialized value (for example 1).

我想初始化它,以便它在所有元素中都有一个带有初始化值的一维向量(例如 1)。

I have used Boost Assign as follows

我使用 Boost Assign 如下

v= repeat(10,list_of(list_of(1)));

and I've got a compilation error

我有一个编译错误

error: no matching function for call to ‘repeat(boost::assign_detail::generic_list<int>)'

Could you please tell me how to do that. Thanks in advance

你能告诉我怎么做吗。提前致谢

回答by juanchopanza

This doesn't use boost::assignbut does what you need:

这不使用,boost::assign但做你需要的:

vector<vector<int>> v(10, vector<int>(10,1));

This creates a vector containing 10 vectors of int, each containing 10 ints.

这将创建一个包含 10 个向量的向量int,每个向量包含 10个ints

回答by hmjd

You don't need to use boostfor the required behaviour. The following creates a vectorof 10vector<int>s, with each vector<int>containing 10ints with a value of 1:

您不需要boost用于所需的行为。下面创建一个vector10vector<int>S,其中每个vector<int>含有10int具有的值s 1

std::vector<std::vector<int>> v(10, std::vector<int>(10, 1));

回答by moldovean

I will just try to explain it to those new to C++. A vector of verctors mathas the advantage that you can access its elements directly at almost no cost using the []operator..

我将尝试向那些刚接触 C++ 的人解释它。向量向量mat的优点是您可以使用[]运算符直接访问其元素,几乎没有成本。

int n(5), m(8);
vector<vector<int> > mat(n, vector<int>(m));

mat[0][0] =4; //direct assignment OR

for (int i=0;i<n;++i)
    for(int j=0;j<m;++j){
        mat[i][j] = rand() % 10;
    }

Of course this is not the only way. And if you do not add or remove elements, one can also use the native containers mat[]which are nothing more than pointers. Here's my fav way, using C++:

当然,这不是唯一的方法。如果您不添加或删除元素,也可以使用mat[]仅是指针的本机容器。这是我最喜欢的方式,使用 C++:

int n(5), m(8);
int *matrix[n];
for(int i=0;i<n;++i){
    matrix[i] = new int[m]; //allocating m elements of memory 
    for(int j=0;j<m;++j) matrix[i][j]= rand()%10;
}

This way, you don't have to use #include <vector>. Hopefully, it's clearer!

这样,您就不必使用#include <vector>. 希望它更清楚!

回答by Frank Hou

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


int main(){
    int n; cin >> n;
    vector<vector<int>> v(n);
    //populate
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            int number; cin >> number;
            v[i].push_back(number);
        }
    }
    // display
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            cout << v[i][j] << " ";
        }
        cout << endl;
    }
}

Input:

输入:

4
11 12 13 14
21 22 23 24
31 32 33 34
41 42 43 44

Output:

输出:

11 12 13 14
21 22 23 24
31 32 33 34
41 42 43 44