change between std::string & int demo

来源:互联网 发布:cydia怎么卸载软件 编辑:程序博客网 时间:2024/06/05 13:16
#include <iostream>#include <string>#include <sstream>using namespace std;int ReverseInteger(int n);const int SENTINEL = -1;int main(){cout << "this program reversed the digits in an integer." << endl;cout << "Enter a positive integer: " << endl;int n;while (true) {cout << "? ";cin >> n;if (n == SENTINEL) break;cout << "The reversed integer is " << ReverseInteger(n) << endl;}return 0;}int ReverseInteger(int n) {// integer => stringostringstream os;os << n;string s = os.str();// reverse stringstring s2(s.rbegin(), s.rend());// string => integeristringstream is(s2);is >> n;return n;}

0 0
原创粉丝点击