【问题标题】:How to print a grid vector in C++如何在 C++ 中打印网格矢量
【发布时间】:2017-08-05 01:14:53
【问题描述】:

我正在研究网格中的 C++ 打印向量。 在这里,我需要将随机数放入大小为 3x * 3y 的向量中。然后我必须用一个数组的二维矩阵将它们打印出来。 我不明白如何用一个数组表示二维矩阵。 另外,我不确定如何打印出多维向量。我必须处理 print_vector 函数,它以网格形式打印向量。 你能帮我改进下面的代码吗?

int main()
{
  populate_vector();
  return 0;
}

void populate_vector()
{

  int x,y;
  cout<<"Enter two Vectors x and y \n->";
  cin>> x;
  cin>> y;
  srand((unsigned)time(NULL));
  std::vector<int> xVector((3*x) * (3*y));
  for(int i = 0; (i == 3*x && i == 3*y); ++i){
    xVector[i] = rand() % 255 + 1;
       if(i == 3*x){
         cout << "\n";
       }
  }
  print_vector(xVector);
}

void print_vector(vector<int> &x) {


}

【问题讨论】:

  • 我建议使用双 for 循环并从两个索引 (i * width + j) 将索引重建为平面数组,\n 自然地在内部循环之后,这很好地转换为例如向量的向量。
  • 你能给我一个for循环的例子吗?

标签: c++ arrays vector


【解决方案1】:

这样的事情会澄清你的代码,有一个填充向量的过程,另一个打印向量的过程

int main()
{
    int x,y;
  std::cout<<"Enter two Vectors x and y \n->";
  std::cin>> x;
  std::cin>> y;

  srand((unsigned)time(NULL));
  int xSize = x * 3; // it is important to have the size of the final grid stored
  int ySize = y * 3; // for code clarity 
  std::vector<int> xVector( xSize * ySize);
  // iterate all y 
  for ( y = 0 ; y < ySize; ++y) {
      // iterate all x 
    for ( x = 0 ; x < xSize;  ++x) {
      // using row major order https://en.wikipedia.org/wiki/Row-_and_column-major_order
        xVector[y * xSize + x]  = rand() % 255 + 1;
    }
  }
  // when printing you want to run y first 
  for ( y = 0 ; y < ySize; ++y) {
    for ( x = 0 ; x < xSize;  ++x) {
      // iterate all y 
        printf("%d ", xVector[y * xSize + x] );
    }
    printf("\n");
   }
}

我想你要注意这一步,你可以将 x 和 y 位置转换为一个数组维度。很简单,您只需将 y 乘以 x 的大小并添加 x。

所以像这样的二维

1 2 3 
4 5 6 

最终会变成这样的

1 2 3 4 5 6

you can see it running here

【讨论】:

    【解决方案2】:

    我不是专家,但我喜欢只拥有一个向量的向量......类似于:

    void print_vector(vector<vector<int>>& m_vec)
    {
        for(auto itY: m_vec)
        {
            for(auto itX: itY)
                cout << itX << " ";
    
            cout << endl;
        }
    }
    
    void populate_vector()
    {
        int x,y;
        cout << "Enter Two Vectors x and y \n ->";
        cin >> x;
        cin >> y;
        srand((unsigned)time(NULL));
        vector<vector <int>> yVector;
        for(auto i = 0; i < y*3; ++i)
        {
            vector<int> xVector;
            for(auto j = 0; j < x*3; ++j)
                xVector.push_back(rand()%255+1);
    
            yVector.push_back(xVector);
        }
        print_vector(yVector);
    }
    

    编辑:哦,我以前从未见过这个网站,谢谢 Saykou...这是代码工作:http://cpp.sh/3vzg

    【讨论】:

    • part2.cpp:10:21: 警告:基于范围的 for 循环是 C++11 扩展 [-Wc++11-extensions] for(int itY : m_vec) ^ part2.cpp :10:17: 错误:没有从 'std::__1::vector >' 到 'int' for(int itY : m_vec) ^ ~的可行转换>
    • 我分离了这两个函数并从终端 g++ 运行它
    • 啊,是的,对于那些 for 循环和自动的东西,你需要告诉编译器你正在使用 c++ 11 stackoverflow.com/questions/10363646/compiling-c11-with-g
    • 这里被重写为标准 C++ 98 cpp.sh/4sll5 虽然我建议进入 C++11,但它真的很棒 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-08
    相关资源
    最近更新 更多