C++ 如何将文字与整数连接起来
C++ How do I concatenate literals with integers?
我正在尝试将文字与整数连接起来。
问题是当使用 + 运算符将文字与整数连接时,它会告诉我"error: invalid operands of types 'const char*' and 'const char [17]' to binary 'operator+
'"。
这是相关代码:
if ( ( A == 0 ) | ( B == 0 ) ) {
cout << "Sorry, gcd(" + A + ',' + B + ") is undefined.n";
return 0;
}
这里不需要串联,让cout
为您完成所有繁重的工作 - 毕竟它的<<
操作员可以处理int
!
cout << "Sorry, gcd(" << A << ',' << B << ") is undefined.n";
使用您提供的代码片段的最简单方法:
if( ( A == 0 ) || ( B == 0 ) ){
cout << "Sorry, gcd(" << A << ',' << B << ") is undefined.n";
return 0;
}
请注意,您的or
声明不正确。你错过了第二个"|
">
您可以使用std::stringstream
:
std::stringstream result;
result << "A string plus a number: " << 33;
如果您想在其他地方使用它,请string
:
std::string s = result.str();
相关文章:
- 您将如何连接"on the fly"文本+整数并将其传递给函数?
- 在 c++ 中连接字符串和整数,以便在 C++ 11 不支持计算机的情况下读取多个文件
- Vscode/C++ - cout 无法输出完整的句子,也无法与整数连接
- 如何编写模板函数来添加整数但连接字符串和字符类型?
- QT按钮组和可检查的按钮:如何将切换信号与整数和布尔值连接?
- 如何将多个字符连接成一个整数
- C++ 如何将文字与整数连接起来
- 如何在不使用对象的情况下连接字符串、整数和浮点数
- 如何在C 中连接整数和打印
- 连接整数
- wostringstream,Ascii,Unicode,Win32和整数连接到字符串
- 将带有时间戳的向量整数连接为C++中的字符串
- 如何将 3 个整数连接成无符号长整型
- 将字符串变量与整数和文本字符串连接起来的 C++ 字符串流
- 一个简单的 C++ 连接/拆分字符串和 char 数组中的整数
- 如何快速将整数simmilar连接到连接字符串
- C++整数连接
- 如何将整数变量连接到字符串文字
- 如何连接字符串和整数以生成文件名
- 将整数连接到字符串