-3

I am using gnuplot-iostream for c++,my problem is like below:

 gp << "set object 1 rect from 3,1 to 3.2,4 fc lt 1 \n";

In the numbers of above, can we use some variables in c++ to replace them? and how to do that? Adding one rectangle is easy, but when I want to add more, that will be very intractable. I try several ways ,but it does not work. Thanks ahead!

davidhigh
  • 14,652
  • 2
  • 44
  • 75
  • I'm not familiar with `gbuplot-iostream` but if they work like standard iostreams: `int x = 3; int y = 1; gp << x << "," << y;` – James Adkison May 26 '16 at 18:40
  • 1
    Have you read a C++ tutorial? At least if this stream is modeled after C++ IOStreams, you will find the answer there. – Ulrich Eckhardt May 26 '16 at 18:50
  • you can input the value of x into gp, but it can not be used in the above command, if you use x to replace the number, it is an error. It does not help, but thanks! – yunxiao shan May 26 '16 at 18:54
  • can you give me an example? – yunxiao shan May 26 '16 at 18:56
  • please edit and show the ways you tried. Particularly, make sure that the simple solution of James Adkinson does not work (and if so, state it explcitly please). Moreover, try to set up the string like `string s = "set object 1 rect from " + std::to_string(3) + "," ...` (and so on) and try to pass this into the gnuplot-stream. (PS: I gave +1 as I didn't know the interface) – davidhigh May 26 '16 at 19:03

1 Answers1

-1

Never used gnuplot-iostream, but maybe some vanilla C++ will help you, iostream is just a pipe to console, so you cant interrupt the buffer but concatenate information, you could do something like this:

 gp << "set object " << 1 << " rect from "
 << 3,1 << " to " << 3.2 << "," << 4 << " fc lt 1 \n";

But i suppose you don't want to do that.

I'd create a struct and overload the << operator to return whatever you need.

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;
}

So you can just define rectangles and pass them to the stream

Rect r1 = {{2,41},{63,1.4}};
std::cout << r1; // "from 2,41 to 63,1.4"
PRDeving
  • 679
  • 3
  • 11
  • thank you, that really works, I am not very family with the stream operation, also thanks the others, I really need to review them. – yunxiao shan May 26 '16 at 19:33
  • Thank you, next time read the documentation carefully, also read about iostream and operators overloading. Good luck pal ;) – PRDeving May 26 '16 at 19:35