将 const char 转换为 std::string 作为标题
Converting const char to std::string for title
我正在使用 gnuplot。
在header文件中我有以下函数:
void PlotPath(Gnuplot& gp, const char* title, bool first_plot = true, bool last_plot = true);
然后,在cpp文件中:
template <typename T>
void Path<T>::PlotPath(Gnuplot& gp, const char* title, bool first_plot, bool last_plot)
{
std::vector<WaypointPath<T>> waypoints = GenerateWaypoints(5000);
std::vector<std::pair<T, T>> plot_points(5000);
for (size_t i = 0; i < waypoints.size(); ++i) {
plot_points[i] = std::make_pair(waypoints[i].position[0], waypoints[i].position[1]);
}
if (first_plot) {
gp << "plot" << gp.file1d(plot_points) << "with lines title '" << title << "',";
} else {
gp << gp.file1d(plot_points) << "with lines title '" << title << "',";
}
if (last_plot) {
gp << std::endl;
}
}
我想做的是不使用 const char* title,而是使用 std::string title。
所以我所做的如下并且有效,但我想知道是否有更好的方法来完成这项任务。
template <typename T>
void Path<T>::PlotPath(Gnuplot& gp, const char* title, bool first_plot, bool last_plot)
{
std::vector<WaypointPath<T>> waypoints = GenerateWaypoints(5000);
std::vector<std::pair<T, T>> plot_points(5000);
// Convert const char * to std::string
std::string string_title = title;
for (size_t i = 0; i < waypoints.size(); ++i) {
plot_points[i] = std::make_pair(waypoints[i].position[0], waypoints[i].position[1]);
}
if (first_plot) {
gp << "plot" << gp.file1d(plot_points) << "with lines title '" << string_title << "',";
} else {
gp << gp.file1d(plot_points) << "with lines title '" << string_title << "',";
}
if (last_plot) {
gp << std::endl;
}
}
在这里,我通过创建另一个变量将 const char* 转换为 std::string。但是有没有办法在函数参数本身中包含 std::string title
?
我在这里所做的工作正常,但我只是在寻找一种更优雅的方法来解决这个问题。
感谢您的期待。
提出你的论点std::string
。 std::string
有一个采用 const char*
的构造函数,所以这应该不是问题。
无需转换。此代码运行良好。
if (first_plot) {
gp << "plot" << gp.file1d(plot_points) << "with lines title '" << title << "',";
} else {
gp << gp.file1d(plot_points) << "with lines title '" << title << "',";
}
我正在使用 gnuplot。
在header文件中我有以下函数:
void PlotPath(Gnuplot& gp, const char* title, bool first_plot = true, bool last_plot = true);
然后,在cpp文件中:
template <typename T>
void Path<T>::PlotPath(Gnuplot& gp, const char* title, bool first_plot, bool last_plot)
{
std::vector<WaypointPath<T>> waypoints = GenerateWaypoints(5000);
std::vector<std::pair<T, T>> plot_points(5000);
for (size_t i = 0; i < waypoints.size(); ++i) {
plot_points[i] = std::make_pair(waypoints[i].position[0], waypoints[i].position[1]);
}
if (first_plot) {
gp << "plot" << gp.file1d(plot_points) << "with lines title '" << title << "',";
} else {
gp << gp.file1d(plot_points) << "with lines title '" << title << "',";
}
if (last_plot) {
gp << std::endl;
}
}
我想做的是不使用 const char* title,而是使用 std::string title。 所以我所做的如下并且有效,但我想知道是否有更好的方法来完成这项任务。
template <typename T>
void Path<T>::PlotPath(Gnuplot& gp, const char* title, bool first_plot, bool last_plot)
{
std::vector<WaypointPath<T>> waypoints = GenerateWaypoints(5000);
std::vector<std::pair<T, T>> plot_points(5000);
// Convert const char * to std::string
std::string string_title = title;
for (size_t i = 0; i < waypoints.size(); ++i) {
plot_points[i] = std::make_pair(waypoints[i].position[0], waypoints[i].position[1]);
}
if (first_plot) {
gp << "plot" << gp.file1d(plot_points) << "with lines title '" << string_title << "',";
} else {
gp << gp.file1d(plot_points) << "with lines title '" << string_title << "',";
}
if (last_plot) {
gp << std::endl;
}
}
在这里,我通过创建另一个变量将 const char* 转换为 std::string。但是有没有办法在函数参数本身中包含 std::string title
?
我在这里所做的工作正常,但我只是在寻找一种更优雅的方法来解决这个问题。
感谢您的期待。
提出你的论点std::string
。 std::string
有一个采用 const char*
的构造函数,所以这应该不是问题。
无需转换。此代码运行良好。
if (first_plot) {
gp << "plot" << gp.file1d(plot_points) << "with lines title '" << title << "',";
} else {
gp << gp.file1d(plot_points) << "with lines title '" << title << "',";
}