简体   繁体   中英

Input element by user into 2D vector c++

I'a trying to implement an algorithm, i want to input element by the user into 2D vector so that I have an element like this:

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

so I want to know how to push_back the element into 2D vector

my problem here:

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!!
    }
}

C++ is a strong type language, d is a vector of vector:

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);
}

Here is the solution

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[i].push_back(temp); 
    }
}

This should work:

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();
}

In general, we can add elements in a 2D matrix of vectors according to a similar approach as mentioned below :

#include<bits/stdc++.h>  
using namespace std;
int main() {
int row,col;
cin>>row>>col;
vector<vector<int>>matrix;
for(int i=0;i<row;i++){
vector<int>temp;
for(int j=0;j<col;j++){
int val;
cin>>val;
temp.push_back(val);
}
matrix.push_back(temp);
}
return 0;
}
d[x].push_back(y);

这应该对你有用。

There are two methods to perform this task:

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);
}
/* 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];  
    }  
}  

This is what you can do

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!!
    }
}
#include <iostream>
#include <vector>
using namespace std;

int main() {
    int N;
    cout << "Please enter the no. of edges: ";
    cin >> N;
    vector<vector<int>> outer;
    for(int i = 0; i < N; i++)
    {
        vector<int> temp;
        for(int j = 0; j < 1; j++)
        {
            int u, v;
            cin >> u;
            cin >> v;
            temp.push_back(u);
            temp.push_back(v);
        };
        outer.push_back(temp);
        temp.clear();
    };
    
    for(int i = 0; i<outer.size(); i++)
    {
        for(int j = 0; j < outer[i].size(); j++)
        {
            cout<<" "<<outer[i][j];
        };
        cout<<endl;
    };
};
//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;
}
vector<vector<int>> matrix; //declaring 2D vactor as matrix

int val;
cin>>val; //reading size of matrix in val

for(int i=0;i<val;i++){
    vector<int> temp; //make temp vector for storing val for every iteration 
    for(int j=0;j<val;j++){
        int x;
        cin>>x;
        temp.emplace_back(x);
    }
    matrix.emplace_back(temp); // push back to matrix, temp vector's all value for every iteration
    temp.clear(); // clear temp vector for every iteration
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM