C++ 中的矩阵生成

发布于 2024-10-16 13:39:08 字数 609 浏览 4 评论 0原文

我有以下代码:

for (int w=0; w<10; w++)
{
    //some lines of code
    unsigned long num = x.to_ulong(); 
    cout << "Decimal form is: " << num << endl;
}  

现在我想要的是,制作一个矩阵,该矩阵将具有这些 num 值,逐列。例如,matrix[i][j],其中 i 是从“0 到 15”,“j”是从“0 到 15”,如下所示出色地。我希望他们像,matrix[0][0],然后matrix[0][1]matrix[0][2]代码>等等,比如:

for(int i=0; i<=15; i++)
{
    for(int j=0; j<=15; j++)
        matrix[i][j] = num //here is problem, value of "num" what shall i write instead of it?
}

I have the following code:

for (int w=0; w<10; w++)
{
    //some lines of code
    unsigned long num = x.to_ulong(); 
    cout << "Decimal form is: " << num << endl;
}  

Now what I want is, to make a matrix which will have these values of num, column wise. For example, matrix[i][j], where i is from, let's say "0 to 15", and "j" is from "0 to 15" as well. I want them to put like, matrix[0][0], then matrix[0][1], matrix[0][2] and so on, something like:

for(int i=0; i<=15; i++)
{
    for(int j=0; j<=15; j++)
        matrix[i][j] = num //here is problem, value of "num" what shall i write instead of it?
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

念三年u 2024-10-23 13:39:08

这是一些伪代码。

std::vector<std::vector<unsigned long> > matrix(15); // 15 rows
typedef std::vector<std::vector<unsigned long> >::iterator it_type;

it_type row = matrix.begin();

for (int w=0;w<10;w++)
{
  //some lines of code
  unsigned long num = x.to_ulong(); 
  cout <<"Decimal form is: " << num<< end;
  // if we've hit 15 items in the current row (i.e. 15 columns), then shift to the next row
  if (row->size() == 15)
    ++row;

  // add this to the next column in the current row
  row->push_back(num);
}
// resize the last row
row->resize(15); // this will ensure that there are 15 columns in the last row

注意:您必须进行一些边界检查(例如,如果数字多于 15 * 15,则递增迭代器 row 将导致垃圾...等)

编辑:像普通数组一样显示、迭代,例如:

for(size_t i=0; i < matrix.size(); ++i)
{
  for(size_t j=0; j < matrix[i].size(); ++j)
    cout << matrix[i][j] << " ";
  cout << endl;
}

Here is some pseudo code..

std::vector<std::vector<unsigned long> > matrix(15); // 15 rows
typedef std::vector<std::vector<unsigned long> >::iterator it_type;

it_type row = matrix.begin();

for (int w=0;w<10;w++)
{
  //some lines of code
  unsigned long num = x.to_ulong(); 
  cout <<"Decimal form is: " << num<< end;
  // if we've hit 15 items in the current row (i.e. 15 columns), then shift to the next row
  if (row->size() == 15)
    ++row;

  // add this to the next column in the current row
  row->push_back(num);
}
// resize the last row
row->resize(15); // this will ensure that there are 15 columns in the last row

NOTES: you'll have to do some bounds checking (for example if there are more numbers than 15 * 15, the incrementing the iterator row will lead to crap... etc.)

EDIT: to display, iterate through like a normal array, for example:

for(size_t i=0; i < matrix.size(); ++i)
{
  for(size_t j=0; j < matrix[i].size(); ++j)
    cout << matrix[i][j] << " ";
  cout << endl;
}
蓝眼泪 2024-10-23 13:39:08

标准“使用Boost”答案:

如果您只需要一个二维数组,请查看Boost.MultiArray

如果您需要进行线性代数,请查看 Boost .uBlas

Standard "use Boost" answer:

If you just need a two-dimensional array, check out Boost.MultiArray.

If you need to do linear algebra, check out Boost.uBlas.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文