我试图实现一种算法,我想由用户将元素输入到 2D 向量中,以便我有一个这样的元素:
reference 1:
1 2 3
3 2 1
1 2 3
所以我想知道如何将元素push_back成 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!!
}
}
这是解决方案
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);
}
}
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);
}
这应该有效:
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();
}
通常,我们可以根据类似的方法在向量的 2D 矩阵中添加元素,如下所示:
#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);
这应该适合您。
有两种方法可以执行此任务:
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];
}
}
这是你可以做的
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
}