int i = 4;
string text = "Player ";
cout << (text + i);

我想打印参与人4。

上面显然是错误的,但它显示了我在这里要做的事情。是否有一个简单的方法来做到这一点,或者我必须开始添加新的包含?


cout << "Player" << i ;

cout << text << i;

cout << text << " " << i << endl;

cout << text << i;

ostream的<<操作符返回ostream的引用,因此可以继续链接<<操作。也就是说,以上基本等同于:

cout << text;
cout << i;

cout << text << " " << i << endl;

为了记录,如果您想在实际输出字符串之前创建字符串,还可以使用std::stringstream。


这些适用于一般字符串(以防你不想输出到文件/控制台,而是存储以供以后使用)。

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;

printf("Player %d", i);

(你可以给我的答案投票;我仍然讨厌c++的I/O操作符。)

:-P


如果你用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,它使用类似于内置类型强制转换的语法包装stringstream转换。

#include <boost/lexical_cast.hpp>

text += boost::lexical_cast<std::string>(i);

这也适用于其他方式,即解析字符串。


有几个选项,您想要哪个取决于上下文。

最简单的方法是

std::cout << text << i;

或者你想把它写在一条直线上

std::cout << text << i << endl;

如果你正在编写一个单线程程序,如果你不经常调用这段代码(这里的“很多”是每秒数千次),那么你就完成了。

如果您正在编写一个多线程程序,并且有多个线程正在向cout写入内容,那么这段简单的代码可能会给您带来麻烦。让我们假设编译器附带的库使cout线程足够安全,任何对它的单独调用都不会被中断。现在让我们假设一个线程正在使用这段代码编写“参与人1”,另一个线程正在编写“参与人2”。如果你幸运的话,你会得到以下信息:

Player 1
Player 2

如果你运气不好,你可能会得到如下的结果

Player Player 2
1

问题是std::cout << text << i << endl;变成3个函数调用。代码等价于以下内容:

std::cout << text;
std::cout << i;
std::cout << endl;

如果你使用c风格的printf,并且你的编译器再次提供了一个具有合理线程安全性的运行时库(每个函数调用都是原子的),那么下面的代码会更好地工作:

printf("Player %d\n", i);

能够在单个函数调用中执行某些操作,使得io库能够在幕后提供同步,现在您的整行文本都将被原子地写入。

对于简单的程序,std::cout非常好。再加上多线程或其他复杂程序,不那么时尚的printf看起来就更有吸引力了。


另一种可能是Boost。格式:

#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"

如果使用Windows/MFC,并且需要字符串的即时输出,请尝试:

int i = 4;
CString strOutput;
strOutput.Format("Player %d", i);

这里有一个小的转换/附加示例,其中有一些我以前需要的代码。

#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

请注意,在最后两行中,您在实际打印出修改后的字符串之前保存了它,如果需要,您可以稍后使用它。


你可以使用下面的方法

int i = 4;
string text = "Player ";
text+=(i+'0');
cout << (text);

你的例子似乎表明你想要显示一个字符串后面跟着一个整数,在这种情况下:

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有一个已定义的流输出,您就可以将它附加到您的字符串(或者至少是其副本)。


使用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 << text << i;

否则,最安全的方法之一就是使用

sprintf(count, "%d", i);

然后复制它到你的“文本”字符串。

for(k = 0; *(count + k); k++)
{ 
  text += count[k]; 
} 

这样,您就有了所需的输出字符串

有关sprintf的更多信息,请参见: http://www.cplusplus.com/reference/cstdio/sprintf


我能想到的最简单的方法是: 它将作为单个字符串和字符串数组工作。 我正在考虑一个字符串数组,因为它是复杂的(有点相同将跟随字符串)。 我创建了一个名称数组,并附加了一些整数和字符与它,以显示它是多么容易附加一些int和字符字符串,希望它有帮助。 长度只是用来衡量数组的大小。如果您熟悉编程,那么size_t是一个无符号整型

#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