Obe*_*ane 631 c++ int concatenation stdstring
我认为这很简单,但它存在一些困难.如果我有
std::string name = "John";
int age = 21;
Run Code Online (Sandbox Code Playgroud)
如何组合它们以获得单个字符串"John21"?
Dan*_*nyT 1069
按字母顺序排列:
std::string name = "John";
int age = 21;
std::string result;
// 1. with Boost
result = name + boost::lexical_cast<std::string>(age);
// 2. with C++11
result = name + std::to_string(age);
// 3. with FastFormat.Format
fastformat::fmt(result, "{0}{1}", name, age);
// 4. with FastFormat.Write
fastformat::write(result, name, age);
// 5. with the {fmt} library
result = fmt::format("{}{}", name, age);
// 6. with IOStreams
std::stringstream sstm;
sstm << name << age;
result = sstm.str();
// 7. with itoa
char numstr[21]; // enough to hold all numbers up to 64-bits
result = name + itoa(age, numstr, 10);
// 8. with sprintf
char numstr[21]; // enough to hold all numbers up to 64-bits
sprintf(numstr, "%d", age);
result = name + numstr;
// 9. with STLSoft's integer_to_string
char numstr[21]; // enough to hold all numbers up to 64-bits
result = name + stlsoft::integer_to_string(numstr, 21, age);
// 10. with STLSoft's winstl::int_to_string()
result = name + winstl::int_to_string(age);
// 11. With Poco NumberFormatter
result = name + Poco::NumberFormatter().format(age);
Run Code Online (Sandbox Code Playgroud)
#include <string>)#include <sstream>(来自标准C++)Jer*_*emy 253
在C++ 11中,您可以使用std::to_string,例如:
auto result = name + std::to_string( age );
Run Code Online (Sandbox Code Playgroud)
Jay*_*rod 82
如果您有Boost,则可以使用将整数转换为字符串boost::lexical_cast<std::string>(age).
另一种方法是使用stringstreams:
std::stringstream ss;
ss << age;
std::cout << name << ss.str() << std::endl;
Run Code Online (Sandbox Code Playgroud)
第三种方法是使用sprintf或snprintf来自C库.
char buffer[128];
snprintf(buffer, sizeof(buffer), "%s%d", name.c_str(), age);
std::cout << buffer << std::endl;
Run Code Online (Sandbox Code Playgroud)
其他海报建议使用itoa.这不是标准功能,因此如果您使用它,您的代码将无法移植.有些编译器不支持它.
Ben*_*ein 75
#include <iostream>
#include <sstream>
std::ostringstream o;
o << name << age;
std::cout << o.str();
Run Code Online (Sandbox Code Playgroud)
tlo*_*ach 50
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
string itos(int i) // convert int to string
{
stringstream s;
s << i;
return s.str();
}
Run Code Online (Sandbox Code Playgroud)
从http://www.research.att.com/~bs/bs_faq2.html无耻地窃取.
小智 30
这是最简单的方法:
string s = name + std::to_string(age);
Run Code Online (Sandbox Code Playgroud)
0x4*_*2D2 22
如果你有C++ 11,你可以使用std::to_string.
例:
std::string name = "John";
int age = 21;
name += std::to_string(age);
std::cout << name;
Run Code Online (Sandbox Code Playgroud)
输出:
John21
Run Code Online (Sandbox Code Playgroud)
use*_*576 18
在我看来,最简单的答案是使用该sprintf功能:
sprintf(outString,"%s%d",name,age);
Run Code Online (Sandbox Code Playgroud)
Seb*_*ose 15
#include <string>
#include <sstream>
using namespace std;
string concatenate(std::string const& name, int i)
{
stringstream s;
s << name << i;
return s.str();
}
Run Code Online (Sandbox Code Playgroud)
Zin*_*ng- 11
#include <sstream>
template <class T>
inline std::string to_string (const T& t)
{
std::stringstream ss;
ss << t;
return ss.str();
}
Run Code Online (Sandbox Code Playgroud)
然后你的用法看起来像这样
std::string szName = "John";
int numAge = 23;
szName += to_string<int>(numAge);
cout << szName << endl;
Run Code Online (Sandbox Code Playgroud)
用Google搜索 [并测试:p]
小智 9
此问题可以通过多种方式解决。我将以两种方式展示它:
使用将数字转换为字符串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;
}
Run Code Online (Sandbox Code Playgroud)在 C++20 中,您将能够执行以下操作:
auto result = std::format("{}{}", name, age);
Run Code Online (Sandbox Code Playgroud)
同时您可以使用{fmt} 库,std::format基于:
auto result = fmt::format("{}{}", name, age);
Run Code Online (Sandbox Code Playgroud)
免责声明:我是 {fmt} 库和 C++20 的作者std::format。
如果您想+用于连接任何具有输出操作符的内容,您可以提供以下模板版本operator+:
template <typename L, typename R> std::string operator+(L left, R right) {
std::ostringstream os;
os << left << right;
return os.str();
}
Run Code Online (Sandbox Code Playgroud)
然后你可以用一种简单的方式编写你的连接:
std::string foo("the answer is ");
int i = 42;
std::string bar(foo + i);
std::cout << bar << std::endl;
Run Code Online (Sandbox Code Playgroud)
输出:
the answer is 42
Run Code Online (Sandbox Code Playgroud)
这不是最有效的方法,但除非你在循环中进行大量连接,否则不需要最有效的方法.
如果您使用的是MFC,则可以使用CString
CString nameAge = "";
nameAge.Format("%s%d", "John", 21);
Run Code Online (Sandbox Code Playgroud)
托管C++也有一个 字符串格式化程序.