[英]Concatenation operator in C++?
I have an application in which I need to combine strings within a variable like so: 我有一个应用程序,需要在这样的变量中组合字符串:
int int_arr[4];
int_arr[1] = 123;
int_arr[2] = 456;
int_arr[3] = 789;
int_arr[4] = 10;
std::string _string = "Text " + int_arr[1] + " Text " + int_arr[2] + " Text " + int_arr[3] + " Text " + int_arr[4];
It gives me the compile error 它给了我编译错误
Error C2210: '+' Operator cannot add pointers" on the second string of the expression.
As far as I can tell I am combining string literals and integers, not pointers. 据我所知,我是在组合字符串文字和整数,而不是指针。
Is there another concatenation operator that I should be using? 我应该使用另一个串联运算符吗? Or is the expression just completely wrong and should figure out another way to implement this?
还是该表达方式完全错误,应该找出另一种实现方式?
BTW I am using Visual Studio 2010 顺便说一句我正在使用Visual Studio 2010
Neither C nor C++ allow concatenation of const char *
and int
. C和C ++都不允许
const char *
和int
串联。 Even C++'s std::string
, doesn't concatenate integers. 即使C ++的
std::string
也不连接整数。 Use streams instead: 改用流:
std::stringstream ss;
ss << "Text " << int_arr[1] << " Text " << int_arr[2] << " Text " << int_arr[3] << " Text " << int_arr[4];
std::string _string = ss.str();
You can do this in Java since it uses the toString()
method automatically on each part. 您可以在Java中执行此操作,因为它在每个部分上自动使用
toString()
方法。
If you want to do it the same way in C++, you'll have to explicitly convert those integer to strings in order for this to work. 如果要在C ++中以相同的方式执行此操作,则必须将这些整数显式转换为字符串,以使其工作。
Something like: 就像是:
#include <iostream>
#include <sstream>
std::string intToStr (int i) {
std::ostringstream s;
s << i;
return s.str();
}
int main (void) {
int var = 7;
std::string s = "Var is '" + intToStr(var) + "'";
std::cout << s << std::endl;
return 0;
}
Of course, you can just use: 当然,您可以使用:
std::ostringstream os;
os << "Var is '" << var << "'";
std::string s = os.str();
which is a lot easier. 这要容易得多。
A string literal becomes a pointer in this context. 字符串文字在此上下文中成为指针。 Not a
std::string
. 不是
std::string
。 (Well, to be pedantically correct, string literals are character arrays, but the name of an array has an implicit conversion to a pointer. One predefined form of the +
operator takes a pointer left-argument and an integral right argument, which is the best match, so the implicit conversion takes place here. No user-defined conversion can ever take precedence over this built-in conversion, according to the C++ overloading rules.). (从字面上看,字符串字面量是字符数组,但数组名称具有对指针的隐式转换。
+
运算符的一种预定义形式采用指针左参数和整数右参数,即最佳匹配,因此将在此处进行隐式转换。根据C ++重载规则,任何用户定义的转换都不能优先于此内置转换。)
You should study a good C++ book, we have a list here on SO. 您应该学习一本不错的C ++书籍,此处有关于SO 的列表 。
A string literal is an expression returning a pointer const char*
. 字符串文字是返回指针
const char*
的表达式。
std::stringstream _string_stream;
_string_stream << "Text " << int_arr[1] << " Text " << int_arr[2] << " Text " << int_arr[3] << " Text " << int_arr[4];
std::string _string = _string_stream.str();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.