Conversion of C + + string to int and other types

Source: Internet
Author: User

See there are many implementations of this on the web, and will involve the processing of details. For the convenience of later use, this provides functions that can be used directly.

Resources:

1: Explain C + + StringStream (Details): http://blog.csdn.net/leonardwang/article/details/4881122

2:c++ template (very detailed, there are examples): http://www.cnblogs.com/gaojun/archive/2010/09/10/1823354.html

1#include <iostream>2#include <sstream>3 using namespacestd;4 5 6 //because StringStream constructs and parsing functions are time-consuming, try to create only one. 7Template <classType>//the type to be converted with string8Type Stringandtype (Type strtmp) {//string and other types convert to each other9 StringStream stream;TenStream <<strtmp; One Type typetmp; AStream >>typetmp; -Stream.clear ();//just reset the status flag of the StringStream and did not clear the data -STREAM.STR ("");//Clear Data the     returntypetmp; - } -  - intMain () {//Test +  -     stringstrtmp; +     intinttmp; A __int64 int64tmp; at     Doubledbetmp; -  -      while(true) { -cout <<"-----int to string-----"<<Endl; -CIN >>inttmp; -cout << Stringandtype (inttmp) <<Endl; incout <<"-----String to int-----"<<Endl; -CIN >>strtmp; tocout << Stringandtype (strtmp) <<Endl; +  -  thecout <<"-----Double to string-----"<<Endl; *CIN >>dbetmp; $cout << Stringandtype (dbetmp) <<Endl;Panax Notoginsengcout <<"-----String to double-----"<<Endl; -CIN >>strtmp; thecout << Stringandtype (strtmp) <<Endl; +  A  thecout <<"-----Int64 to string-----"<<Endl; +CIN >>int64tmp; -cout << Stringandtype (int64tmp) <<Endl; $cout <<"-----String to Int64-----"<<Endl; $CIN >>strtmp; -cout << Stringandtype (strtmp) <<Endl; -cout <<"--------------------------End-------------------------"<<Endl; the     } -     return 0;Wuyi}

Conversion of C + + string to int and other types

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.