我以为这很简单,但它却有些困难。如果我有
std::string name = "John";
int age = 21;
如何将它们合并为一个字符串"John21"
?
我以为这很简单,但它却有些困难。如果我有
std::string name = "John";
int age = 21;
如何将它们合并为一个字符串"John21"
?
#include <sstream>
template <class T>
inline std::string to_string (const T& t)
{
std::stringstream ss;
ss << t;
return ss.str();
}
std::string szName = "John";
int numAge = 23;
szName += to_string<int>(numAge);
cout << szName << endl;
搜索了一下 [并测试了一下 :p ]
这个问题可以用许多方法来解决。我将展示两种方式:
使用to_string(i)
函数将数字转换为字符串。
使用字符串流。
代码:
#include <string>
#include <sstream>
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int main() {
string name = "John";
int age = 21;
string answer1 = "";
// Method 1). string s1 = to_string(age).
string s1=to_string(age); // Know the integer get converted into string
// where as we know that concatenation can easily be done using '+' in C++
answer1 = name + s1;
cout << answer1 << endl;
// Method 2). Using string streams
ostringstream s2;
s2 << age;
string s3 = s2.str(); // The str() function will convert a number into a string
string answer2 = ""; // For concatenation of strings.
answer2 = name + s3;
cout << answer2 << endl;
return 0;
}
name += std::to_string(age);
即可。如果你想使用+
来拼接任何具有输出运算符的内容,你可以提供一个模板版本的operator+
:
template <typename L, typename R> std::string operator+(L left, R right) {
std::ostringstream os;
os << left << right;
return os.str();
}
std::string foo("the answer is ");
int i = 42;
std::string bar(foo + i);
std::cout << bar << std::endl;
输出:
the answer is 42
这不是最有效的方法,但除非你在循环中做很多拼接操作,否则不需要最有效的方法。
std::string
,因此在字符串不能转换为所需类型的表达式中,它不会是候选项。例如,这个operator+
不能用于int x = 5 + 7;
中的+
。总之,我不会定义这样一个运算符,除非有非常强烈的理由,但我的目标是提供一个与其他答案不同的答案。 - uckelmanstd::string
、std::string_view
和const char *
)的模板应该足够。 - Kai PetzkeCString nameAge = "";
nameAge.Format("%s%d", "John", 21);
托管 C++ 也有一个 字符串格式化器。
QString string = QString("Some string %1 with an int somewhere").arg(someIntVariable);
string.append(someOtherIntVariable);
现在,字符串变量中%1的值已被someIntVariable的值替换,并且在末尾添加了someOtherIntVariable的值。
std::ostringstream是一个很好的方法,但有时候这个额外的技巧可能会变得方便,将格式转换为一行:
#include <sstream>
#define MAKE_STRING(tokens) /****************/ \
static_cast<std::ostringstream&>( \
std::ostringstream().flush() << tokens \
).str() \
/**/
int main() {
int i = 123;
std::string message = MAKE_STRING("i = " << i);
std::cout << message << std::endl; // prints: "i = 123"
}
#define
而不是template
?使用可变参数模板,甚至可以传递多个标记。但我不确定是否要使用它,因为在<<
操作数的输出上进行static_cast
回到std::ostringstream
有点不安全。按照惯例,所有输出器都返回对原始流对象的引用,但这在标准中并没有得到保证。 - Kai Petzke
#include <sstream>
std::ostringstream s;
s << "John " << age;
std::string query(s.str());
std::string query("John " + std::to_string(age));
#include <boost/lexical_cast.hpp>
std::string query("John " + boost::lexical_cast<std::string>(age));
有更多的选项可以用来将整数(或其他数字对象)与字符串连接起来。这是Boost.Format。
#include <boost/format.hpp>
#include <string>
int main()
{
using boost::format;
int age = 22;
std::string str_age = str(format("age is %1%") % age);
}
来自Boost.Spirit(v2)的Karma
#include <boost/spirit/include/karma.hpp>
#include <iterator>
#include <string>
int main()
{
using namespace boost::spirit;
int age = 22;
std::string str_age("age is ");
std::back_insert_iterator<std::string> sink(str_age);
karma::generate(sink, int_, age);
return 0;
}
string name = "John";
int age = 5;
char temp = 5 + '0';
name = name + temp;
cout << name << endl;
Output: John5
Boost::lexical_cast
,std::stringstream
,std::strstream
(已弃用)以及sprintf
与snprintf
的比较。 - Fred Larson