我怎样才能更好地管理字符串和内存 [英] How can I better manage strings and memory

查看:58
本文介绍了我怎样才能更好地管理字符串和内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在努力改进我目前拥有的一些代码。


我有一个名为RequestMessage的简单类,例如


类RequestMessage

{

public:

RequestMessage();

~RequestMessage();


string& getMessage()


private:

string origin

string type;

string body; < br $> b $ b ....

....

string rqstMessage;

};


getMessage格式化私有成员并将它们存储在rqstMessage

中,然后返回对它的引用。


string&

RequestMessage :: getMessage()

{

ostringstream * message = new ostringstream(" Orgin:");

条消息<<起源<< endl;

message<< "类型:" <<类型<< endl;

message<< 身体:<<身体<< endl;


rqstMessage = message-> str();


返回rqstMessage;

}


我遇到的问题是,我不喜欢我已经拥有的事实

将我的数据存储在字符串中,然后使用另一个字符串来存储它所有

再次以格式化的方式。

有没有更好的方法来做到这一点,而不使用额外的内存?


干杯

Hi,

I am trying to improve some code that I currently have.

I have a simple class called RequestMessage e.g.

class RequestMessage
{
public:
RequestMessage();
~RequestMessage();

string& getMessage()

private:
string origin
string type;
string body;
....
....
string rqstMessage;
};

getMessage formats the private members and stores them in rqstMessage
before returning a reference to it.

string&
RequestMessage::getMessage()
{
ostringstream* message = new ostringstream("Orgin: ");
message << origin << endl;
message << "Type:" << type << endl;
message << "Body: << body << endl;

rqstMessage = message->str();

return rqstMessage;
}

The problem I have, is that I don''t like the fact I already have
stored my data in strings, and then use another string to store it all
again in a formatted way.
Is there a better way to do this, without using the additional memory?

Cheers

推荐答案

DeveloperDave写道:
DeveloperDave wrote:




我正在尝试改进我目前拥有的一些代码。


我有一个名为RequestMessage的简单类,例如


class RequestMessage

{

public:

RequestMessage();

~RequestMessage();


string& getMessage()


private:

string origin

string type;

string body; < br $> b $ b ....

....

string rqstMessage;

};


getMessage格式化私有成员并将它们存储在rqstMessage

中,然后返回对它的引用。


string&

RequestMessage :: getMessage()

{

ostringstream * message = new ostringstream(" Orgin:");

条消息<<起源<< endl;

message<< "类型:" <<类型<< endl;

message<< 身体:<<身体<< endl;


rqstMessage = message-> str();


返回rqstMessage;

}


我遇到的问题是,我不喜欢我已经拥有的事实

将我的数据存储在字符串中,然后使用另一个字符串来存储它所有

再次以格式化的方式。

有没有更好的方法来做到这一点,而不使用额外的内存?
Hi,

I am trying to improve some code that I currently have.

I have a simple class called RequestMessage e.g.

class RequestMessage
{
public:
RequestMessage();
~RequestMessage();

string& getMessage()

private:
string origin
string type;
string body;
....
....
string rqstMessage;
};

getMessage formats the private members and stores them in rqstMessage
before returning a reference to it.

string&
RequestMessage::getMessage()
{
ostringstream* message = new ostringstream("Orgin: ");
message << origin << endl;
message << "Type:" << type << endl;
message << "Body: << body << endl;

rqstMessage = message->str();

return rqstMessage;
}

The problem I have, is that I don''t like the fact I already have
stored my data in strings, and then use another string to store it all
again in a formatted way.
Is there a better way to do this, without using the additional memory?



了解上下文非常重要。无论如何,首先要确保你没有泄漏额外的记忆。对于ostringstream

(这不是Java)。那么,rqstMessage的作用是什么?缓存是否是
?你做过测量表明你需要*吗?如果

所以使它成为一个可变成员并使getMessage()const。

否则只要忘记它,并返回一个字符串而不是

参考。


-

Gennaro Prota | name.surname yahoo.com

Breeze C ++(预览版):< https://sourceforge.net/projects/breeze/>

你需要C ++方面的专业知识吗? ?我有空。

It would be important to know the context. Anyhow, first be sure
you don''t leak the "additional memory" for the ostringstream
(this is not Java). Then, what''s the role of rqstMessage? Is it
a cache? Did you make measurements that show you *need* it? If
so make it a mutable member and make getMessage() const.
Otherwise just forget about it, and return a string instead of a
reference.

--
Gennaro Prota | name.surname yahoo.com
Breeze C++ (preview): <https://sourceforge.net/projects/breeze/>
Do you need expertise in C++? I''m available.


DeveloperDave写道:
DeveloperDave wrote:




我正在尝试改进我目前拥有的一些代码。


我有一个名为RequestMessage的简单类,例如


class RequestMessage

{

public:

RequestMessage();

~RequestMessage();


string& getMessage()


private:

string origin

string type;

string body; < br $> b $ b ....

....

string rqstMessage;

};


getMessage格式化私有成员并将它们存储在rqstMessage

中,然后返回对它的引用。


string&

RequestMessage :: getMessage()

{

ostringstream * message = new ostringstream(" Orgin:");

条消息<<起源<< endl;

message<< "类型:" <<类型<< endl;

message<< 身体:<<身体<< endl;


rqstMessage = message-> str();


返回rqstMessage;

}
Hi,

I am trying to improve some code that I currently have.

I have a simple class called RequestMessage e.g.

class RequestMessage
{
public:
RequestMessage();
~RequestMessage();

string& getMessage()

private:
string origin
string type;
string body;
....
....
string rqstMessage;
};

getMessage formats the private members and stores them in rqstMessage
before returning a reference to it.

string&
RequestMessage::getMessage()
{
ostringstream* message = new ostringstream("Orgin: ");
message << origin << endl;
message << "Type:" << type << endl;
message << "Body: << body << endl;

rqstMessage = message->str();

return rqstMessage;
}



这是内存泄漏。你为什么要新闻?你为什么不删除它?b $ b删除它?它应该简单地声明为局部变量:


ostringstream消息;


让我猜,你来自Java?

This is a memory leak. Why are you newing message? And why are you not
deleteing it? It should simply be declared as a local variable:

ostringstream message;

Let me guess, you''re coming from Java?


10月19日,7:34 * pm,red floyd< no.spam.h ... @ example.comwrote:
On Oct 19, 7:34*pm, red floyd <no.spam.h...@example.comwrote:

DeveloperDave写道:
DeveloperDave wrote:


Hi,


我是试图改进我目前拥有的一些代码。
I am trying to improve some code that I currently have.


我有一个名为RequestMessage的简单类,例如
I have a simple class called RequestMessage e.g.


class RequestMessage

{

public:

* * RequestMessage();

* * ~RequestMessage();
class RequestMessage
{
public:
* * RequestMessage();
* * ~RequestMessage();


* * string& getMessage()
* *string& getMessage()


private:

* * string origin

* * string type;

* *串体;

* * ....

* * ....

* string rqstMessage;

};
private:
* *string origin
* *string type;
* *string body;
* *....
* *....
* string rqstMessage;
};


getMessage格式化私有成员并将它们存储在rqstMessage

中,然后再返回对它的引用。
getMessage formats the private members and stores them in rqstMessage
before returning a reference to it.


string&

RequestMessage :: getMessage()

{

* * ostringstream * message = new ostringstream(" Orgin:");

* * message<<起源<< endl;

* * message<< "类型:" <<类型<< endl;

* * message<< 身体:*<<身体<< ENDL;
string&
RequestMessage::getMessage()
{
* *ostringstream* message = new ostringstream("Orgin: ");
* *message << origin << endl;
* *message << "Type:" << type << endl;
* *message << "Body: *<< body << endl;


* * rqstMessage = * message-> str();
* *rqstMessage = *message->str();


* * return rqstMessage;

}
* *return rqstMessage;
}



这是内存泄漏。 *你为什么要新闻? *你为什么不

删除它? *它应该简单地声明为局部变量:


* * ostringstream消息;


让我猜,你来自Java的?


This is a memory leak. *Why are you newing message? *And why are you not
deleteing it? *It should simply be declared as a local variable:

* *ostringstream message;

Let me guess, you''re coming from Java?



伙计们,我不是来自java,我不想知道我应该

总是叫删除新的(我已经知道了)。这也不是最终代码,它是一个例子来说明我的问题。为什么不

评论...会导致代码不能编译吗?


问题是:


是否可以使用成员变量构造格式化

字符串,即

类似


" Orgin:%s \ nnType:%s \ nBody:% s $ \\ n"


用我的成员变量,orgin,type和

" body"替换%s,但不要将这些存储在一个新的字符串中,即不要两次存储

相同的信息。

Guys, I''m not coming from java, and I don''t want to know that I should
always call delete with new (I know this already). This also isn''t
the final code, it is an example to illustrate my problem. Why not
comment that the "..." will cause the code not to compile as well?

The question is:

is it possible to use the member variables to construct a formatted
string, i.e.
Something like

"Orgin:%s\nType:%s\nBody:%s\n"

Replacing the %s with my member variables, "orgin", "type", and
"body", but without storing these in a new string i.e. not storing the
same information twice.


这篇关于我怎样才能更好地管理字符串和内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆