两个不同大小的硬编码阵列到一个二维向量中

时间:2012-03-24 20:20:49

标签: c++ arrays vector

我在一个二维矢量中使用了2个不同大小的阵列,我的输出不正确。阵列的大小为4,大小为13 我希望COLUMN 0有:55,66,77,88 我希望第1-12列在每个行中都有1,2,3,4,5,6,7,8,9,10,10,10,11。 似乎第13个数组的第二个循环需要循环4次以填充4行,但是,我不知道该怎么做。 以下是我目前在代码和输出方面的内容:

#include <iostream> 
#include <vector>

using namespace std;

int main()
{
 int typeArray[4] = {55,66,77,88}; 
 int valArray[13] = {1,2,3,4,5,6,7,8,9,10,10,10,11}; 

// 4 = LENGTH or NUMBER of ROWS; 13 = WIDTH or NUMBER of COLUMNS; 
//  0 = VALUE all cells are initialized to 
 vector< vector <int> > myVector(4, vector<int> (14,0)); 

  for (int i = 0; i < myVector.size(); i++)  
    { 
        myVector[i][0] = typeArray[i]; 
    for (int j = 0; j < myVector[i].size(); j++)  
      { 
         myVector[1][j] = valArray[j]; 
      } 
   } 
   // print vector to screen with 4 ROWS, 1 COLUMNS 
    for (int i = 0; i < myVector.size();  i++) 
      {          
        for (int j = 0; j < myVector[i].size(); j++) 
         {  
          cout << myVector[i][j] << ' '; 
          }          
          cout << '\n'; 
      } 

OUTPUT ---------------------------------------------- -------------------------------------------------- -------------------------------------------------- - 55 0 0 0 0 0 0 0 0 0 0 0 0 ----第一行------------------------------- -------------------------------------------------- -------------------------------- 1 2 3 4 5 6 7 8 9 10 10 10 11 ----第二排------------------------------- -------------------------------------------------- -------------------- 77 0 0 0 0 0 0 0 0 0 0 0 0 -------第三行---------------------------- -------------------------------------------------- ---------------------------- 88 0 0 0 0 0 0 0 0 0 0 0 0 -------第四行


int typeArray[4] = {55,66,77,88}; 
 int valArray[14] = {0,1,2,3,4,5,6,7,8,9,10,10,10,11}; 

// 4 = LENGTH or NUMBER of ROWS; 13 = WIDTH or NUMBER of COLUMNS; 
//  0 = VALUE all cells are initialized to 
 vector< vector <int> > myVector(4, vector<int> (14,0)); 

  for (int i = 0; i < myVector.size(); i++)  
    { 
        myVector[i][0] = typeArray[i]; 

    for (int j = 0; j < myVector[i].size(); j++)  
      { 
         myVector[1][j + 1] = valArray[j]; 
      } 
   } 
   // print vector to screen with 4 ROWS, 14 COLUMNS 
    for (int i = 0; i < myVector.size();  i++) 
      {          
        for (int j = 0; j <= myVector[i].size(); j++) 
         {  
          cout << myVector[i][j] << ' '; 
          }          
          cout << '\n'; 
      } 

OUTPUT ---------------------------------------------- -------------------------------------------------- -------------------------------------------------- - 55 1 2 3 4 5 6 7 8 9 10 10 10 11 ----第一排------------------------------ -------------------------------------------------- ----------------------- 66 1 2 3 4 5 6 7 8 9 10 10 10 11 ----第二排------------------------------ -------------------------------------------------- ------------------ 77 1 2 3 4 5 6 7 8 9 10 10 10 11 ----第三排------------------------------ -------------------------------------------------- -------------------- 88 1 2 3 4 5 6 7 8 9 10 10 10 11 ----第四行


这是每个Kerrek更新的修订代码,它完美运行:

 int typeArray[4] = {55,66,77,88}; 
 int valArray[13] = {1,2,3,4,5,6,7,8,9,10,10,10,11}; 

// 4 = LENGTH or NUMBER of ROWS; 13 = WIDTH or NUMBER of COLUMNS; 
//  0 = VALUE all cells are initialized to 
 vector< vector <int> > myVector(4, vector<int> (14,0)); 

 for (int i = 0; i < myVector.size(); i++) 
    {
       myVector[i][0] = typeArray[i];

   for (int j = 1; j < myVector[i].size(); j++) 
     {
         myVector[i][j] = valArray[j - 1];
     }

   } 

  // print vector to screen with 4 ROWS, 14 COLUMNS
  for (int i = 0; i < myVector.size(); i++)
      {         
        for (int j = 0; j < myVector[i].size(); j++)
         { 
          cout << myVector[i][j] << ' ';
          }         
          cout << '\n';
      }

输出与第二个代码块相同,是的!

1 个答案:

答案 0 :(得分:1)

或许这样的事情:

vector< vector <int> > myVector(4, vector<int> (14, 0));   // one longer!

for (int i = 0; i < myVector.size(); i++)  
{ 
    myVector[i][0] = typeArray[i]; 
    for (int j = 1; j < myVector[i].size(); j++)  
    { 
         myVector[i][j] = valArray[j - 1]; 
    }
}