# How insert a vector of Points C++

**URL:** <https://get-help.theconstruct.ai/t/how-insert-a-vector-of-points-c/21344>\
**Category:** Open Discussions\
**Created:** [January 2, 2023, 5:46pm UTC](https://get-help.theconstruct.ai/t/how-insert-a-vector-of-points-c/21344 "2023-01-02T17:46:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Voltedge](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/voltedge/32/7586_2.png) [@Voltedge](https://get-help.theconstruct.ai/u/Voltedge)\
**Post date:** [January 2, 2023, 5:46pm UTC](https://get-help.theconstruct.ai/t/how-insert-a-vector-of-points-c/21344/1 "2023-01-02T17:46:24Z")

</div>

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());

```

---

<div class="post-metadata">

**Author:** ![girishkumar.kannan](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/girishkumar.kannan/32/6994_2.png) [@girishkumar.kannan](https://get-help.theconstruct.ai/u/girishkumar.kannan)\
**Post date:** [January 2, 2023, 6:33pm UTC](https://get-help.theconstruct.ai/t/how-insert-a-vector-of-points-c/21344/2 "2023-01-02T18:33:35Z")

</div>

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](https://en.cppreference.com/w/cpp/container/vector)
2. [https://cplusplus.com/reference/vector/vector/](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.

```auto
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

---

<div class="post-metadata">

**Author:** ![Voltedge](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/voltedge/32/7586_2.png) [@Voltedge](https://get-help.theconstruct.ai/u/Voltedge)\
**Post date:** [January 2, 2023, 7:49pm UTC](https://get-help.theconstruct.ai/t/how-insert-a-vector-of-points-c/21344/3 "2023-01-02T19:49:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://get-help.theconstruct.ai/uploads/default/original/2X/9/9d3ce8305cc0d0d9b6e07d15767a6f245c526f1b.jpeg) [@system](https://get-help.theconstruct.ai/u/system)\
**Post date:** [January 9, 2023, 5:47pm UTC](https://get-help.theconstruct.ai/t/how-insert-a-vector-of-points-c/21344/4 "2023-01-09T17:47:00Z")

</div>

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