Create a 2d vector of pair ints
Nov 24, 2014 at 9:29am Nov 24, 2014 at 9:29am UTC
How would i create a 2d vector of pair of ints, and how do i iterate through the positions?
Nov 24, 2014 at 11:00am Nov 24, 2014 at 11:00am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
struct T
{
int n1;
int n2;
};
int main()
{
T a{5,6};
cout << a.n1 << " " << a.n2 << endl;
vector<T> vect;
vect.push_back(a);
return 0;
}
Nov 24, 2014 at 11:08am Nov 24, 2014 at 11:08am UTC
I hoped for something like vector<vector<pair<int,int>>>
or is it not possible?
Nov 24, 2014 at 11:29am Nov 24, 2014 at 11:29am UTC
Yes, it is possible. However, do note that
vector<vector<pair<int ,int >>> foo;
the foo is empty. You could push_pack one or more
vector<pair<int ,int >>
"rows" into it (and fill each row separately).
If you want a "neat" rectangle, then:
1 2 3
size_t rows = ...
size_t cols = ...
vector<vector<pair<int ,int >>> foo( rows, vector<pair<int ,int >>( cols ) );
Iteration is a nested loop. There are many styles for that.
Nov 24, 2014 at 2:43pm Nov 24, 2014 at 2:43pm UTC
How do i then insert a pair?
Nov 24, 2014 at 5:28pm Nov 24, 2014 at 5:28pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
// store object in vector
#include <iostream>
#include <vector>
using namespace std;
class Pair {
public :
int x;
int y;
Pair() { x=0, y=0; }
Pair(int n1, int n2) { x=n1; y=n2; }
};
int main()
{
int row= 3, column= 5;
Pair x(5,6); // x Pair object
vector<Pair> ar;
vector<vector<Pair> > arr;
for (int i=0; i<row; i++)
{
arr.push_back(ar);
}
for (int i=0; i<row; i++)
{
for (int j=0; j<column; j++)
{
arr[i].push_back( Pair(i,j) );
}
}
cout << arr[2][3].x << ", " << arr[2][3].y; // 2, 3
return 0;
}
Nov 24, 2014 at 6:57pm Nov 24, 2014 at 6:57pm UTC
vector<vector<pair<int,int>>> temp (1000, vector<pair<int, int>>(1000));
vector<vector<Test>> temp (1000, vector<Test>(1000));
I have this class, and a vector of vector of this class which i am having some problems with, inserting object of this class into the vector..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
class Test{
public :
Test(vector<vector<pair<int , int >>> &temp)
{
Objects = move(temp);
};
vector<vector<pair<int , int >>> Objects;
};
vector<vector<pair<int ,int >>> temp (1000, vector<pair<int , int >>(1000));
vector<vector<Test>> objects_vector (1000, vector<Test>(1000));
//for loop inserting elements to temp
objects_vector(temp); // gives me an error message saying this constructor doesn't exist..
Nov 25, 2014 at 7:16am Nov 25, 2014 at 7:16am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
// 2D vector, objects, pair
#include <iostream>
#include <vector>
#include <utility> // pair()
using namespace std;
class Test{
public :
vector<vector<pair<int , int >>> Obj;
Test () { }
Test (vector<vector<pair<int , int > > > temp)
{
Obj = temp; //move(temp);
}
};
int main() {
std::pair <int ,int > foo;
foo = std::make_pair (10,20);
cout << "foo: " << foo.first << ", " << foo.second << '\n' ;
vector<pair<int ,int > > v1;
vector<vector<pair<int ,int > > > v2;
for (int row=0; row<10; row++) {
v2.push_back( v1 );
for (int column=0; column<5; column++) {
v2[row].push_back( std::make_pair (row,column) );
}
}
cout << v2.at(9).at(4).first << " " << v2.at(9).at(4).second << "\n" ;
cout << "\n" ;
Test t1;
Test t2(v2);
vector<Test> vt(100, v2);
cout << vt[99].Obj[6][3].first << " " << vt[99].Obj[6][3].second << "\n" ;
return 0;
}
Nov 25, 2014 at 9:45am Nov 25, 2014 at 9:45am UTC
1 2 3 4 5 6
class Test{
public :
vector<vector<pair<int , int >>> Objects;
};
vector<vector<Test>> foo;
That is a bit scary, because it is almost the same as:
vector<vector<vector<vector<pair<int , int >>>>> foo;
A 4D array of pairs.
How do i then insert a pair?
1 2 3 4 5 6 7 8 9 10 11
size_t rows = ...
size_t cols = ...
vector<vector<pair<int ,int >>> bar( rows, vector<pair<int ,int >>( cols ) );
// the bar does already have rows*cols pairs. One has to simply change their values.
for ( size_t row = 0; row < bar.size(); ++row {
for ( size_t col = 0; col < bar[row].size(); ++col ) {
// some "real" data
bar[row][col].first = 42 + row;
bar[row][col].second = col - 7;
}
}
objects_vector(temp); // gives me an error message saying this constructor doesn't exist..
One element (value_type) in your objects_vector is a
vector<Test>
. The temp is a
vector<vector<pair<int ,int >>>
. Its value_type is
vector<pair<int ,int >>
.
The std::vector has 6 constructor forms:
http://www.cplusplus.com/reference/vector/vector/vector/
None of those matches to your case.
Topic archived. No new replies allowed.