Reputation: 13
I am working on a c++ problem where I need to convert a vector pair of type struct to an array. My code is as follows:
struct YObject
{
YObject(unsigned int val1,
float val2,
float val3)
: m_V1(val1)
, m_V2(val2)
, m_V3(val3)
{}
unsigned int m_V1;
float m_V2;
float m_V3;
};
int result(float *output)
{
int output_size = 3*3;
std::vector<YObject> vect;
// using emplace() to insert pair in-place
vect.emplace_back(32, 24.5, 56.3);
vect.emplace_back(45, 30.3, 67.8);
vect.emplace_back(99, 78.6, 59.3);
float out[3];
for (int i=0; i<vect.size(); i++)
{
out[0]=vect[i].m_V1;
out[1]=vect[i].m_V2;
out[2]=vect[i].m_V3;
for (int n=0; n<3; n++)
{
output[n] = out[n];
//cout << output[n] << endl;
}
}
return 0;
}
int final(float *outr)
{
result(outr);
return 0;
}
int main()
{
int size = 3*3;
float arr[size];
final(arr);
cout << "arr[0]: " << arr[0] << " arr[1]: " << arr[1] << " arr[2]: " << arr[2] << " arr[3]: " << arr[3] << " arr[4]: " << arr[4] << " arr[5]: " << arr[5] << " arr[6]: " << arr[6] << " arr[7]: " << arr[7] << " arr[8]: " << arr[8];
return 0;
}
I want that the output array passed in result function to return the full vector which is formed after three emplace_back statements. i.e. the cout statement in main function should print:
arr[0]: 32 arr[1]: 24.5 arr[2]: 56.3 arr[3]: 45 arr[4]: 30.3 arr[5]: 67.8 arr[6]: 99 arr[7]: 78.6 arr[8]: 59.3
but right now its printing:
arr[0]: 99 arr[1]: 78.6 arr[2]: 59.3arr[3]: 0 arr[4]: -1.94201e+27 arr[5]: 4.59121e-41 arr[6]: -9.96761e+17 arr[7]: -5.79801e+60 arr[8]: 1.98345e-22
Upvotes: 0
Views: 294
Reputation: 211560
If you lightly clean up your code and use std::vector
there's really not much that can go wrong and it works as you expect:
#include <vector>
#include <iostream>
struct YObject
{
YObject(const unsigned int val1, const float val2, const float val3)
: m_V1(val1)
, m_V2(val2)
, m_V3(val3)
{}
unsigned int m_V1;
float m_V2;
float m_V3;
};
std::vector<float> result(const std::vector<YObject>& input)
{
std::vector<float> output;
// Easily iterate over each "input" given using for
for (auto& vect : input) {
output.push_back(vect.m_V1);
output.push_back(vect.m_V2);
output.push_back(vect.m_V3);
}
return output;
}
int main()
{
std::vector<YObject> vect;
// Define example inputs here, not deep inside a function
vect.emplace_back(32, 24.5, 56.3);
vect.emplace_back(45, 30.3, 67.8);
vect.emplace_back(99, 78.6, 59.3);
auto arr = result(vect);
std::cout << "arr[0]: " << arr[0] << " arr[1]: " << arr[1] << " arr[2]: " << arr[2] << " arr[3]: " << arr[3] << " arr[4]: " << arr[4] << " arr[5]: " << arr[5] << " arr[6]: " << arr[6] << " arr[7]: " << arr[7] << " arr[8]: " << arr[8] << std::endl;
return 0;
}
In your code you referenced arr[9]
which doesn't exist in a length 9 array, the indexes are only 0 .. 8.
Try and write functions that take inputs and return useful outputs. Having a useless int
return value instead of returning a new vector
is one such case. Also get into the habit of declaring your arguments as const
and references for non-trivial types to avoid inadvertent copies or mutations.
Upvotes: 1