用户将元素输入到2D矢量c ++中

时间:2016-04-26 23:27:24

标签: c++ vector user-input

我正在尝试实现一个算法,我希望用户将元素输入到2D矢量中,这样我就有了这样的元素:

reference 1:
1 2 3
3 2 1
1 2 3

所以我想知道如何将元素推送到2D矢量

我的问题在这里:

std::vector<vector<int>> d;
//std::vector<int> d;
cout<<"Enter the N number of ship and port:"<<endl;
cin>>in;
cout<<"\Enter preference etc..:\n";
for(i=0; i<in; i++){ 
cout<<"ship"<<i+1<<":"<<' ';
    for(j=0; j<in; j++){
    cin>>temp;
    d.push_back(temp);// I don't know how to push_back here!!
    }
}

8 个答案:

答案 0 :(得分:2)

这是解决方案

df[df['Phone'] == 'Redmi Note 7']][['Detection for 1 person(s)' , 'Detection for 2 persons (s)' , 'Recognition for 1 person(s)' , 'Recognition for 2 persons (s)']] = df[df['Phone'] == 'Redmi Note 7'][['Detection for 1 person(s)' , 'Detection for 2 persons (s)' , 'Recognition for 1 person(s)' , 'Recognition for 2 persons (s)']].div(1.14) 

答案 1 :(得分:1)

C ++是一种强类型语言,d是向量的向量:

for(i=0; i<in; i++){ 
    cout<<"ship"<<i+1<<":"<<' ';
    vector<int> row;
    for(j=0; j<in; j++){
      cin>>temp;
      row.push_back(temp);// I don't know how to push_back here!!
    }
    d.push_back(row);
}

答案 2 :(得分:0)

d[x].push_back(y);

这对你有用。

答案 3 :(得分:0)

有两种方法可以执行此任务:

vector<vector<int> > v;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
v[i].push_back(data);
}}

vector<vector<int> > v;
for(int i=0;i<n;i++){
vector<int> x;
for(int j=0;j<m;j++) x[j].push_back(data);
v.push_back(x);
}

答案 4 :(得分:0)

/* Below takes user input for n x n array */ 

vector<vector <int>> arr(n);  
for (int i = 0; i < n; i++) {  
    arr[i].resize(n);  
    for (int j = 0; j < n; j++) {  
        cin >> arr[i][j];  
    }  
}  

答案 5 :(得分:0)

这应该有效:

vector<vector<int> > d;
int val;
for(int i = 0; i < in; i++){
    vector<int> temp;
    for(int j = 0; j < in; j++){
        cin >> val;
        temp.push_back(val);
    }
    d.push_back(temp);
    temp.clear();
}

答案 6 :(得分:0)

这是你能做的

int in;
std::cout << "Enter the N number of ship and port:" << std::endl;
std::cin >> in;

// declare a vector d of size 'in' containing vectors of size 0;
std::vector<std::vector<int>> d(in, std::vector<int>());

std::cout << "\Enter preference etc..:\n";
for(i=0; i<in; i++){ 
    std::cout << "ship" << i+1 << ":" << ' ';
    for(j=0; j<in; j++){
        int temp;
        std::cin >> temp;
        d[i].push_back(temp); // now you can push_back here!!
    }
}

答案 7 :(得分:0)

//this is a solution and it litrally works give it a try
//v(n) is must 
//thats why your sol was giving problem
//we must specify rows

int main()
{
  int value;
  vector<vector<int>> v(n);

  for(int i=0;i<n;++i)
  {
    for(int j=0;j<n;++j)
    {
      cin>>value;
      v[i].push_back(value);
    }
  }

  return 0;
}