How insert a vector of Points C++

Hi

Now I tried to add the vector of odometry (x,y,theta) to → geometry_msgs/Point list_of_odom to my ROS Basic Real Project C++

but I don’t understand of can I do that

    // x, y , tetha are variables from odom_callback
    geometry_msgs::Point P;
    P.x = x;
    P.y = y;
    P.z = theta;

    std::vector<geometry_msgs::Point> P1;
    // P1.insert(P1.end(), P.x);
    // P1.insert(P1.end(), P.y);
    P1.insert(P1.end(), P.z);
    P1.push_back(P.x);

    result_.list_of_odoms.insert(result_.list_of_odoms.end(), P1.begin(),
                                 P1.end());

Hi @Voltedge ,

Please refer the documentation for C++ vectors.

I am telling the following to you in a good way:
DO NOT EXPECT PEOPLE TO SPOON-FEED YOU THE ANSWERS
If you want to be good at programming you must start to research, learn and practice.
Do basic Google searches. I am sure you know how to do a simple google search.

  1. std::vector - cppreference.com
  2. https://cplusplus.com/reference/vector/vector/

Answer:
You should use vector.push_back() instead of vector.insert().
You should add the entire Point32 message as an item into the vectors, NOT x, y and z separately.

  1. Define a vector as vector<geometry_msgs::Point32> list_of_odoms;
  2. Use the odometry subscriber callback to store current odometry values into a class variable of type Point32 (you just use position x and y along with theta[yaw] from orientation quaternion).
  3. Append each odometry point data as an item into the vector list_of_odoms.push_back(current_odom_point32) where current_odom_point32 is a Point32 message that you just got from odom subscriber.
  4. You can then get the vector item by index for calculating distance between this value and the previous value.

In case you cannot get anything working, try to break it down to simpler steps and solve that. Later incorporate what you did into the main code.
How to do it simple you ask?

  1. Create a vector with Point32 messages.
  2. Add / remove / modify existing items to that vector you created in step 1.
  3. Print the current contents of the vector with a loop of your choice.
  4. If something goes wrong then fix and redo until you get it right.
std::vector<geometry_msgs::Point> P1;
P1.insert(P1.end(), P.z);
P1.push_back(P.x);

This will not work. Why? Because your vector contains items that are Point type. When you do P1.insert(P1.end(), P.z); You are trying to insert a float into a vector containing Point. Obviously, it will throw an error.
Again, same thing for P1.push_back(P.x);. You are trying to add float into a vector with Point type data. It will throw an error.

Finally,
result_.list_of_odoms.insert(result_.list_of_odoms.end(), P1.begin(), P1.end());
You can just do push_back() - why are you using a function of that complexity?
insert() is usually used to insert an item somewhere in-between the vector.

All you need is practice! Just a little bit, enough to understand the concept. Don’t get sucked into the entire topic.

Regards,
Girish

1 Like

Hi @girishkumar.kannan

I was confused about the geometry_msgs::Point type and how he interact with vector, cause when I put the push_back() appears " " so, I was thinking more than the interact, no like a C++ problem. Thanks for change my perspective.

Regards,
Ruben

This topic was automatically closed after 6 days. New replies are no longer allowed.