如何形成 C++来自字符串文字串联的字符串?

发布于 2024-11-14 03:38:40 字数 300 浏览 4 评论 0原文

我想连接字符串文字和整数,如下所示:
string message("该值应介于 " + MIN_VALUE + " 和 " + MAX_VALUE);

但这给了我这个错误:

错误:类型 'const char*' 和 ' 的操作数无效const char [6]' 到二进制 'operator+'|

正确的方法是什么?我可以将其拆分为 2 个字符串声明(每个字符串声明连接一个字符串文字和一个 int),但这很丑陋。我也尝试过<<操作员。

谢谢

I would like concatenate string literals and ints, like this:
string message("That value should be between " + MIN_VALUE + " and " + MAX_VALUE);

But that gives me this error:

error: invalid operands of types ‘const char*’ and ‘const char [6]’ to binary ‘operator+’|

What is the correct way to do that? I could split that in 2 string declarations (each concatenating a string literal and a int), but that's ugly. I've also tried << operator.

Thanks

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

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

发布评论

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

评论(5

嗫嚅 2024-11-21 03:38:40

您可能应该为此使用 stringstream。

#include <sstream>

std::stringstream s;
s << "This value shoud be between " << MIN_VALUE << " and " << MAX_VALUE;
message = s.str();

You should probably use stringstream for this.

#include <sstream>

std::stringstream s;
s << "This value shoud be between " << MIN_VALUE << " and " << MAX_VALUE;
message = s.str();
娜些时光,永不杰束 2024-11-21 03:38:40

C++ 方法是使用 stringstream 然后你可以使用 stringstream <操作员。它会给你更一致的代码感觉

The c++ way to do this is to use a stringstream then you can use the << operator. It will give you a more consistent code feel

夏尔 2024-11-21 03:38:40

有很多方法可以做到这一点,但我最喜欢的是:

string message(string("That value should be between ") + MIN_VALUE + " and " + MAX_VALUE);

第一个文字周围额外的 string() 使世界变得不同,因为有一个重载的 string::operator+(const char*) 返回一个 string,而 operator+ 具有从左到右的关联性,因此整个事情变成了一个 operator+ 链 调用。

There are many ways to do this, but my favourite is:

string message(string("That value should be between ") + MIN_VALUE + " and " + MAX_VALUE);

That extra string() around the first literal makes all the difference in the world because there is an overloaded string::operator+(const char*) which returns a string, and operator+ has left-to-right associativity, so the whole thing is turned into a chain of operator+ calls.

卖梦商人 2024-11-21 03:38:40
#include <sstream>
#include <string>

template <typename T>
std::string Str( const T & t ) {
     std::ostringstream os;
     os << t;
     return os.str();
}



std::string message = "That value should be between " + Str( MIN_VALUE ) 
                       + " and " + Str( MAX_VALUE );
#include <sstream>
#include <string>

template <typename T>
std::string Str( const T & t ) {
     std::ostringstream os;
     os << t;
     return os.str();
}



std::string message = "That value should be between " + Str( MIN_VALUE ) 
                       + " and " + Str( MAX_VALUE );
愚人国度 2024-11-21 03:38:40

您可能想使用这样的字符串流:

std::stringstream msgstream;
msgstream << "That value should be between " << MIN_VALUE << " and " <<  MAX_VALUE;
std::string message(msgstream.c_str());

You probably want to use a stringstream like this:

std::stringstream msgstream;
msgstream << "That value should be between " << MIN_VALUE << " and " <<  MAX_VALUE;
std::string message(msgstream.c_str());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文