84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
Code:string str(double dX, double dY)//Convert vector coordinates to string, the format is "(x=, y=)"
{ return "(x=" + dX + ", y=" + dY + ")"; }
Error message:Expression must contain an integer or an unscoped enumeration type
Two common solutions.
std::string v1(double dX, double dY) { std::ostringstream stream; stream << "(x=" << dX << ", y=" << dY << ")"; return stream.str(); } std::string v2(double dX, double dY) { char buff[1024]; sprintf(buff, "(x=%f, y=%f)", dX, dY); return buff; }
v2 may overflow.
c++11 provides std::to_string for string conversion
Or as mentioned above
std::string v1(double dX, double dY) { std::ostringstream stream; stream << "(x=" << dX << ", y=" << dY << ")"; return stream.str(); }
According to the original poster’s procedure, to_string conversion is more efficient.
It is recommended to return const string
Two common solutions.
v2 may overflow.
include
c++11 provides std::to_string for string conversion
Or as mentioned above
According to the original poster’s procedure, to_string conversion is more efficient.
It is recommended to return const string