C++ stringstream >> int returns zero

天大地大妈咪最大 提交于 2019-12-10 16:35:45

问题


I'm writing a function to return the reverse of a number i.e it converts int(1234) to int(4321). This is what I have currently:

#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>

using namespace std;


int reverse(int num) {
  stringstream ss (stringstream::in | stringstream::out);
  string initial; 
  int reversed;

  // read the number in to a string stream
  ss << num;
  initial = ss.str();

  // flush the stringstream
  ss.str("");
  for(unsigned int i(0); i <= initial.size(); i++) {
    ss << initial[initial.size() - i];
  }
  ss >> reversed;

  return reversed;
}


int main(int argc, const char *argv[])
{
  int test = 9871;
  cout << "test = " << test << endl;
  cout << "reverse = " << reverse(test) << endl;

  return 0;
}

However this just outputs:

test = 9871
reverse = 0

And I'm pretty certain the problem is in the line ss >> reversed is the problem in that reversed is being set to 0 instead of the value of ss, but I can't figure out what's wrong with this code, and it's infuriating as it seems like it should be simple. Can anyone help?

Thanks


回答1:


i starts from 0, then initial.size() - i is out of string bounds.

Change to ss << initial[initial.size() - i - 1]; and iterate i from 1 to initiali.size() - 1

for(unsigned i = 0; i != initial.size(); ++i) {
  ss << initial[initial.size() - i -1];
}

Or iterate i from 1 to initial.size()

for(unsigned i = 1; i <= initial.size(); ++i) {
  ss << initial[initial.size() - i];
}



回答2:


The for loop results in out of bounds access on initial. In addition to problem pointed out by Alessandro Pezzato the terminating condition in the for loop needs changed to i < initial.size() otherwise an out of bounds will still occur:

for(unsigned int i(0); i < initial.size(); i++) {
    ss << initial[initial.size() - i - 1];
}



回答3:


The quickest C++11 way to perform what you want is:

string s = std::to_string(my_int);
std::reverse(begin(s), end(s));
return std::stoi(s);



回答4:


Your problem stems from the way you index into your array, you always need to know in the back of your head that indexing arrays in C/C++ and many similar languages is zero-based because it allows for some counting issues to go away common when starting at one.

If the size of your string is 16 characters, that means that indices of that particular string max out at 16-1=15, giving the range [0,15]. In general, it's size() - 1. If initial[initial.size() - 1 - i] looks dirty to you, you can always set it into a temporary variable like maxIndex.

int maxIndex = initial.size() - 1;
for(unsigned int i = 0; i <= maxIndex; i++) 
{
    ss << initial[maxIndex - i]; 
}



回答5:


It seems to me that a string conversion makes this more complex. I think I'd do a direct conversion from int to int:

int reverse(int input) {
    static const int base = 10;
    int ret = 0;

    while (input) { 
        ret = ret * base + input % base;
        input /= base;
    }
    return ret;
}

Note that you'd have to get (a tiny bit) more elaborate to handle negative numbers correctly. I used int as the input and output type because the original did -- but like the original this only really produces sensible results for non-negative inputs.



来源:https://stackoverflow.com/questions/10819161/c-stringstream-int-returns-zero

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!