●构建一个名为Costmap2D的类要求包含成员变量
1存储地图数据的容器要求使用指针要求可以容纳unsigned int * unsigned int大小的地图
2地图分辨率 即一个像素是多少米
3地图上每一个点的默认值
4地图上每一个点是unsigned char类型的数据
5需要提供类的构造函数(使用初值列)析构函数,拷贝构造函数一定要提供,
●需要重载运算符"="和"<<" map= ; cout <
#include <iostream>
#include <cstring>
#include <stdexcept>
#include <cassert>
#include <iomanip>
class Costmap2D
{
public:
typedef unsigned int size_type;
typedef unsigned char value_type;
Costmap2D() {}
Costmap2D(const std::initializer_list<std::initializer_list<value_type>> &lists)
{
_numRows = lists.size();
size_type maxNumColumns = 0;
for (auto list : lists)
{
if (list.size() > maxNumColumns)
maxNumColumns = list.size();
}
_numColumns = maxNumColumns;
auto size = _numRows * _numColumns;
if (size > 0)
{
_data = new value_type(size);
std::memset(_data, 0, size);
size_type i = 0;
for (auto list : lists)
{
size_type j = 0;
for (auto v : list)
{
_data[i * _numColumns + j] = v;
j++;
}
i++;
}
}
}
Costmap2D(size_type numRows, size_type numColumns,
value_type resolution, value_type defaultValue = 0)
: _numRows(numRows), _numColumns(numColumns), _resolution(resolution),
_defaultValue(defaultValue)
{
auto size = numRows * numColumns;
if (size > 0)
{
_data = new unsigned char[size];
std::memset(_data, defaultValue, size);
}
}
Costmap2D(const Costmap2D &other)
: _numRows(other._numRows), _numColumns(other._numColumns),
_resolution(other._resolution), _defaultValue(other._defaultValue)
{
auto size = _numRows * _numColumns;
if (size > 0)
{
_data = new value_type[size];
std::memcpy(_data, other._data, size);
}
}
Costmap2D(Costmap2D &&other)
: _numRows(other._numRows), _numColumns(other._numColumns),
_data(other._data), _resolution(other._resolution),
_defaultValue(other._defaultValue)
{
other._numRows = 0;
other._numColumns = 0;
other._data = nullptr;
}
~Costmap2D()
{
delete[] _data;
}
Costmap2D &operator=(const Costmap2D &other)
{
if (this == &other)
return *this;
_numRows = other._numRows;
_numColumns = other._numColumns;
_resolution = other._resolution;
_defaultValue = other._defaultValue;
delete[] _data;
_data = nullptr;
auto size = _numRows * _numColumns;
if (size > 0)
{
_data = new value_type[size];
std::memcpy(_data, other._data, size);
}
return *this;
}
Costmap2D &operator=(Costmap2D &&other)
{
_numRows = other._numRows;
_numColumns = other._numColumns;
_resolution = other._resolution;
_defaultValue = other._defaultValue;
_data = other._data;
other._numRows = 0;
other._numColumns = 0;
other._data = nullptr;
return *this;
}
size_type numRows() const { return _numRows; }
size_type numColumns() const { return _numColumns; }
value_type resolution() const { return _resolution; }
value_type defaultValue() const { return _defaultValue; }
value_type operator()(size_type i, size_type j) const
{
assert(!isEmpty());
return _data[i * _numColumns + j];
}
value_type &operator()(size_type i, size_type j)
{
assert(!isEmpty());
return _data[i * _numColumns + j];
}
bool isEmpty() const
{
return _numRows == 0 || _numColumns == 0;
}
private:
size_type _numRows = 0;
size_type _numColumns = 0;
value_type *_data = nullptr;
value_type _resolution = 1;
value_type _defaultValue = 0;
};
std::ostream &operator<<(std::ostream &os, const Costmap2D &map)
{
os << "Rows: " << map.numRows() << '\n';
os << "Columns: " << map.numColumns() << '\n';
os << "Resolution: " << static_cast<int>(map.resolution()) << '\n';
os << "Default Value: " << static_cast<int>(map.defaultValue()) << '\n';
os << "Data:\n";
for (unsigned int i = 0; i < map.numRows(); i++)
{
for (unsigned int j = 0; j < map.numColumns(); j++)
{
os << std::hex << std::uppercase << std::setw(2) << std::setfill('0')
<< static_cast<int>(map(i, j)) << ' ';
}
os << '\n';
}
return os;
}
int main()
{
Costmap2D map = {{1, 2, 3}, {4, 5, 6}, {200, 201, 202}, {255}};
std::cout << map;
return 0;
}
$ g++ -Wall main.cpp
$ ./a.out
Rows: 4
Columns: 3
Resolution: 1
Default Value: 0
Data:
01 02 03
04 05 06
C8 C9 CA
FF 00 00