Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在C++;? inti=4; string text=“Player”; coutcoutcoutcoutcoutcoutcoutcoutcoutcout_C++_Int_Stdstring - Fatal编程技术网

C++ 如何在C++;? inti=4; string text=“Player”; coutcoutcoutcoutcoutcoutcoutcoutcoutcout

C++ 如何在C++;? inti=4; string text=“Player”; coutcoutcoutcoutcoutcoutcoutcoutcoutcout,c++,int,stdstring,C++,Int,Stdstring,对于记录,如果要在字符串实际输出之前创建字符串,也可以使用a,如果您想在字符串实际输出之前创建字符串,也可以使用。这些适用于常规字符串(如果您不想输出到文件/控制台,而是存储以供以后使用或其他情况) boost.u-cast cout << text << " " << i << endl; MyStr+=boost::词法转换(MyInt); 串流 MyStr += boost::lexical_cast<std::string>

对于记录,如果要在字符串实际输出之前创建字符串,也可以使用a,如果您想在字符串实际输出之前创建字符串,也可以使用。

这些适用于常规字符串(如果您不想输出到文件/控制台,而是存储以供以后使用或其他情况)

boost.u-cast

cout << text << " " << i << endl;
MyStr+=boost::词法转换(MyInt);
串流

MyStr += boost::lexical_cast<std::string>(MyInt);
//sstream.h
std::stringstream;
Stream.str(MyStr);

Stream这些用于一般字符串(以防您不想输出到文件/控制台,而是存储起来供以后使用或做其他事情)

boost.u-cast

cout << text << " " << i << endl;
MyStr+=boost::词法转换(MyInt);
串流

MyStr += boost::lexical_cast<std::string>(MyInt);
//sstream.h
std::stringstream;
Stream.str(MyStr);
流动

(我喜欢你的答案,我仍然讨厌C++ I/O运算符) :-p

(我喜欢你的答案,我仍然讨厌C++ I/O运算符)


:-p

如果使用cout,可以直接将整数写入其中,如

printf("Player %d", i);

这也是另一种方法,即解析字符串。

如果使用cout,您可以直接将整数写入其中,如中所示

printf("Player %d", i);

这也是另一种方法,即解析字符串。

有几个选项,您需要哪一个取决于上下文

最简单的方法是

#include <boost/lexical_cast.hpp>

text += boost::lexical_cast<std::string>(i);
如果你运气不好,你可能会得到如下结果

Player 1
Player 2
/dev/video
/dev/video456
/dev/video321
/dev/video123

问题是std::cout有几个选项,您想要哪一个取决于上下文

最简单的方法是

#include <boost/lexical_cast.hpp>

text += boost::lexical_cast<std::string>(i);
如果你运气不好,你可能会得到如下结果

Player 1
Player 2
/dev/video
/dev/video456
/dev/video321
/dev/video123
问题在于std::cout另一种可能性是:

#包括
#包括
#包括
int main(){
int i=4;
std::string text=“播放器”;
另一种可能性是:

#包括
#包括
#包括
int main(){
int i=4;
std::string text=“播放器”;

std::cout对于记录,您还可以使用Qt的
QString
类:

#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;
}
#包括
int i=4;
QString qs=QString(“玩家%1”).arg(i);

std::cout对于记录,您还可以使用Qt的
QString
类:

#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;
}
#包括
int i=4;
QString qs=QString(“玩家%1”).arg(i);

std::cout如果使用Windows/MFC,并且需要字符串进行多次即时输出,请尝试:

#include <QtCore/QString>

int i = 4;
QString qs = QString("Player %1").arg(i);
std::cout << qs.toLocal8bit().constData();  // prints "Player 4"

如果使用Windows/MFC,并且需要该字符串进行多个即时输出,请尝试:

#include <QtCore/QString>

int i = 4;
QString qs = QString("Player %1").arg(i);
std::cout << qs.toLocal8bit().constData();  // prints "Player 4"

这里是一个小的工作转换/附加示例,以及我以前需要的一些代码

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

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

这里是一个小的工作转换/追加示例,以及我之前需要的一些代码

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

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

您可以使用以下命令

Player 1
Player 2
/dev/video
/dev/video456
/dev/video321
/dev/video123
inti=4;
string text=“Player”;
文本+=(i+'0');

cout您可以使用以下方法

Player 1
Player 2
/dev/video
/dev/video456
/dev/video321
/dev/video123
inti=4;
string text=“Player”;
文本+=(i+'0');

cout您的示例似乎表明您希望显示后跟整数的字符串,在这种情况下:

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

cout您的示例似乎表明您希望显示后跟整数的字符串,在这种情况下:

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

使用C++11,您可以编写:

template <class T>
std::string operator+(std::string const &a, const T &b){
  std::ostringstream oss;
  oss << a << b;
  return oss.str();
}
#include//使用std::string、std::to_string()和作用于字符串的“+”运算符
int i=4;
std::string text=“播放器”;
text+=std::to_字符串(i);

使用C++11,您可以编写:

template <class T>
std::string operator+(std::string const &a, const T &b){
  std::ostringstream oss;
  oss << a << b;
  return oss.str();
}
#include//使用std::string、std::to_string()和作用于字符串的“+”运算符
int i=4;
std::string text=“播放器”;
text+=std::to_字符串(i);

这里的一种方法是,如果问题需要,直接打印输出

#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);
然后将其复制到“文本”字符串中

因此,您有了所需的输出字符串

有关sprintf的更多信息,请参见:

这里的一种方法是,如果问题需要,直接打印输出

#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);
然后将其复制到“文本”字符串中

因此,您有了所需的输出字符串

有关sprintf的更多信息,请参见:

我能找出这一点的最简单方法如下。
它将作为单个字符串字符串数组工作。 我正在考虑一个字符串数组,因为它很复杂(字符串后面跟一点相同)。 我创建了一个名称数组,并用它附加了一些整数和字符,以显示在字符串中附加一些整数和字符是多么容易,希望能有所帮助。 长度只是用来测量数组的大小。如果您熟悉编程,那么size\u t是一个无符号的int

for(k = 0; *(count + k); k++)
{ 
  text += count[k]; 
} 
#包括
#包括
使用名称空间std;
int main(){
字符串名称[]={“amz”、“Waq”、“Mon”、“Sam”、“Has”、“Shak”、“GBy”};//简单数组
int length=sizeof(names)/sizeof(names[0]);//给出数组的大小
int-id;
字符串append[7];//因为长度是7,只是为了存储和打印输出
对于(大小i=0;icout我能找出这一点的最简单方法如下..
它将作为单个字符串字符串数组工作。 我正在考虑一个字符串数组,因为它很复杂(字符串后面跟一点相同)。 我创建了一个名称数组
#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]; 
} 
#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;
        }


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