C++模板化的zip
C++ templated zip
我有以下模板化zip函数的代码:
template< typename T1, typename T2, typename R, R F( const T1, const T2)>
inline std::vector< R> zip( const std::vector< T1> & v1, const std::vector< T2> & v2) {
if( v1.size() != v2.size())
throw exception( "Bad length!");
typename std::vector< T2>::const_iterator it2 = v2.begin();
std::vector< R> res;
for( typename std::vector< T1>::const_iterator it1 = v1.begin();
it1!= v1.end();
++it1)
{
res.push_back( F( *it1, *it2));
++it2;
}
return res;
}
我试着这样使用它:
class C {
public:
static C* foo( const C* c1, const C* c2) {
return new C;
}
};
std::vector< C*> sum( const std::vector< C*> &v1, const std::vector< C*> &v2)
{
return zip< C*, C*, C*, C::foo>( v1, v2);
}
获取:
error: could not convert template argument 'C::foo' to 'C* (*)(C*, C*)'
我应该怎么做才能使它编译和执行?
您没有将类型传递到模板列表:
zip< C*, C*, C*, C::foo>( v1, v2);
^^^^^^
试试这个代码
template< typename T1, typename T2, typename R, typename F>
inline std::vector<R> zip(const std::vector<T1> & v1,
const std::vector<T2> & v2, F f) {
//...
res.push_back( f( *it1, *it2));
//...
}
//...
std::vector<C*> sum(const std::vector< C*> &v1, const std::vector< C*> &v2)
{
return zip< C*, C*, C*>( v1, v2, C::foo);
}
有关工作代码,请参阅此处。
相关文章:
- 捕获标准输出以压缩并使用 CTRL-C 中断会给出损坏的 zip 文件
- 使用 Poco::Zip 将文件附加到现有 zip 文件
- 无法在 Cocos2dx 中使用受密码保护的 zip 文件
- 使用 Poco::zip 添加新目录始终给出异常
- 如何使用 Poco::ZIP 压缩/解压缩 zip 文件
- C++.SevenZip压缩为zip格式.ClassFactory无法提供请求的类
- cpp libcurl 在 HTTP post 调用中发送 zip 文件,而不使用 multipart\form-da
- 如何通过提升::iostream防止zip炸弹
- 有谁知道ziplib是否能够在不实际提取所有文件的情况下验证zip库
- 使用 libzip 打开 zip 文件时遇到问题
- 将 ZIP 提取到 QByteArray 中
- 如何使用 "DFLT-X" 方法编写 Zip 文件
- 使用迷你zip解压缩字符数组
- C++ 使用Powershell命令提取zip文件
- 尝试使用 wxWidgets 从 zip 打开不存在的文件时无法捕获的异常
- g++:错误:/u01/app/orace/product/11.2.0/xe/lib/lclasses12.zip:此
- 如何在 mfc c++ 中流式传输.zip文件夹的二进制表示形式
- 如何解决系统.Zip.TZipFile.ExtractAll无法从.zip文件中提取所有数据的问题
- 如何在视觉工作室中使用C++解压缩压缩文件 (.zip)
- 需要正则表达式来提取zip文件中的所有文件