小编jon*_*ark的帖子

C++ stringstream >> int返回零

我正在写一个函数来返回一个数字的反转,即它转换int(1234)int(4321).这就是我目前所拥有的:

#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; …
Run Code Online (Sandbox Code Playgroud)

c++ integer stringstream

4
推荐指数
1
解决办法
1508
查看次数

标签 统计

c++ ×1

integer ×1

stringstream ×1