l*****e 发帖数: 64 | 1 在c++ template编程中如何判断数据的类型?
比如下面的代码,我需要确定DataT的类型,比如int, float等,
然后根据类型相应修改fprintf的参数输出。
谢了先。
template
void
myArray::write( File &file )
{
for ( unsigned i=Array::_base; !file.error()&&(i<_mysize); i++ )
{
fprintf (file, "%d %g\n", i, float (Array::_data[i]) );
}
} |
f*******y 发帖数: 988 | 2 写specialization
【在 l*****e 的大作中提到】 : 在c++ template编程中如何判断数据的类型? : 比如下面的代码,我需要确定DataT的类型,比如int, float等, : 然后根据类型相应修改fprintf的参数输出。 : 谢了先。 : template : void : myArray::write( File &file ) : { : for ( unsigned i=Array::_base; !file.error()&&(i<_mysize); i++ ) : {
|
d*******d 发帖数: 2050 | 3 用ofstream不就行了,不用你自己去判断类型。
【在 l*****e 的大作中提到】 : 在c++ template编程中如何判断数据的类型? : 比如下面的代码,我需要确定DataT的类型,比如int, float等, : 然后根据类型相应修改fprintf的参数输出。 : 谢了先。 : template : void : myArray::write( File &file ) : { : for ( unsigned i=Array::_base; !file.error()&&(i<_mysize); i++ ) : {
|
l*****e 发帖数: 64 | 4 能说得稍微详细点吗?
【在 f*******y 的大作中提到】 : 写specialization
|
l*****e 发帖数: 64 | 5 是个好建议。
不过就是为了统一风格,程序中没有用到ofstream等作为文件输出。
【在 d*******d 的大作中提到】 : 用ofstream不就行了,不用你自己去判断类型。
|
h**o 发帖数: 347 | 6 why mix c & c++...
【在 l*****e 的大作中提到】 : 在c++ template编程中如何判断数据的类型? : 比如下面的代码,我需要确定DataT的类型,比如int, float等, : 然后根据类型相应修改fprintf的参数输出。 : 谢了先。 : template : void : myArray::write( File &file ) : { : for ( unsigned i=Array::_base; !file.error()&&(i<_mysize); i++ ) : {
|
l*****e 发帖数: 64 | 7 good point, hehe
but i don't know either, since this is from a large toolkit on which my
codes based
【在 h**o 的大作中提到】 : why mix c & c++...
|
l*****e 发帖数: 64 | 8 i guess one reason maybe that:
it's more flexible to control the reading/writing using fprintf/sscanf than
ofstream, especially when working with a bit complex data.
【在 h**o 的大作中提到】 : why mix c & c++...
|
t****t 发帖数: 6806 | 9 真的要用printf的话,你可以先用ostringstream得到string,再用printf输出
不混合printf和ostream倒是对的.
虽然我觉得这样的代码非常傻
than
【在 l*****e 的大作中提到】 : i guess one reason maybe that: : it's more flexible to control the reading/writing using fprintf/sscanf than : ofstream, especially when working with a bit complex data.
|
s*******d 发帖数: 59 | 10 C++吸收新东西和发展的速度太慢了,感觉现在做应用不如java,.net, 做系统
不如C。 |