I am having some issues creating a 2-d vector.

I need it to be of a determined length in the first dimension but of an undetermined length in the other. The length in the second dimention will be calculated later in the code, in one of the functions that uses the vector and writes to it.

So, here is what I have:

string line ("line");
	string circle ("circle");
	string sphere ("sphere");
	string addparticle ("particle");
	int NumberObjects;
	int number_line = 0;
	int number_circle = 0;
	int number_sphere = 0;
	int number_particle = 0;

	cout<< "How Many Objects Would You Like To Add?"  << endl;

	cin >> NumberObjects;

	vector<string> shape(NumberObjects);

	for (int i = 0; i<NumberObjects; i++)
{

	cout << "Shape " << i << " ?  (line / circle/ sphere / particle)" << endl;

	cin >> shape[i];

	if( shape[i].compare(line) == 0)
	{
		cout << shape[i] << "   Success!"<< endl;

		number_line ++;

	}
	if( shape[i].compare(circle) == 0)
	{
		cout << shape[i] << "   Success!"<< endl;

		number_circle ++;

	}
	if( shape[i].compare(sphere) == 0)
	{
		cout << shape[i] << "   Success!"<< endl;

		number_sphere ++;

	}
	if( shape[i].compare(addparticle) == 0)
	{
		cout << shape[i] << "   Success!"<< endl;

		number_particle ++;

	}
	else
	{
		cout << shape[i] << "   FAIL" << endl;
	}

vector<vector<particle>> pline(number_line, vector<particle>());
vector<vector<particle>> pcircle(number_circle);
vector<vector<particle>> psphere(number_sphere);
vector<particle> pparticle(number_particle);

for (int i = 0; i < number_line; i++)
	{	
		particle::CreateLine(pline[i]);

	}

All the individual functions work and have been tested. The code complies, but then on running kick up an error saying "vector subscript out of range". Which I assume means that I am simply defining my vectors incorrectly.

Any help with this would be great, I have searched around but cannot find anything on defining a vector with an undetermined length.

If you require more code to explain the answer I can provide it, but I suspect it is quite a simple problem that either has an explanation or it cannot be done.

If you cannot define a 2-D vector where one of the dimensions is of an unspecified length could you point me in the direction of something that would do roughly the same thing? Contain a 2-d matrix of objects (particle in this case).

Just noticed something, the first definition of a vector I made is different to the others, this was me experimenting to try and find something that works. Suffice to say it did not, but if it is the same as all the others it does not work either.

Cheers

Josh

Recommended Answers

All 4 Replies

If you are referencing those pline values they must exist at that time. Is there a way you could set up SetLine() with default values and then fill them in once you have the pline ones? I understand what you're asking but don't understand how it relates to the dimension (which by virtue of the vector can be adjusted).

I could just set it to be a large number, but then I will get a vector full of zeros if it only needed to be short in the end.

I am creating a vector multiple times, so want to put each vector I create into a vector all together. So I have a vector of vectors. I know how many vectors I am going to create, but do not know the dimensions of those that are going to be created.

Okay, what I am doing now is setting it to start with to be just a number, then resizing it inside the function when I know how big it needs to be. Seems like a bit of a waste, and there should be a better way to do it, but it will do for now.

Yeah it does work (I just tried making 3 diff vectors of variable length and putting them into a vector<vector<int> > . It goes without saying but you'd have to make sure if you were using the [][] notation that you didn't step out into undefined territory as my guess would be the second dimension of the vector is defined as the size of the largest vector within it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.