Write a function that is not defined in the STL (if it can be compiled)

Source: Internet
Author: User

Instance target: This example describes how to write a function that is not defined in the Standard Template Library STL. Here are two examples, one is to convert all data types into string to_string functions, and the other is its "inverse function" from_string function, used to convert a string to a specific type of content: in std:: The string template does not contain some functions that are more practical, because it is easy for the customer to write the code. For example, you can implement a to_string function code that converts all data types to string literals, as follows: #include &LT;IOSTREAM&GT;TEMPLATE&LT;CLASS&NBSP;T&GT;INLINE&NBSP;STD:: String to_string (Const t& value) { std::stringstream streamout; streamout< <value; return streamout.str ();} For special handling of the original data type String type Template<class t>inline std::string to_string (CONST&NBSP;STD:: String& value) { return value;} The/* function is used for all C + + data types, and for classes that support the "<<" operator, of course, it is also easy to implement functions that are opposite to the to_string function (see the following compilation results for ease)  */template<class  t>inline t from_string (Const std::string& toconvert) { std::stringstream  streamin (Toconvert);  t returnvalue=t (); streamin>>returnvalue; return  returnvalue;} The/*from_string function converts a string to a type, for example, it can convert a string to an integer but please, Mr. Schoolgirl note: From_string assumes that there is only one value in the string to be converted, any extraThe value will be ignored if I can run the program, I think I will get the result of  */int main () { int n=4; std::string str=to_string (n);   // long l=3245; str=to_string (L); n=from_string<int> ("a");  int  ntest=from_string<int> ("4354");// int ntest1=from_string<int> ("34y3");// int  ntest2=from_string<int> ("35 89");//  89 will be ignored  return 0;} --------------------Configuration: stl1 - win32 debug--------------------Compiling ... STL1. Cppc:\ accepts party \STL1. CPP (8)  : error C2079:  ' streamout '  uses undefined class  ' Basic_ Stringstream<char,struct std::char_traits<char>,class std::allocator<char> > '         c:\ accepts party \STL1. CPP (+)  : see reference to function template instantiation  ' class  std::basic_string<char,struct std::char_traits<char>,class sTd::allocator<char> > __cdecl to_string (const int &) '  being  Compiledc:\ accepts party \STL1. CPP (9)  : warning C4552:  ' << '  : operator has no effect;  expected operator with side-effect        c:\ recipient \ STL1. CPP (+)  : see reference to function template instantiation  ' class  std::basic_string<char,struct std::char_traits<char>,class std::allocator<char>  > __cdecl to_string (const int &) '  being compiledc:\ Party \stl1.cpp (10)  : error C2228: left of  '. Str '  must have class/struct/union  Type        c:\ accepts party \STL1. CPP (+)  : see reference to function template instantiation  ' class &NBSP;STD::BASIC_STRING&LT;CHAR,STRUCT&NBSP;STD:: char_traits<char>,class std::allocator<char> > __cdecl to_string (const  int &) '  being compiledc:\ accepts the party \STL1. CPP (8)  : error C2079:  ' streamout '  uses undefined class  ' Basic_ Stringstream<char,struct std::char_traits<char>,class std::allocator<char> > '         c:\ accepts party \STL1. CPP  : see reference to function template instantiation  ' class  std::basic_string<char,struct std::char_traits<char>,class std::allocator<char>  > __cdecl to_string (const long &) '  being compiledc:\ Party \stl1.cpp (9)  : warning C4552:  ' << '  : operator has no effect;  Expected operator with side-effect        c:\ recipient \ STL1. CPP ( : see&)nbsp;reference to function template instantiation  ' class std::basic_string< Char,struct std::char_traits<char>,class std::allocator<char> > __cdecl  to_string (const long &) '  being compiledc:\ receiver \STL1. CPP  : error C2228: left of  '. Str '  must have class/struct/ Union type        c:\ accepts party \STL1. CPP  : see reference to function template instantiation  ' class  std::basic_string<char,struct std::char_traits<char>,class std::allocator<char>  > __cdecl to_string (const long &) '  being compiledc:\ Party \stl1.cpp (26 )  : error C2079:  ' streamin '  uses undefined class  ' Basic_stringstream <char,struct std::char_traits<char>,class std::allocator<char>&nbsP;> '         c:\ accepts party \STL1. CPP (+)  : see reference to function template instantiation  ' int _ _cdecl from_string (Const class std::basic_string<char,struct std::char_traits<char >,class std::allocator<char> > &) '  being compiledc:\ \stl1.cpp (26)  : error C2440:  ' initializing '  : cannot convert from  ' const  class std::basic_string<char,struct std::char_traits<char>,class std::allocator< Char> > '  to  ' int '         No  User-defined-conversion operator available that can perform this conversion , or the operator cannot be called         c:\ accepts party \STL1. CPP ( : see reference to function&)nbsp;template instantiation  ' Int __cdecl from_string (const class std::basic_ string<char,struct std::char_traits<char>,class std::allocator<char> >  &) '  being compiledc:\ accepts the party \STL1. CPP  : warning C4552:  ' >> '  : operator has no effect;  expected operator with side-effect        c:\ recipient \ STL1. CPP (+)  : see reference to function template instantiation  ' int _ _cdecl from_string (Const class std::basic_string<char,struct std::char_traits<char >,class std::allocator<char> > &) '  being compilederror executing  cl.exe.stl1.exe - 6 error (s),  3 warning (s)

Write a function that is not defined in the STL (if it can be compiled)

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.