如何在 gnuplot-iostream 中使用 C++ 中的变量
how to use variables in C++ in the gnuplot-iostream
我正在为 C++ 使用 gnuplot-iostream,我的问题如下:
gp << "set object 1 rect from 3,1 to 3.2,4 fc lt 1 \n";
上面的数字,能不能用c++中的一些变量来代替呢?以及如何做到这一点?添加一个矩形很容易,但是当我想添加更多时,这将非常棘手。
我尝试了几种方法,但它不起作用。
先谢谢了!
从未使用过 gnuplot-iostream,但也许一些普通的 C++ 可以帮助你,iostream 只是一个控制台的管道,所以你不能中断缓冲区但连接信息,你可以这样做:
gp << "set object " << 1 << " rect from "
<< 3,1 << " to " << 3.2 << "," << 4 << " fc lt 1 \n";
但我想你不想那样做。
我会创建一个结构并将 << 运算符重载为 return 任何你需要的。
struct Rect {
float from[2];
float to[2];
}
std::ostream& operator<<(std::ostream& os, const Rect& obj)
{
std::string formated = "from "
+ std::to_string(obj.from[0]) + ","
+ std::to_string(obj.from[1]) + " to "
+ std::to_string(obj.to[0]) + ","
+ std::to_string(obj.to[1]);
os << formated;
return os;
}
所以你可以只定义矩形并将它们传递给流
Rect r1 = {{2,41},{63,1.4}};
std::cout << r1; // "from 2,41 to 63,1.4"
我正在为 C++ 使用 gnuplot-iostream,我的问题如下:
gp << "set object 1 rect from 3,1 to 3.2,4 fc lt 1 \n";
上面的数字,能不能用c++中的一些变量来代替呢?以及如何做到这一点?添加一个矩形很容易,但是当我想添加更多时,这将非常棘手。 我尝试了几种方法,但它不起作用。 先谢谢了!
从未使用过 gnuplot-iostream,但也许一些普通的 C++ 可以帮助你,iostream 只是一个控制台的管道,所以你不能中断缓冲区但连接信息,你可以这样做:
gp << "set object " << 1 << " rect from "
<< 3,1 << " to " << 3.2 << "," << 4 << " fc lt 1 \n";
但我想你不想那样做。
我会创建一个结构并将 << 运算符重载为 return 任何你需要的。
struct Rect {
float from[2];
float to[2];
}
std::ostream& operator<<(std::ostream& os, const Rect& obj)
{
std::string formated = "from "
+ std::to_string(obj.from[0]) + ","
+ std::to_string(obj.from[1]) + " to "
+ std::to_string(obj.to[0]) + ","
+ std::to_string(obj.to[1]);
os << formated;
return os;
}
所以你可以只定义矩形并将它们传递给流
Rect r1 = {{2,41},{63,1.4}};
std::cout << r1; // "from 2,41 to 63,1.4"