[英]How do you append an int to a string in C++? [duplicate]
这个问题已经在这里有了答案:
int i = 4;
string text = "Player ";
cout << (text + i);
我想要它打印Player 4
。
上面显然是错误的,但是它显示了我在这里想要做的事情。 有没有简单的方法可以做到这一点,还是我必须开始添加新的包含?
使用C ++ 11,您可以编写:
#include <string> // to use std::string, std::to_string() and "+" operator acting on strings
int i = 4;
std::string text = "Player ";
text += std::to_string(i);
好吧,如果您使用cout,您可以直接将整数写入其中,如
std::cout << text << i;
将各种对象转换为字符串的C ++方法是通过字符串流 。 如果您没有一个方便的工具,只需创建一个即可。
#include <sstream>
std::ostringstream oss;
oss << text << i;
std::cout << oss.str();
或者,您可以只转换整数并将其附加到字符串。
oss << i;
text += oss.str();
最后,Boost库提供boost::lexical_cast
,它使用类似于内置类型转换的语法来包装字符串流转换。
#include <boost/lexical_cast.hpp>
text += boost::lexical_cast<std::string>(i);
这也可以以另一种方式起作用,即解析字符串。
printf("Player %d", i);
(将我喜欢的答案全部否决;我仍然讨厌C ++ I / O运算符。)
:-P
这些适用于常规字符串(以防您不希望输出到文件/控制台,而是存储以备后用)。
boost.lexical_cast
MyStr += boost::lexical_cast<std::string>(MyInt);
串流
//sstream.h
std::stringstream Stream;
Stream.str(MyStr);
Stream << MyInt;
MyStr = Stream.str();
// If you're using a stream (for example, cout), rather than std::string
someStream << MyInt;
作为记录,如果要在实际输出字符串之前创建它,也可以使用std::stringstream
。
cout << text << " " << i << endl;
您的示例似乎表明您想显示一个字符串,后跟一个整数,在这种情况下:
string text = "Player: ";
int i = 4;
cout << text << i << endl;
会很好的。
但是,如果要存储字符串位置或将字符串位置传递,并经常这样做,则可能会受益于重载加法运算符。 我在下面演示这个:
#include <sstream>
#include <iostream>
using namespace std;
std::string operator+(std::string const &a, int b) {
std::ostringstream oss;
oss << a << b;
return oss.str();
}
int main() {
int i = 4;
string text = "Player: ";
cout << (text + i) << endl;
}
实际上,您可以使用模板使此方法更强大:
template <class T>
std::string operator+(std::string const &a, const T &b){
std::ostringstream oss;
oss << a << b;
return oss.str();
}
现在,只要对象b
具有定义的流输出,就可以将其附加到字符串(或至少其副本)上。
另一种可能性是Boost.Format :
#include <boost/format.hpp>
#include <iostream>
#include <string>
int main() {
int i = 4;
std::string text = "Player";
std::cout << boost::format("%1% %2%\n") % text % i;
}
为了记录,您还可以使用Qt的QString
类:
#include <QtCore/QString>
int i = 4;
QString qs = QString("Player %1").arg(i);
std::cout << qs.toLocal8bit().constData(); // prints "Player 4"
这是一个小的工作转换/附加示例,其中包含我之前需要的一些代码。
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
int main(){
string str;
int i = 321;
std::stringstream ss;
ss << 123;
str = "/dev/video";
cout << str << endl;
cout << str << 456 << endl;
cout << str << i << endl;
str += ss.str();
cout << str << endl;
}
输出将是:
/dev/video
/dev/video456
/dev/video321
/dev/video123
请注意,在最后两行中,您需要在修改后的字符串实际打印出来之前保存它,以后可以根据需要使用它。
如果您的问题需要输出,这里的一种方法是直接打印输出。
cout << text << i;
否则,最安全的方法之一就是使用
sprintf(count, "%d", i);
然后将其复制到您的“文本”字符串中。
for(k = 0; *(count + k); k++)
{
text += count[k];
}
因此,您拥有所需的输出字符串
有关sprintf
更多信息, 请访问 : http : //www.cplusplus.com/reference/cstdio/sprintf
cout << text << i;
我能弄清楚的最简单方法是以下方法。
它将作为单个字符串和字符串数组工作 。 我正在考虑一个字符串数组,因为它很复杂(字符串将跟在后面一样)。 我创建了一个名称数组,并在其中添加了一些整数和char,以显示在字符串中添加一些int和chars是多么容易,希望它会有所帮助。 长度只是用来衡量数组的大小。 如果您熟悉编程,则size_t是一个无符号的int
#include<iostream>
#include<string>
using namespace std;
int main() {
string names[] = { "amz","Waq","Mon","Sam","Has","Shak","GBy" }; //simple array
int length = sizeof(names) / sizeof(names[0]); //give you size of array
int id;
string append[7]; //as length is 7 just for sake of storing and printing output
for (size_t i = 0; i < length; i++) {
id = rand() % 20000 + 2;
append[i] = names[i] + to_string(id);
}
for (size_t i = 0; i < length; i++) {
cout << append[i] << endl;
}
}
您还可以尝试使用std::string::push_back
连接玩家的号码:
您的代码示例:
int i = 4;
string text = "Player ";
text.push_back(i + '0');
cout << text;
您将在控制台中看到:
玩家4
cout << "Player" << i ;
cout << text << i;
ostream的<<
操作符返回对该ostream的引用,因此您可以继续链接<<
操作。 也就是说,以上内容基本上与以下内容相同:
cout << text;
cout << i;
cout << text << " " << i << endl;
有几个选项,您要选择哪个取决于上下文。
最简单的方法是
std::cout << text << i;
或者如果您只想一行
std::cout << text << i << endl;
如果您正在编写一个单线程程序,并且您不经常调用此代码(其中“很多”是每秒数千次),那么您就完成了。
如果您正在编写一个多线程程序,并且有多个线程正在编写cout,那么这个简单的代码可能会给您带来麻烦。 假设编译器随附的库使cout线程安全到足以确保对cout线程的任何单个调用都不会被中断。 现在,假设一个线程正在使用此代码编写“ Player 1”,另一个正在编写“ Player 2”。 如果幸运的话,您将获得以下信息:
Player 1
Player 2
如果您不走运,您可能会收到类似以下的内容
Player Player 2
1
问题是std :: cout <<文本<< i << endl; 变成3个函数调用。 该代码等效于以下代码:
std::cout << text;
std::cout << i;
std::cout << endl;
如果改为使用C风格的printf,并且编译器再次提供了具有合理线程安全性的运行时库(每个函数调用都是原子的),则以下代码会更好地工作:
printf("Player %d\n", i);
能够在单个函数调用中执行某项操作,使io库可以在后台进行同步,现在您的整个文本行将被自动编写。
对于简单的程序,std :: cout很棒。 抛出多线程或其他复杂情况,不太时尚的printf开始显得更具吸引力。
您可以使用以下内容
int i = 4;
string text = "Player ";
text+=(i+'0');
cout << (text);
如果使用Windows / MFC,并且除了立即输出以外还需要该字符串,请尝试:
int i = 4;
CString strOutput;
strOutput.Format("Player %d", i);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.