如何在for循环中创建具有不同名称的向量 [英] How do you create vectors with different names in a for loop

查看:58
本文介绍了如何在for循环中创建具有不同名称的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在c ++中创建一堆向量,每个向量都有不同的名称.我在这个站点上四处张望,却没有找到任何简单"的信息,因为我对C ++还是很陌生.我想做到这一点而无需导入以前从未使用过的库.例如:

I'm trying to create a bunch of vectors in c++, each having a different name. I've looked around on this site, and have not found any information that is 'simple', as I'm very new to c++. I would like to do it without importing libraries i've never used before. For example:

#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <vector>
using namespace std;

int main(int argc, char* argv[])
{
    int n = 2;
    int m = 4;
    double size = pow(m,n);
    for (int i=0; i<n; ++i)
        {
             vector<double> xi(size);
             // where xi would vary with the iteration through n
             // i.e. I would have vectors x1, x2 in the case of n=2
        }
return 0;
}

这不是简单的情况,因为我可以在开始时自己创建x1和x2,因为我不知道'n'是什么,因为它将在用户开始时输入.程序.最简单的方法是什么?

This isn't the simple case were I could create x1, and x2 by themselves at the start, because I won't know what 'n' will be, as it will be inputted by the user at the start of the program. What would be the easiest way of doing this?

推荐答案

使用向量向量.

std::vector<std::vector<double>> xArray;
for (int i=0; i<n; ++i)
{
   vector<double> xi(size);

   // Fill up xi
   // ...

   xArray.push_back(xi);    
}

这篇关于如何在for循环中创建具有不同名称的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆