C++ 三个整数组成一个带空格键的字符串;
我的意见如下:C++ 三个整数组成一个带空格键的字符串;,c++,string,int,C++,String,Int,我的意见如下: 1581 303 1127 Bravo string a="1581 303 1127"; string b="Bravo"; getline(cin, a); // read the numbers followed by a space after each number and press enter getline(cin, b); // read the string cout << a << endl << b; //
1581 303 1127 Bravo
string a="1581 303 1127";
string b="Bravo";
getline(cin, a); // read the numbers followed by a space after each number and press enter
getline(cin, b); // read the string
cout << a << endl << b; // output the string with numbers
// first then the string with the word 'Bravo'
我想把它变成这样的字符串:
1581 303 1127 Bravo
string a="1581 303 1127";
string b="Bravo";
getline(cin, a); // read the numbers followed by a space after each number and press enter
getline(cin, b); // read the string
cout << a << endl << b; // output the string with numbers
// first then the string with the word 'Bravo'
我该怎么做呢?基于这样一个事实,即前三个值是int,后三个值是string
inti1、i2、i3;
//输入三个整数
sprintf(a,“%d%d%d”,i1、i2、i3);
只要把它们当作字符串读出来,然后把它们放在一起就行了
std::string x1, x2, x3;
std::cin >> x1 >> x2 >> x3;
std::string a = x1 + " " + x2 + " " + x3;
std::string b;
std::cin >> b;
一种SimpelC++风格的方法,将使用<代码> STD::toSype
string += " "
string += std::to_string(int_value);
这会在字符串末尾添加一个“int”值
但您是否考虑使用字符串流?< /P>
#include <sstream>
std::stringstream sstream;
sstream << int_1 << " " << int_2 << std::endl;
您可以这样做:
1581 303 1127 Bravo
string a="1581 303 1127";
string b="Bravo";
getline(cin, a); // read the numbers followed by a space after each number and press enter
getline(cin, b); // read the string
cout << a << endl << b; // output the string with numbers
// first then the string with the word 'Bravo'
getline(cin,a);//读取数字,然后在每个数字后加空格,然后按enter键
getline(cin,b);//读字符串
CUT< P>标准C++中不依赖于读取值的方式是
#include <string>
#include <sstream>
int main()
{
int i1 = 1581;
int i2 = 303;
int i3 = 1127;
std::ostringstream ostr;
ostr << i1 << ' ' << i2 << ' ' << i3 << ' ' << "Bravo";
// possible to stream anything we want to the string stream
std::string our_string = ostr.str(); // get our string
std::cout << our_string << '\n'; // print it out
}
#包括
#包括
int main()
{
int i1=1581;
int i2=303;
int i3=1127;
标准:ostringstream ostr;
ostr您是将1581031127保存为int还是仅将其作为字符串?到目前为止您尝试了什么?如何工作?如何不工作?输入来自何处?前3个是int,最后一个是string“空格键”不是字符,而是按下一个空格的键。您可以使用Stravtf来使用字符串B,但是使用%s。此时不能使用C++代码库SCAFTF 使用C++标准库类。之后,STD::String S(CARARRY);这是C方法。是的,它可以用在C++中,但不被使用。