c++ - How to convert vector coordinates to string in the format of "(x=, y=)"
过去多啦不再A梦
过去多啦不再A梦 2017-06-05 11:11:01
0
2
1276

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

过去多啦不再A梦
过去多啦不再A梦

reply all(2)
我想大声告诉你

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.

大家讲道理

include<string>

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template