编写STL中没有定义的函数(如果能编译)

实例目标:本例介绍如何编写标准模板库STL中没有定义的函数。这里列举两个例子,一个是将所有数据类型转换成字符串型的to_string函数,另一个是它的“反函数”from_string函数,用来将字符串转化为某一类型
具体内容:
在std::string模板中没有包含一些函数实用性较强的,原因就是因为客户轻易编写其中的代码。例如:可以实现一个将所有数据类型转换为字符串型的to_string函数代码如下:
#include<iostream>
template<class T>
inline std::string to_string(const T& Value)
{
 std::stringstream streamOut;
 streamOut<<Value;
 return streamOut.str();
}
//对原数据类型为string类型的特殊处理
template<class T>
inline std::string to_string(const std::string& Value)
{
 return Value;
}
/*函数对所有C++的数据类型均使用,对支持“<<"运算符的类同样适用
当然,实现与to_string功能相反的函数也是容易的(说的轻松,请看
下面的编译结果)
 
*/
template<class T>
inline T from_string(const std::string& ToConvert)
{
 std::stringstream streamIn(ToConvert);
 T ReturnValue=T();
 streamIn>>ReturnValue;
 return ReturnValue;
}
/*from_string函数将字符串转换成某一类型,例如,它可以将字符串转换成整型
但是请先生女生注意:from_string假设要转换的字符串中仅有一个值,
任何多余的值将被忽略,如果我能运行该程序,我想我会得到结果的
 */
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将被忽略
 return 0;
}

--------------------Configuration: STL1 - Win32 Debug--------------------
Compiling...
STL1.CPP
C:\接受方\STL1.CPP(8) : error C2079: 'streamOut' uses undefined class 'basic_stringstream<char,struct std::char_traits<char>,class std::allocator<char> >'
        C:\接受方\STL1.CPP(40) : 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 compiled
C:\接受方\STL1.CPP(9) : warning C4552: '<<' : operator has no effect; expected operator with side-effect
        C:\接受方\STL1.CPP(40) : 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 compiled
C:\接受方\STL1.CPP(10) : error C2228: left of '.str' must have class/struct/union type
        C:\接受方\STL1.CPP(40) : 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 compiled
C:\接受方\STL1.CPP(8) : error C2079: 'streamOut' uses undefined class 'basic_stringstream<char,struct std::char_traits<char>,class std::allocator<char> >'
        C:\接受方\STL1.CPP(42) : 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 compiled
C:\接受方\STL1.CPP(9) : warning C4552: '<<' : operator has no effect; expected operator with side-effect
        C:\接受方\STL1.CPP(42) : 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 compiled
C:\接受方\STL1.CPP(10) : error C2228: left of '.str' must have class/struct/union type
        C:\接受方\STL1.CPP(42) : 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 compiled
C:\接受方\STL1.CPP(26) : error C2079: 'streamIn' uses undefined class 'basic_stringstream<char,struct std::char_traits<char>,class std::allocator<char> >'
        C:\接受方\STL1.CPP(43) : 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 compiled
C:\接受方\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:\接受方\STL1.CPP(43) : 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 compiled
C:\接受方\STL1.CPP(28) : warning C4552: '>>' : operator has no effect; expected operator with side-effect
        C:\接受方\STL1.CPP(43) : 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 compiled
Error executing cl.exe.
STL1.exe - 6 error(s), 3 warning(s)

你可能感兴趣的:(STL)