创建包含多个变量的大字符串的最佳方法?

发布于 2024-08-18 04:14:33 字数 455 浏览 3 评论 0原文

我想创建一个包含许多变量的字符串:

std::string name1 = "Frank";
std::string name2 = "Joe";
std::string name3 = "Nancy";
std::string name4 = "Sherlock";

std::string sentence;

sentence =   name1 + " and " + name2 + " sat down with " + name3;
sentence += " to play cards, while " + name4 + " played the violin.";

这应该生成一个句子,内容为

弗兰克和乔与南希一起坐下来打牌,而夏洛克拉小提琴。

我的问题是:最佳的是什么方法来完成这个?我担心不断使用 + 运算符效率低下。有更好的办法吗?

I want to create a string that contains many variables:

std::string name1 = "Frank";
std::string name2 = "Joe";
std::string name3 = "Nancy";
std::string name4 = "Sherlock";

std::string sentence;

sentence =   name1 + " and " + name2 + " sat down with " + name3;
sentence += " to play cards, while " + name4 + " played the violin.";

This should produce a sentence that reads

Frank and Joe sat down with Nancy to play cards, while Sherlock played the violin.

My question is: What is the optimal way to accomplish this? I am concerned that constantly using the + operator is ineffecient. Is there a better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淡水深流 2024-08-25 04:14:33

是的,std::stringstream,例如:

#include <sstream>
...

std::string name1 = "Frank";
std::string name2 = "Joe";
std::string name3 = "Nancy";
std::string name4 = "Sherlock";

std::ostringstream stream;
stream << name1 << " and " << name2 << " sat down with " << name3;
stream << " to play cards, while " << name4 << " played the violin.";

std::string sentence = stream.str();

Yes, std::stringstream, e.g.:

#include <sstream>
...

std::string name1 = "Frank";
std::string name2 = "Joe";
std::string name3 = "Nancy";
std::string name4 = "Sherlock";

std::ostringstream stream;
stream << name1 << " and " << name2 << " sat down with " << name3;
stream << " to play cards, while " << name4 << " played the violin.";

std::string sentence = stream.str();
怪我闹别瞎闹 2024-08-25 04:14:33

您可以使用 boost::format 来实现:

http:// www.boost.org/doc/libs/1_41_0/libs/format/index.html

std::string result = boost::str(
    boost::format("%s and %s sat down with %s, to play cards, while %s played the violin")
      % name1 % name2 % name3 %name4
)

这是一个非常简单的例子,说明了 boost::format 的功能,它是一个非常强大的库。

You could use boost::format for this:

http://www.boost.org/doc/libs/1_41_0/libs/format/index.html

std::string result = boost::str(
    boost::format("%s and %s sat down with %s, to play cards, while %s played the violin")
      % name1 % name2 % name3 %name4
)

This is is a very simple example of what boost::format can do, it is a very powerful library.

冷月断魂刀 2024-08-25 04:14:33

您可以在临时变量上调用诸如 operator+= 之类的成员函数。不幸的是,它的结合性是错误的,但我们可以用括号来修复它。

std::string sentence(((((((name1  +  " and ")
                        += name2) += " sat down with ")
                        += name3) += " to play cards, while ")
                        += name4) += " played the violin.");

它有点丑陋,但它不涉及任何不需要的临时对象。

You can call member functions like operator+= on temporaries. Unfortunately, it has the wrong associativity, but we can fix that with parenthesis.

std::string sentence(((((((name1  +  " and ")
                        += name2) += " sat down with ")
                        += name3) += " to play cards, while ")
                        += name4) += " played the violin.");

It's a little ugly, but it doesn't involve any unneeded temporaries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文