gpt4 book ai didi

c++ - 还有另一种使用stringstream的方法吗?

转载 作者:行者123 更新时间:2023-12-02 10:21:01 25 4
gpt4 key购买 nike

我目前正在使用stringstream传递字符串,但是我想知道是否还有另一种方法?下面是它的完成方式的快照。因为我对c++还是很陌生,所以我不确定这是否是最好的方法,或者我可能听说过使用字符串流会减慢该过程的速度,并希望了解更多信息,因此可能会有其他选择。

string Circle::toString()
{
stringstream sentence;
sentence << "Shape [" << index <<"]" << endl;
sentence << "Name: " << name << endl;

if (containsWarpSpace == false)
sentence << "Special Type: NS" << endl;
else
sentence << "Special Type: WS" << endl;

sentence << "Area: " << area << " units square" << endl;
sentence << "Vertices: " << endl;
sentence << "Point[0]: (" << center[0] << ", " << center[1] << ")" << endl;
sentence << endl;
sentence << "Points on perimeter: " << endl;
bool trueFalse= false;
int count = 0;
for ( int y = center[1] - unit; y < center[1] + unit + 1; y++)
{
for ( int x = center[0] - unit; x < center[0] + unit + 1; x++)
{
trueFalse = isPointOnShape (x, y);
if (trueFalse == true)
{
if (count %6 == 0)
sentence << "(" << x << ", " << y << ")";
else
sentence << ", (" << x << ", " << y << ")";

count++;
if (count %6 == 0)
sentence << endl;
}
}
}

if (count == 0)
sentence << "none!";

sentence << endl;
count = 0;
sentence << "Points within shape: " << endl;
for (int y = center[1] - unit; y < center[1] + unit + 1; y++)
{
for ( int x = center[0] - unit; x < center[0] + unit + 1; x++)
{
trueFalse = isPointInShape (x, y);

if (trueFalse == true)
{
if (count %6 == 0)
sentence << "(" << x << ", " << y << ")";
else
sentence << ", (" << x << ", " << y << ")";

count++;
if (count %6 == 0)
sentence << endl;
}
}
}
if (count == 0)
sentence << "none!";

sentence << endl;
return sentence.str();
}

希望有人可以指导我

最佳答案

使用stringstream的方式很好,但是您需要考虑到您的代码在每个stringstream调用中都会创建一个toString()对象,从而导致内存分配。例如,如果您有一千个圆圈要转储为字符串,那么临时stringstream对象的基础内存缓冲区将有一千个内存分配,最终将有一千个释放(如果初始缓冲区为'足够大)。

最有可能的是,您将使用toString()进行调试或记录日志,而性能并不重要。但是,仍有改进的空间。考虑一下您将如何处理Circle::toString()返回的字符串?该字符串很可能会显示,写入文件或通过网络流式传输。您将执行类似cout << circle.toString()的操作。您可以直接将其写入输出流,而无需创建临时stringstream对象,将其转换为临时字符串并最终将其写入流中。

void Circle::toStream(ostream &os)
{
os << "Shape [" << index <<"]\n";
// ...
os << endl;
}

在头文件中声明 operator<<。这样就可以编写 cout << circle;
class Circle {
// ...
public:
void toStream(ostream &os);

friend ostream & operator << (ostream &os, const Circle &c) {
c.toStream(os);
return os;
}
};

如果Circle具有基类,那么在基层将 toStream声明为虚拟是一个好主意:

class Object {
public:
virtual ~Object() = default;

virtual void toStream(ostream &os) = 0;

friend ostream &operator << (ostream &os, const Object &obj) {
obj.toStream(os);
return os;
}
};

class Circle : public Object {
public:
void toStream(ostream &os) override;
};

class Rectangle : public Object {
public:
void toStream(ostream &os) override;
};

现在,您可以将任何对象打印到任何输出流,而不会降低性能,同时仍保持代码干净。

Circle circle1, circle2;
Rectangle rectangle;
// ...
// print circle to stdout
cout << "My circles:\n" << circle1 << circle2;
cout << "My rectangle:\n" << rectangle;
// write circles to a file
ofstream myfile;
myfile.open ("circle.log");
myfile << circle1 << circle2;
myfile.close();

最后,如果您毕竟需要一个字符串,则可以创建一个辅助方法:

string Circle::toString()
{
stringstream sentence;
toStream(sentence);
return sentence.str();
}

关于c++ - 还有另一种使用stringstream的方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60151574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com