filling a 2d vector

Hello,
I'm new in C++, and I have a small problem with filling a 2 columns and n rows vector with 2 variables:
1
2
3
4
5
	  for (ms::it i = _ms->starting (); i < _ms->ending (); i++)
		{int x=(**i).ll;
		 double y=(**i).pp;		
		 _final->adding (x,y);		    
		std::vector< std::pair< int, double> >mytable;

So I've created a 2d vector " mytable" and I wish to fill it with parameters "x" and "y"...
I cant do a push_back using .first and .second ... I get an error saying:
first and second arent decalred.

Could I get some help please?

Thanks
std::vector< std::pair< int, double> > mytable;

This is not a 2D vector. This is a 1D vector. Each element in the 1D vector contains a std::pair object. If you're going to use this, you have to create each pair object and then push_back that pair object.

Hello,

Is there another way to do that using a 2d vector?

Thanks
I don't understand why you need a 2d array/vector. How about this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <vector>

using namespace std;

struct DataStruct
{
	int x;
	double y;

	DataStruct() : x(0), y(0.00) {}
	DataStruct(const int _x, const double _y) : x(_x), y(_y) {}
};

int main()
{
	vector<DataStruct> v;

	v.push_back(DataStruct(10, 20.05));
	v.push_back(DataStruct(200, 500.00));

	return 0;
}
I dont need absolutely a 2d array.

I just need something, that let me use the values in the array later in another function.
I think the std::pair object can do that and your example too.
Thanks for your code, I will test it.

Concerning your first reply, I cant push_back the pair object in the array... I've got an error each time. How can I do it?

Thanks
1
2
3
4
5
std::vector< std::pair< int, double> > mytable ;
int x = 78 ;
double y = 2.35 ;
mytable.emplace_back( x, y ) ; // C++11
mytable.push_back( std::make_pair( x, y ) ) ; // C++98 

Topic archived. No new replies allowed.