How can insert value in structure vector?
Here I have given my sample code, but it gave error. How can insert value in structure vector?
Code:
struct Hough_Transform_3D_Accumulator
{
long int i;
long int j;
long int k;
long int count;
};
int main()
{
........................
for(i=0;i<total_r_count;i++)//for loop r count start
{
for(j=0;j<total_theta_count;j++)//for loop theta count start
{
for(k=0;k<total_shi_count;k++)//for loop shi count start
{
if(accumulator_3D[i][j][k]>0)
{
vec.push_back(i,j,k,accumulator_3D[i][j][k]);/// error message
}
}
}
}
Error Message:error C2661: 'std::vector<_Ty>::push_back' : no overloaded function takes 4 arguments
Re: How can insert value in structure vector?
Quote:
Originally Posted by
nihad
Here I have given my sample code, but it gave error. How can insert value in structure vector?
Give your struct a constructor.
Second, give your struct more meaningful member names. Giving names such as "i", "j", and "k" not only doesn't convey any meaning, imagine if you had many source files, and you want to search where you used "j".
Code:
struct Hough_Transform_3D_Accumulator
{
long int loop1;
long int loop2;
long int loop3;
long int count;
Hough_Transform_3D_Accumulator(long val1 = 0, long val2 = 0, long val3 = 0, long val4 = 0) :
loop1(val1), loop2(val2), loop3(val3), count(val4) {}
};
int main()
{
//
vec.push_back(Hough_Transform_3D_Accumulator(i,j,k,accumulator_3D[i][j][k]));
}
Regards,
Paul McKenzie
Re: How can insert value in structure vector?
I solved the problem. Here is the solution:
Code:
Hough_Transform_3D_Accumulator temp;
temp.i = i;
temp.j = j;
temp.k = k;
temp.count = accumulator_3D[i][j][k];
vec.push_back(temp);
Re: How can insert value in structure vector?
Quote:
Originally Posted by
nihad
I solved the problem. Here is the solution:
That "solution" is really not a good one.
1) The user must now know what the member names of the struct are. What if you decide to use better names? You now have to change all of your code to use the new member names.
2) What if the user forgets to initialize one of the names?
The correct solution is to write a constructor, as I pointed out in my initial post.
Regards,
Paul McKenzie