简体   繁体   English

如何push_back一个整数到一个字符串?

[英]How to push_back an integer to a string?

Right now, I'm preparing to do a homework assignment by first sorting out what I'm going to do in my methods. 现在,我正在准备做家庭作业,方法是先弄清楚我要用自己的方法做的事情。 For one of them, I have to prepare a list of names to be added to a list in the form A1, B2, C3 ... etc. What I am testing right now is a way to add them via a for loop. 对于其中之一,我必须准备要添加到列表中的名称列表,形式为A1,B2,C3 ...等。我现在正在测试的是一种通过for循环添加名称的方法。 Please note that I am not doing the whole thing yet, I'm just ensuring that the items are made in the correct form. 请注意,我还没有做完所有事情,我只是确保这些物品以正确的形式制成。 I have the following code: 我有以下代码:

list<string> L; //the list to hold the data in the form A1, B2, etc.
char C = 'A'; //the char value to hold the alphabetical letters
for(int i = 1; i <= 5; i++)
{ 
    string peas; //a string to hold the values, they will be pushed backed here
    peas.push_back(C++); //adds an alphabetical letter to the temp string, incrementing on every loop
    peas.push_back(i); //is supposed to add the number that i represents to the temp string
    L.push_back(peas); //the temp string is added to the list
}

The letter chars add and increment to the value just fine (they show up as ABC etc.), but the problem I am having is that when I push_back the integer value, it doesn't actually push_back an integer value, but an ascii value related to the integer (that's my guess -- it returns emoticons). 字母char加并增加到值就好了(它们显示为ABC等),但是我遇到的问题是,当我push_back整数值时,它实际上并没有push_back一个整数值,而是一个ascii值与整数有关(这是我的猜测-它返回图释)。

I'm thinking the solution here is to turn the integer value into a char, but, so far, looking that up has been very confusing. 我在想这里的解决方案是将整数值转换为char,但是到目前为止,看起来一直很混乱。 I have tried to_string (gives me errors) and char(i) (same result as just i) but none have worked. 我尝试了to_string(给我错误)和char(i)(与i相同的结果),但是没有一个起作用。 So basically: how can I add i as a char value representing the actual integer number it holds and not as an ascii value? 所以基本上: 我如何将i添加为代表其持有的实际整数而不是ascii值的char值?

My TA usually doesn't actually read the code sent to him and the instructor takes far too long to respond, so I was hoping I could get this issue resolved here. 我的助教通常实际上并不读取发送给他的代码,而导师花费的时间太长了,因此我希望可以在这里解决此问题。

Thank you! 谢谢!

push_back appends individual characters to the string. push_back单个字符附加到字符串。 What you want is to convert a number to a string and then concatenate this string to another string. 你需要的是一个数字转换 字符串,然后拼接这个字符串到另一个字符串。 That's a fundamentally different operation. 这是根本不同的操作。

To convert a number to a string, use to_string . 要将数字转换为字符串,请使用to_string To concatenate strings, you can simply use + : 要连接字符串,只需使用+

std::string prefix = std::string(1, C++);
L.push_back(prefix + std::to_string(i));

If your compiler doesn't support C++11 yet, there's the solution using a stringstream : 如果您的编译器尚不支持C ++ 11,则可以使用stringstream解决方案:

std::ostringstream ostr;
ostr << C++ << i;
L.push_back(ostr.str());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM