0

Here is my code:

vector<vector<int>> tranches_max(vector<vector<int>> x)
{
   vector<vector<int>> y;
   y[0].swap(x[0]);
   return y;
}
int main() {

vector<vector<int>> x=
{
      {2, 1, 0, 2},
      {0, 1, 0, 3},
      {1, 3, 0, 0},
      {0, 2, 2, 0},
    };
  for (vector< vector<int> >::size_type u = 0; u < x.size(); u++) 
     { for (vector<int>::size_type v = 0; v < x[u].size(); v++) 
       { cout << x[u][v] << " ";
       }
       cout <<endl;
     }
 vector<vector<int>> y= tranches_max(x);    
 for (vector< vector<int> >::size_type u = 0; u < y.size(); u++) 
 { for (vector<int>::size_type v = 0; v < y[u].size(); v++) 
{ cout << y[u][v] << " ";
    }
    cout <<endl;
  }
return 0;
}

My code does not generate an error, but it crashes .. I think the copy is the problem .. Do you have any idea?

Thank you.

4

2 回答 2

3

我不确定您尝试做什么,但是在您的函数中,您访问了 vector 超出范围tranches_max的元素。您使用使向量为空的空构造函数构造 y。也许你需要用一些初始大小创建 y ?y[0]y

于 2013-11-05T14:50:50.023 回答
0
  1. Y 为空
  2. 此代码不应编译,因为 >> 之间没有空格,这使其成为流运算符
于 2013-11-05T14:53:05.247 回答