Help.... I have 3 vectors I want to store in 1 array, but I can't figure out what I'm doing wrong

vector<double> range, az, el;

double RAzEl[1000][3] = {}; 

	for ( unsigned int j = range; j <= (num); j = j + ncols )
	{
		range.push_back(row_d[j]);
	}
	for ( unsigned int j = az; j <= (num); j = j + ncols )
	{
		az.push_back(row_d[j]);
	}
	for ( unsigned int j = el; j <= (num); j = j + ncols )
	{
		el.push_back(row_d[j]);
	}
		
         RAzEl =  {range, az, el} ;

would making a for loop to assign each number of each vector to their apropriate place be the only way to do it? surely there is a more efficient way

since vectors are arrays why would you want to do that? You could create a vector of vectors

vector< vector<double> > RAzE1;
RaZe1.push_back(range);
RaZe1.push_back(az);
RaZe1.push_back(el);

Of course that won't work if you want to pass the array to a function that expects double[]

Here is an exmaple

#include <vector>
#include <algorithm>
using std::vector;
using std::copy;
using std::cout;

void foo(double a[], size_t size)
{
    for(size_t i = 0; i < size; i++)
        cout << a[i] << '\n';
}

int main()
{
   vector<double> d1,d2,d3;
   vector< vector<double>> t1;
   double d0[255] = {0};
   for(int i = 0; i < 5; i++)
    d1.push_back(i*1.0);
   for(int i = 0; i < 10; i++)
    d2.push_back(i*2.0);
   for(int i = 0; i < 15; i++)
    d2.push_back(i*3.0);
   t1.push_back(d1);
   t1.push_back(d2);
   t1.push_back(d3);
   copy(d1.begin(), d1.end(), d0);
   copy(d2.begin(),d2.end(), &d0[d1.size()]);
   copy(d3.begin(),d3.end(), &d0[d1.size()+d2.size()]);
   foo(d0, d1.size()+d2.size()+d3.size());
   std::cin.get();
}

Edited 5 Years Ago by Ancient Dragon: n/a

ah I had never thought of doing a vector of vectors!


unfortunately that won't work because like you said I can't pass it into a function, but it looks like the code you posted would work, right?

I'm very new to C++, started working in Matlab and now am trying to code C++, so thanks for your help

A somewhat easier way to write

copy(d1.begin(), d1.end(), d0);
copy(d2.begin(),d2.end(), &d0[d1.size()]);
copy(d3.begin(),d3.end(), &d0[d1.size()+d2.size()]);

is as follows:

double* next = copy(d1.begin(), d1.end(), d0);
next = copy(d2.begin(), d2.end(), next);
next = copy(d3.begin(), d3.end(), next);

After this code, next points to the first available location in d0.

My intention with all of this is to pass three vectors into a function, perform operations on each, and return three new vectors. I can't figure out if there is a way to return more than one vector, so I hoped I could put them in an array and return the array.

Would it be more efficient to put the three vectors into a single vector as you both have described? Or put them in an array and return the array?

vector<double> Util_RAzEl_to_ENU(vector<double> range, vector<double> az, vector<double> el, vector<double> ENU)
{
	// East coord
	for (int i = 0; i != range.size(); i++)
	{
		east.push_back(range[i]*cos(el[i])*sin(az[i]));
	}

	// North coord
	for (int i = 0; i != range.size(); i++)
	{
		north.push_back(range[i]*cos(el[i])*cos(az[i]));
	}

	// Up coord
	for (int i = 0; i != range.size(); i++)
	{
		up.push_back(range[i]*sin(el[i]));
	}

	double* next = copy(east.begin(), east.end(), ENU);
	next = copy(north.begin(), north.end(), next);
	next = copy(up.begin(), up.end(), next);

	return ENU;
	
}

int main()
{
         vector<double> range, az, el, ENU;

         // define range, az, el vectors
         Util_RAzEl_to_ENU(range, az, el, ENU);

}

the code doesn't compile, ill figure out the kinks, but is this a good way to go about doing it? I can break the vector back up into the three separate ones once I get it into main(), but getting it there is the issue currently.

TIA

If you want to return more than one object then pass them by address as parameters to the function. Note that all vectors should be passed by reference instead of by value to keep the compiler from generating the code needed to duplicate them.

void Util_RAzEl_to_ENU(vector<double>& range, vector<double>& az, vector<double>& el, vector<double>& ENU, vector<double>& return1, vector<double>& return2, vector<double>& return3)
{

}

Edited 5 Years Ago by Ancient Dragon: n/a

much appreciated, that helped a ton!

here's the code in case anyone stumbles across this thread with a request similar to mine

// Util_RAzEl_to_ENU() converts coordinates from RAzEl to ENU format
void Util_RAzEl_to_ENU(vector<double>& range, vector<double>& az, vector<double>& el, vector<double>& east, 
					   vector<double>& north, vector<double>& up)
{
	int i = 0;
	// East coord
	for (i = 0; i != range.size(); i++)
	{
		east.push_back(range[i]*cos(el[i])*sin(az[i]));
	}

	// North coord
	for (i = 0; i != range.size(); i++)
	{
		north.push_back(range[i]*cos(el[i])*cos(az[i]));
	}

	// Up coord
	for (i = 0; i != range.size(); i++)
	{
		up.push_back(range[i]*sin(el[i]));
	}

}

int main()
{
        vector<double> my_range, my_az, my_el;

        // define my_range, my_az, my_el

        vector<double> my_north, my_east, my_up;
        double my_ENU[1000][3] = {}; 

        Util_RAzEl_to_ENU(my_range, my_az, my_el, my_east, my_north, my_up);

        for (i = 0; i != nrows-1; i++)
        {
             my_ENU[i][0] = my_east[i];
	    my_ENU[i][1] = my_north[i];
	    my_ENU[i][2] = my_up[i];
        }



}

I just made an array out of the three vectors outside of the function. that'll work for my purposes.

Thanks again!

This question has already been answered. Start a new discussion instead.