ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I'm using for-loop:

for (int i = 0; i < sizeof(msg->tracks)/sizeof(msg->tracks[0]); i++)

Don't do this. This is how you could calculate the number of elements in an array in C.

roscpp is all C++, so there is no need for this (and it also won't (always) work).

This is the structure of a radar_msgs/RadarTrackArray:

std_msgs/Header header
radar_msgs/RadarTrack[] tracks

tracks is a unbounded array (or list). In roscpp, those will be mapped onto std::vector (see wiki/msg).

C++ provides a few convenient ways to iterate over std::vector:

  1. a range-based for loop (if you can use C++11)
  2. regular iteration using iterators
  3. element-wise access using the [] operator (and bounded by std::vector::size())

You tagged this question with kinetic, so I'm going to assume you are on Ubuntu Xenial (16.04). That version of Ubuntu ships with GCC 5.4, so you should be able to enable C++11. Range-based for loops are pretty convenient, so I would recommend to use those.

If you require a counter, use something like:

for (int i = 0; i < msg->tracks.size(); i++)
{
  [...]
}

I'm using for-loop:

for (int i = 0; i < sizeof(msg->tracks)/sizeof(msg->tracks[0]); i++)

Don't do this. This is (almost) how you could calculate the number of elements in an array in C.

roscpp is all C++, so there is no need for this (and it also won't (always) work).

This is the structure of a radar_msgs/RadarTrackArray:

std_msgs/Header header
radar_msgs/RadarTrack[] tracks

tracks is a unbounded array (or list). In roscpp, those will be mapped onto std::vector (see wiki/msg).

C++ provides a few convenient ways to iterate over std::vector:

  1. a range-based for loop (if you can use C++11)
  2. regular iteration using iterators
  3. element-wise access using the [] operator (and bounded by std::vector::size())

You tagged this question with kinetic, so I'm going to assume you are on Ubuntu Xenial (16.04). That version of Ubuntu ships with GCC 5.4, so you should be able to enable C++11. Range-based for loops are pretty convenient, so I would recommend to use those.

If you require a counter, use something like:

for (int i = 0; i < msg->tracks.size(); i++)
{
  [...]
}

I'm using for-loop:

for (int i = 0; i < sizeof(msg->tracks)/sizeof(msg->tracks[0]); i++)

Don't do this. This is (almost) how you could calculate the number of elements in an array in C.

roscpp is all C++, so there is no need for this (and it also won't (always) work).

This is the structure of a radar_msgs/RadarTrackArray:

std_msgs/Header header
radar_msgs/RadarTrack[] tracks

tracks is a unbounded array (or list). In roscpp, those will be mapped onto std::vector (see wiki/msg).

C++ provides a few convenient ways to iterate over std::vector:

  1. a range-based for loop (if you can use C++11)
  2. regular iteration using iterators
  3. element-wise access using the [] operator (and bounded by std::vector::size())

You tagged this question with kinetic, so I'm going to assume you are on Ubuntu Xenial (16.04). That version of Ubuntu ships with GCC 5.4, so you should be able to enable C++11. Range-based for loops are pretty convenient, so I would recommend to use those.

If you require a counter, use something like:

for (int i = 0; i < msg->tracks.size(); i++)
{
  [...]
}

Edit: I was curious to see whether range-based for loops and counting could be combined (just as in Python with enumerate(..)). If you can use C++17, it turns out you can do that: Python-Like enumerate() In C++17.

I'm using for-loop:

for (int i = 0; i < sizeof(msg->tracks)/sizeof(msg->tracks[0]); i++)

Don't do this. This is (almost) how you could calculate the number of elements in an array in C.

roscpp is all C++, so there is no need for this (and it also won't (always) work).

This is the structure of a radar_msgs/RadarTrackArray:

std_msgs/Header header
radar_msgs/RadarTrack[] tracks

tracks is a unbounded array (or list). In roscpp, those will be mapped onto std::vector (see wiki/msg).

C++ provides a few convenient ways to iterate over std::vector:

  1. a range-based for loop (if you can use C++11)
  2. regular iteration using iterators
  3. element-wise access using the [] operator (and bounded by std::vector::size())

You tagged this question with kinetic, so I'm going to assume you are on Ubuntu Xenial (16.04). That version of Ubuntu ships with GCC 5.4, so you should be able to enable C++11. Range-based for loops are pretty convenient, so I would recommend to use those.

If you require a counter, use something like:

for (int i = 0; i < msg->tracks.size(); i++)
{
  [...]
}

Edit: I was curious to see whether range-based for loops and counting could be combined (just as in Python with enumerate(..)). If you can use C++17, it turns out you can do that: Python-Like enumerate() In C++17.

. That should allow you to do something like:

for (auto [i, track] : enumerate(msg->tracks))
{
  // do something with 'i' and/or 'track'
}