C++ 同时接受std::vector和QVector的函数模板?
假设我有一个名为C++ 同时接受std::vector和QVector的函数模板?,c++,templates,vector,c++17,qvector,C++,Templates,Vector,C++17,Qvector,假设我有一个名为loadData()的函数,它接受一个容器(要填充数据)和一个CSV文件。我需要以下重载: loadData(std::vector和data,const std::string和file) loadData(QVector和data,const std::string和file) loadData(std::vector和data,const std::string和file) loadData(QVector和data,const std::string和file) load
loadData()
的函数,它接受一个容器(要填充数据)和一个CSV文件。我需要以下重载:
loadData(std::vector和data,const std::string和file)代码>
loadData(QVector和data,const std::string和file)代码>
loadData(std::vector和data,const std::string和file)代码>
loadData(QVector和data,const std::string和file)代码>
loadData(std::vector和data,const std::string和file)代码>
loadData(QVector和data,const std::string和file)代码>
loadData(std::vector和data,const std::string和file)代码>
loadData(QVector和data,const std::string和file)代码>
QVector
是Qt的类向量,具有类似的API,但只使用一个模板参数T
(而不是两个,如std::vector
)
1-4的实现几乎相同(它只调用5-8,将单个向量包装到另一个向量中(相同类型!)
5-8的实现是相同的(它只是调用CSV解析函数的适当重载)
我可能还需要添加float
重载,或者QVector
/std::vector
重载
基本上,这是一组巨大的重载,我想概括一下
是否可以将它们组合成两个函数模板(一个用于1D容器,另一个用于2D容器)
谢谢您可以使用基于模板和迭代器的通用算法来减少代码重复。 例如:
enum class load_data_status
{
success
// something you need
};
template<typename I>
void loadData(const I& first,const I& last, std::ostream& file)
{
typedef typename std::iterator_traits<I>::value_type T;
// do load raw data
std::for_each(first, last, [file](const T& t) {
// do something
} );
}
template<typename I>
void loadComplexData(const I& first,const I& last, std::ostream& file)
{
typedef typename std::iterator_traits<I>::value_type C;
typedef typename C::value_type T;
// do load complex data
std::for_each(first, last, [file](const C& t) {
// do something
} );
}
template<typename T>
load_data_status loadData(const std::vector< std::vector<T> >& data, const std::string& file) {
std::ofstream f(file);
std::for_each(data.cbegin(), data.cend(), [f] (const std::vector<T>& v) {
loadData(v.cbegin(), v.cend(), f);
} );
return load_data_status::success;
}
template<typename T>
load_data_status loadData(const QVector< QVector<T> >& data, const std::string& file) {
std::ofstream f(file);
std::for_each(data.begin(), data.end(), [f] (const QVector<T>& v) {
loadData(v.begin(), v.end(), f);
} );
return load_data_status::success;
}
template<typename T>
load_data_status loadData(const std::vector< std::vector<std::complex<T> > >& data, const std::string& file) {
std::ofstream f(file);
std::for_each(data.cbegin(), data.cend(), [f] (const std::vector<std::complex<T> >& v) {
loadComplexData(v.cbegin(), v.cend(), f);
} );
return load_data_status::success;
}
template<typename T>
load_data_status loadData(const QVector< QVector< std::complex<T> > >& data, const std::string& file) {
std::ofstream f(file);
std::for_each(data.begin(), data.end(), [f] (const QVector< std::complex<T> >& v) {
loadComplexData(v.begin(), v.end(), f);
} );
return load_data_status::success;
}
enum类加载\u数据\u状态
{
成功
//你需要的东西
};
模板
void loadData(常量I和first、常量I和last、std::ostream和file)
{
typedef typename std::iterator_traits::value_type T;
//是否加载原始数据
std::for_每个(第一个、最后一个[文件](常量T&T){
//做点什么
} );
}
模板
void loadComplexData(常量I&first、常量I&last、std::ostream&file)
{
typedef typename std::iterator_traits::value_type C;
typedef typename C::value_type T;
//是否加载复杂数据
std::for_每个(第一个、最后一个[文件](常量C&t){
//做点什么
} );
}
模板
加载数据状态加载数据(常量std::vector&数据、常量std::字符串和文件){
流f(文件)的std::;
std::for_each(data.cbegin()、data.cend()、[f](const std::vector&v){
加载数据(v.cbegin(),v.cend(),f);
} );
返回加载\数据\状态::成功;
}
模板
加载数据状态加载数据(常量QVector和数据,常量std::字符串和文件){
流f(文件)的std::;
std::for_each(data.begin()、data.end()、[f](const QVector&v){
加载数据(v.begin(),v.end(),f);
} );
返回加载\数据\状态::成功;
}
模板
加载数据状态加载数据(常量std::vector&数据、常量std::字符串和文件){
流f(文件)的std::;
std::for_each(data.cbegin()、data.cend()、[f](const std::vector&v){
loadComplexData(v.cbegin(),v.cend(),f);
} );
返回加载\数据\状态::成功;
}
模板
加载数据状态加载数据(常量QVector>和数据、常量std::string和文件){
流f(文件)的std::;
std::for_each(data.begin()、data.end()、[f](const QVector&v){
loadComplexData(v.begin(),v.end(),f);
} );
返回加载\数据\状态::成功;
}
编写泛型代码时,特别注意分离关注点
loadData
中有两个问题:
- 从文件中获取对象
- 将这些对象附加到容器
#include <fstream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <complex>
struct data_file_loader_base
{
data_file_loader_base(std::string const& path)
: input_stream_(path)
, items_(get_input<std::size_t>())
{
}
template<class T>
T get_input()
{
T val;
input_stream_ >> val;
return val;
}
std::size_t items() const
{
return items_;
}
std::ifstream& input_stream()
{
return input_stream_;
}
private:
std::ifstream input_stream_;
std::size_t items_;
};
// basic value loader
template<class Type>
struct data_file_loader
: data_file_loader_base
{
data_file_loader(std::string const& path)
: data_file_loader_base(path)
{}
std::istream_iterator<Type> begin()
{
return { input_stream() };
}
std::istream_iterator<Type> end()
{
return { };
}
};
// specialize for types that need custom input
// in this case, for a complex. But could easily be a vector etc.
template<class Type>
struct data_file_loader<std::complex<Type>>
: data_file_loader_base
{
data_file_loader(std::string const& path)
: data_file_loader_base(path)
{}
struct iterator
{
using value_type = std::complex<Type>;
using iterator_category = std::forward_iterator_tag;
using pointer = value_type*;
using reference = value_type&;
using difference_type = int;
iterator(data_file_loader& loader, bool eof = false)
: self_(loader)
, eof_(eof || check_eof())
{
}
bool operator==(iterator const& other) const
{
return eof_ && other.eof_;
}
bool operator!=(iterator const& other) const
{
return !(*this == other);
}
iterator& operator++() {
return *this;
}
value_type operator*() {
auto result = value_type { self_.get_input<Type>(), self_.get_input<Type>() };
eof_ = check_eof();
return result;
}
bool check_eof() const {
return !input_stream();
}
data_file_loader& self_;
bool eof_ = false;
};
iterator begin()
{
return { *this, false };
}
iterator end()
{
return { *this, true };
}
};
// one overload per container type
template<class Type>
void loadData(std::vector<Type>& target, std::string const& path)
{
auto loader = data_file_loader<Type>(path);
target.reserve(loader.items());
std::copy(std::begin(loader), std::end(loader), std::back_inserter(target));
}
// test
int main()
{
std::vector<int> vi;
loadData(vi, "foo.txt");
std::vector<std::complex<double>> vc;
loadData(vc, "foo_complex.txt");
}
#包括
#包括
#包括
#包括
#包括
结构数据\u文件\u加载程序\u库
{
数据文件加载程序库(标准::字符串常量和路径)
:输入\流\路径
,项(获取输入()
{
}
模板
无法获取_输入()
{
T值;
输入\u流\u>>val;
返回val;
}
std::size\u t items()常量
{
退货项目;;
}
std::ifstream和input_stream()
{
返回输入流;
}
私人:
std::ifstream input\U stream\;
标准::尺寸\u t项目\u;
};
//基本值加载器
模板
结构数据文件加载器
:数据\u文件\u加载程序\u库
{
数据文件加载器(标准::字符串常量和路径)
:数据\文件\加载程序\基础(路径)
{}
std::istream_迭代器begin()
{
返回{input_stream()};
}
std::istream_迭代器end()
{
返回{};
}
};
//专门化需要自定义输入的类型
//在这种情况下,对于一个复杂的。但很容易是一个向量等。
模板
结构数据文件加载器
:数据\u文件\u加载程序\u库
{
数据文件加载器(标准::字符串常量和路径)
:数据\文件\加载程序\基础(路径)
{}
结构迭代器
{
使用value_type=std::complex;
使用迭代器\u category=std::forward\u迭代器\u标记;
使用指针=值_类型*;
使用引用=值\类型&;
使用差分_type=int;
迭代器(数据文件加载器&加载器,bool eof=false)
:自动加载程序(加载程序)
,eof|(eof | | check_eof())
{
}
布尔运算符==(迭代器常量和其他)常量
{
返回eof&&other.eof ;;
}
布尔运算符!=(迭代器常量和其他)常量
{
返回!(*此==其他);
}
迭代器和运算符++(){
归还*这个;
}
值类型运算符*(){
自动结果=值\u类型{self\u.get\u input(),self\u.get\u input()};
eof=检查eof();
返回结果;
}
bool check_eof()常量{
return!input_stream();
}
数据文件加载器&self;
bool-eof_u2;=false;
};
迭代器
template <template <typename...> class>
struct isVector : public std::false_type
{ };
template <>
struct isVector<std::vector> : public std::true_type
{ };
template <>
struct isVector<QVector> : public std::true_type
{ };
template <typename T>
struct isCFloating : public std::is_floating_point<T>
{ };
template <typename T>
struct isCFloating<std::complex<T>> : public std::true_type
{ };
template <template <typename ...> class V1,
template <typename ...> class V2, typename T>
auto loadData (V1<V2<T>> & v, std::string fn)
-> std::enable_if_t< isVector<V1>::value
&& isVector<V2>::value
&& isCFloating<T>::value>
{
std::cout << "- vector of vector version, " << fn << std::endl;
}
template <template <typename ...> class V, typename T>
auto loadData (V<T> & v, std::string fn)
-> std::enable_if_t<isVector<V>::value && isCFloating<T>::value>
{
std::cout << "- vector version, " << fn << std::endl;
V<V<T>> vv{1, v};
loadData(vv, fn);
}
#include <vector>
#include <complex>
#include <iostream>
#include <type_traits>
// fake QVector
template <typename>
struct QVector
{
template <typename ... Ts>
QVector (Ts const & ...)
{ }
};
template <template <typename...> class>
struct isVector : public std::false_type
{ };
template <>
struct isVector<std::vector> : public std::true_type
{ };
template <>
struct isVector<QVector> : public std::true_type
{ };
template <typename T>
struct isCFloating : public std::is_floating_point<T>
{ };
template <typename T>
struct isCFloating<std::complex<T>> : public std::true_type
{ };
template <template <typename ...> class V1,
template <typename ...> class V2, typename T>
auto loadData (V1<V2<T>> & v, std::string fn)
-> std::enable_if_t< isVector<V1>::value
&& isVector<V2>::value
&& isCFloating<T>::value>
{
std::cout << "- vector of vector version, " << fn << std::endl;
}
template <template <typename ...> class V, typename T>
auto loadData (V<T> & v, std::string fn)
-> std::enable_if_t<isVector<V>::value && isCFloating<T>::value>
{
std::cout << "- vector version, " << fn << std::endl;
V<V<T>> vv{1, v};
loadData(vv, fn);
}
int main ()
{
std::vector<float> vf;
std::vector<std::complex<float>> vc;
std::vector<std::vector<double>> vvf;
std::vector<std::vector<std::complex<double>>> vvc;
QVector<long double> qf;
QVector<std::complex<long double>> qc;
QVector<QVector<float>> qqf;
QVector<QVector<std::complex<float>>> qqc;
loadData(vf, "case 1");
loadData(qf, "case 2");
loadData(vc, "case 3");
loadData(qc, "case 4");
loadData(vvf, "case 5");
loadData(qqf, "case 6");
loadData(vvc, "case 7");
loadData(qqc, "case 8");
// extra cases: mixing std::vector and QVector
std::vector<QVector<double>> vqf;
std::vector<QVector<std::complex<double>>> vqc;
QVector<std::vector<long double>> qvf;
QVector<std::vector<std::complex<long double>>> qvc;
loadData(vqf, "case 9");
loadData(vqc, "case 10");
loadData(qvf, "case 11");
loadData(qvc, "case 12");
}