Help With Vector Conversion

Hi Guys,

I have a variable of the form vector<double> with unknown size.

I need convert this into the form of X1, X2, X3,...,Xn

where Xn is the double value of the vector with position n in the array.

What's the best way to do this? Is it possible even?
When you say convert into the form of, what exactly do you mean?
Well, you could make an array, but that's redundant and not really useful. Just use theVector[n].

You can't do this with "normal" variables since the size is unknown and I still wouldn't recommend it.
I need to input the vector into a function in the form of x1, x2, .. , xn so that I can run an lpsolve but I can't figure out how to do this. I can't put it in as a vector, It needs to be in that form with the commas separating the terms. I don't know how to make it this way though. I was thinking make it a string of characters but I don't know how to do that either.
I've been trying stuff like this:

1
2
3
4
5
6
7
8
{	
		for(i=0; i < P.size()-1; i++){
		double row[] = {DurationVector[i],}; 
		
	
		add_constraint(lp,row, LE, P[P.size()-1][1]);

	}


Where DurationVector is an array of doubles that I need to be put into this function as that same array separated by commas.
Last edited on
you can use a vector just like you do an array, as an array is just a pointer. Just use &vector[0] =]
What do you mean just use &vector[0]? I already have it as an array/vector, I need to have it with the commas separating the values...
Last edited on
I built the vector using the push_back from separate Duration values. I was thinking their may be a way to create a string/char in the same way where I push the value then the comma then another value so on and so forth.
I need to have it with the commas separating the values...

ehrm I doubt you do.... but I'll dig. Why do you need comma separated values? Are you saying the function has comma separated arguments? What does the function do? Is there a better alternative?

In your example you're attempting to convert to an array, which is unnecessary! Just do this!:

add_constraint(lp,&DurationVector[0], LE, P[P.size()-1][1]);
Last edited on
Ill try it now.

From what I have gathered (through trial and error using working programs) I do need comma separated values but I never thought to try to just put it in the add_constraint like that so here goes...
You're amazing...

It worked splendidly. I have been trying to figure this out for five days in a row... it was so simple. Thank you so much for your help.
I do need comma separated values


That's just initializing the array, if you want a new array with the elements of a vector here's a quick (and dirty) and easy way to do it. (you can also just use a pointer...which is exactly what I'm recommending with the &DurationVector[0] thing)

1
2
3
4
5
6
7
8
9
double* row = new double[DurationVector.size()];

for (int a = 0; a < DurationVector.size(); a++){
row[a]=DurationVector[a];
}

//do your stuff with row here

delete [] row;
Topic archived. No new replies allowed.