如何在C ++中将int附加到字符串中? [重复]

问题描述 投票:141回答:20

这个问题在这里已有答案:

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

我想要打印Player 4

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

c++ int stdstring
20个回答
226
投票

使用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);

2
投票

为了记录,您还可以使用Qt的QString类:

#include <QtCore/QString>

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

1
投票
cout << text << i;

1
投票

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

cout << text << i;

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

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

然后将其复制到“文本”字符串。

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

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

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


0
投票
cout << text << i;

ostream的<<运算符返回对ostream的引用,因此您可以继续链接<<操作。也就是说,上面基本相同:

cout << text;
cout << i;

0
投票
cout << "Player" << i ;

0
投票
cout << text << " " << i << endl;

0
投票

您还可以尝试使用std::string::push_back连接玩家的号码:

代码示例:

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

您将在控制台中看到:

球员4


0
投票

我能想到的最简单的方法是以下.. 它将作为单个字符串和字符串数组。我正在考虑一个字符串数组,因为它很复杂(稍后会跟字符串一样)。我创建了一个名称数组,并附加了一些整数和字符,以显示将一些int和chars附加到字符串是多么容易,希望它有所帮助。长度只是为了测量数组的大小。如果您熟悉编程,那么size_t是unsigned 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;
        }


}

-1
投票

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

最简单的方法是

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开始变得更具吸引力。


-3
投票

您可以使用以下内容

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

189
投票

好吧,如果你使用cout,你可以直接写整数,就像在

std::cout << text << i;

将各种对象转换为字符串的C ++方法是通过string streams。如果你没有一个方便,只需创建一个。

#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);

这也是相反的方式,即解析字符串。


-5
投票

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

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

113
投票
printf("Player %d", i);

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

:-P


19
投票

这些适用于一般字符串(如果您不想输出到文件/控制台,但存储以供以后使用或某些东西)。

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;

9
投票

对于记录,如果要在实际输出之前创建字符串,还可以使用std::stringstream


6
投票
cout << text << " " << i << endl;

4
投票

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

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


3
投票

另一种可能性是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;
}

2
投票

这是一个小的工作转换/附加示例,其中包含我之前需要的一些代码。

#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

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

© www.soinside.com 2019 - 2024. All rights reserved.