- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
是否可以使用模板实现下面所示的想法:
// please excuse any syntax errors.
template<typename KEY, typename VALUE>
class Container {
public:
VALUE calculate(vector<KEY> searchFors)
{
KEY searchFor = searchFors[0];
pair<KEY,VALUE> lower = getLower(searchFor);
pair<KEY,VALUE> upper = getUpper(searchFor);
// calculateImpl uses + - * /
VALUE value = calculateImpl(
lower.first, lower.second(searchFor),
upper.first, upper.second(searchFor) );
return value;
}
// an example of calculateImpl
VALUE calculateImpl( KEY key1, VALUE value1, KEY key2, VALUE value2 )
{
return value1 * value2;
}
// an example of getLower getUpper, assuming there're more than 2 elements in _data
pair<KEY,VALUE> getLower(KEY key)
{
return *(_data.begin());
}
pair<KEY,VALUE> getUpper(KEY key)
{
return *(_data.begin()+1);
}
private:
vector<pair<KEY,VALUE>> _data;
};
请注意,VALUE 需要执行 operator()。我怎样才能让它可以在原语或仿函数之间进行选择?例如,如果 searchFors.size() == 0,则使用 VALUE 作为原语,否则,使用 VALUE 作为仿函数?
换句话说,VALUE 可以是原语(VALUE 的类型)或仿函数(VALUE (*)(KEY) 的类型),具体取决于取决于 vector 大小的开关。
这种容器的用法是
vector<double> keys;
keys.push_back(2);
keys.push_back(3);
// usage as primitive
// Note Container<double,double> where VALUE=double
Container<double,Container<double,double>> c1;
double result1 = c1.calculate(keys);
Container<double,Container<double,Array<double>>> c2;
Array<double> result2 = c1.calculate(keys);
我希望能够嵌套容器,递归调用计算,当 VALUE 不再是容器时停止。
编辑:添加代码
template<typename KEY, typename VALUE>
class Container {
public:
Container(vector<KEY> x, vector<VALUE> y)
:_x(x),
_y(y) {}
VALUE calculate(vector<KEY> searchFors)
{
if( searchFors.size() == 0 )
{
throw exception("no search keys");
}
KEY key = searchFors[0];
if( key >= *(_x.end()-1) )
{
return *(_y.end()-1);
}
if( key <= *(_x.begin()) )
{
return *(_y.begin());
}
vector<KEY>::const_iterator iSearchKey;
iSearchKey = upper_bound( _x.begin(), _x.end(), key );
size_t pos = iSearchKey - _x.begin();
return (_y[pos]-_y[pos-1])/(_x[pos]-_x[pos-1]) * (key-_x[pos-1]) + _y[pos-1];
}
private:
vector<KEY> _x;
vector<VALUE> _y;
};
template<typename KEY, typename VALUE>
class Container<KEY, Container<KEY, VALUE> > {
public:
Container(vector<KEY> x, vector<Container<KEY, VALUE> > y)
:_x(x),
_y(y) {}
VALUE calculate(vector<KEY> searchFors)
{
if( searchFors.size() == 0 )
{
throw exception("no search keys");
}
KEY key = searchFors[0];
vector<KEY> remainingKeys( searchFors.begin()+1, searchFors.end() );
if( key >= *(_x.end()-1) )
{
return (_y.end()-1)->calculate(remainingKeys);
}
if( key <= *(_x.begin()) )
{
return _y.begin()->calculate(remainingKeys);
}
vector<KEY>::const_iterator iSearchKey;
iSearchKey = upper_bound( _x.begin(), _x.end(), key );
size_t pos = iSearchKey - _x.begin();
VALUE upperY = _y[pos].calculate(remainingKeys);
VALUE lowerY = _y[pos-1].calculate(remainingKeys);
return (upperY-lowerY)/(_x[pos]-_x[pos-1]) * (key-_x[pos-1]) + lowerY;
}
private:
vector<KEY> _x;
vector<Container<KEY, VALUE> > _y;
};
void main()
{
using namespace boost::assign;
vector<double> y;
vector<double> z;
y += 1,2,3,4;
z += 1,4,9,16;
Container<double,double> yz1(y,z);
z.clear();
z += 1,8,27,64;
Container<double,double> yz2(y,z);
vector<double> x;
x += 1,4;
vector<Container<double,double> > ys;
ys += yz1,yz2;
Container<double,Container<double,double> > xy(x,ys);
vector<double> keys;
keys += 2.5,3.5;
double value = xy.calculate(keys);
// prints 29
cout << value << endl;
}
编辑:一个更高效的版本,通过存储迭代器而不是数据
template<typename KEY, typename VALUE>
class Container;
template<typename KEY, typename VALUE>
class Container_helper{
public:
typedef typename std::vector<KEY>::const_iterator key_iterator_type;
typedef VALUE value_type;
typedef typename vector<VALUE>::const_iterator value_iterator_type;
static value_type getValue(const value_iterator_type& iValue,
const key_iterator_type&, const key_iterator_type&){
return *iValue;
}
};
template<typename KEY, typename VALUE>
class Container_helper<KEY, Container<KEY, VALUE> >{
public:
typedef typename std::vector<KEY>::const_iterator key_iterator_type;
typedef typename Container_helper<KEY, VALUE>::value_type value_type;
typedef typename std::vector<Container<KEY, VALUE> >::const_iterator value_iterator_type;
static value_type getValue(const value_iterator_type& iValue,
const key_iterator_type& xBegin, const key_iterator_type& xEnd)
{
return (*iValue)(xBegin,xEnd);
}
};
template<typename KEY, typename VALUE>
class Container {
typedef typename std::vector<KEY>::const_iterator key_iterator_type;
typedef typename std::vector<VALUE>::const_iterator value_iterator_type;
typedef typename Container_helper<KEY, VALUE>::value_type value_type;
public:
Container(const key_iterator_type& xBegin,
const key_iterator_type& xEnd,
const value_iterator_type& yBegin,
const value_iterator_type& yEnd)
:_xBegin(xBegin),_xEnd(xEnd),_yBegin(yBegin),_yEnd(yEnd) {}
Container(const Container& source)
:_xBegin(source._xBegin),_xEnd(source._xEnd),
_yBegin(source._yBegin),_yEnd(source._yEnd) {}
Container& operator=(const Container& source)
{
_xBegin = source._xBegin;
_xEnd = source._xEnd;
_yBegin = source._yBegin;
_yEnd = source._yEnd;
return *this;
}
public:
value_type
operator()( const key_iterator_type& searchBegin, const key_iterator_type& searchEnd ) const
{
if( searchBegin == searchEnd )
{
throw exception("no search keys");
}
KEY key = *searchBegin;
key_iterator_type searchNext = searchBegin + 1;
if( key >= *(_xEnd-1) )
{
return Container_helper<KEY,VALUE>::getValue(_yEnd-1, searchNext, searchEnd);
}
if( key <= *_xBegin )
{
return Container_helper<KEY,VALUE>::getValue(_yBegin, searchNext, searchEnd);
}
key_iterator_type iSearchKey = upper_bound( _xBegin, _xEnd, key );
size_t pos = iSearchKey - _xBegin;
KEY lowerX = *(_xBegin+pos-1);
KEY upperX = *(_xBegin+pos);
value_type upperY = Container_helper<KEY,VALUE>::
getValue(_yBegin+pos, searchNext, searchEnd);
value_type lowerY = Container_helper<KEY,VALUE>::
getValue(_yBegin+pos-1, searchNext, searchEnd);
return (upperY-lowerY)/(upperX-lowerX) * (key-lowerX) + lowerY;
}
private:
key_iterator_type _xBegin;
key_iterator_type _xEnd;
value_iterator_type _yBegin;
value_iterator_type _yEnd;
};
最佳答案
如果我没理解错的话,我想这可能对你有用
template<typename KEY, typename VALUE>
class Container {
public:
VALUE calculate(vector<KEY> searchFors);
.
.
.
private:
vector<pair<KEY,VALUE> > _data;
};
template<typename KEY, typename VALUE>
VALUE Container<KEY, VALUE>::calculate(vector<KEY> searchFors)
{
// operate on non-container (simple) values.
}
template<typename KEY, typename VALUE>
class Container<KEY, Container<KEY, VALUE> > {
public:
VALUE calculate(vector<KEY> searchFors);
.
.
.
private:
vector<pair<KEY, Container<KEY, VALUE> > > _data;
};
template<typename KEY, typename VALUE>
VALUE Container<KEY, Container<KEY, VALUE> >::calculate(vector<KEY> searchFors)
{
// operate on container values
}
这些函数应该很好地封装什么是基类型和什么是容器。唯一的问题是它只适用于容器类 - 如果您有不止一种类型的容器,您可能会使用某种类型的特征。
编辑:
在看到您的确切示例后,我发现将功能分离到辅助类中是可能的,但它会用其他类破坏命名空间。如果你不介意,那么代码在这里:
#include <iostream>
#include <vector>
#include <boost/assign.hpp>
using namespace std;
template<typename KEY, typename VALUE>
class Container;
template<typename KEY, typename VALUE>
class Container_helper{
public:
typedef VALUE value_type;
static value_type calculate(VALUE& val, vector<KEY> /*remainingKeys*/){
return val;
}
};
template<typename KEY, typename VALUE>
class Container_helper<KEY, Container<KEY, VALUE> >{
public:
typedef typename Container_helper<KEY, VALUE>::value_type value_type;
static value_type calculate(Container<KEY, VALUE>& val, vector<KEY> remainingKeys){
return val.calculate(remainingKeys);
}
};
template<typename KEY, typename VALUE>
class Container {
public:
Container(vector<KEY> x, vector<VALUE> y)
:_x(x),
_y(y) {}
public:
typename Container_helper<KEY, VALUE>::value_type calculate(vector<KEY> searchFors){
if( searchFors.size() == 0 )
{
throw exception(/*"no search keys"*/);
}
KEY key = searchFors[0];
vector<KEY> remainingKeys( searchFors.begin()+1, searchFors.end() );
if( key >= *(_x.end()-1) )
{
return Container_helper<KEY, VALUE>::calculate(*(_y.end()-1), remainingKeys);
}
if( key <= *(_x.begin()) )
{
return Container_helper<KEY, VALUE>::calculate((*_y.begin()), remainingKeys);
}
typename vector<KEY>::const_iterator iSearchKey;
iSearchKey = upper_bound( _x.begin(), _x.end(), key );
size_t pos = iSearchKey - _x.begin();
typename Container_helper<KEY, VALUE>::value_type upperY = Container_helper<KEY, VALUE>::calculate(_y[pos], remainingKeys);
typename Container_helper<KEY, VALUE>::value_type lowerY = Container_helper<KEY, VALUE>::calculate(_y[pos-1], remainingKeys);
return (upperY-lowerY)/(_x[pos]-_x[pos-1]) * (key-_x[pos-1]) + lowerY;
}
private:
vector<KEY> _x;
vector<VALUE> _y;
};
int main(int argc, char* argv[])
{
using namespace boost::assign;
vector<double> y;
vector<double> z;
y += 1,2,3,4;
z += 1,4,9,16;
Container<double,double> yz1(y,z);
z.clear();
z += 1,8,27,64;
Container<double,double> yz2(y,z);
vector<double> x;
x += 1,4;
vector<Container<double,double> > ys;
ys += yz1,yz2;
Container<double,Container<double,double> > xy(x,ys);
vector<double> keys;
keys += 2.5,3.5;
double value = xy.calculate(keys);
// prints 29
cout << value << endl;
}
关于C++ 模板 : How to dynamically choose between classes and primitive types,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7687830/
我正在创建教师表格,我想从图库中选择图片作为个人资料,我想更改右侧“选择图片按钮”的标签和位置,以及左侧同一输入框中的“选择图片”标签和标签旁边“请选择个人资料图片”我正在使用 angular 和 b
我有一个 anchor a.add_input链接,负责向当前文档添加(通过jQuery)file输入字段。输入将附加到其他文件输入字段的列表中(OneToMany 关系上下文中的多个文件上传)。 最
Apple 的版本编辑器帮助只有两个主题,“将文件的版本与时间线进行比较”和“将文件的版本与跳转栏进行比较”,这两个主题都没有描述退出或向当前文件添加特定更改的方法文件的版本,或从正在比较的对创建 m
现在,当我选择要上传的图片时,提交表单效果很好。但是即使没有选择任何图像,我也尝试提交相同的表格... Controller function create(){ $this->dat
这个问题在这里已经有了答案: How does this CASE expression reach the ELSE clause? (3 个回答) 7年前关闭。 当我将随机整数生成与 CHOOSE
我希望我的下拉列表显示第一个值:“-选择汽车-” 我这样成功了: protected void ddl1_DataBound(object sender, EventArgs e) { Co
让我们以项目 A 及其依赖项(包括传递依赖项)为例。如果查看依赖关系树,我会看到在一条路径的末尾有 slf4j-api:1.6.4,在另一条路径的末尾有 slf4j:1.6.1。 我必须选择哪个依赖项
请考虑我的旧脚本: CHOOSE.EXE /c:ynq /t:n,7 " until 7 seconds press Y to smooth details " set "LowPassFilter=
MyBatis 中动态语句 choose-when-otherwise 类似于 Java 中的 switch-case-default 语句。由于 MyBatis 并没有为 if 提供对应的 else
我想使用相同的脚本来处理存储在不同位置的许多文本文件中的数据,然后将输出保存到另一个唯一的位置,而不必每次都修改我的脚本。我输入数据使用: data <- read.table(file.choose
作为作业的一部分,我正在使用 Scalacheck 来查找缺陷。令人难以置信的是,我被卡住了,因为它正在生成一对非零整数。 从我的 IntelliJ 工作表中,逐字逐句: import org.sca
我的数据如下所示: service,rating_1,rating_2,rating_3,rating_4,rating_5 renew_patent,0,0,1,2,11 apply_benefit
我在 Linux Mint 中安装了 mxnet。我使用 anaconda 来运行 python 3.5。我按照说明进行操作,成功安装了。 mxnet 和 anaconda 都是最新版本。但是,当我尝
有没有办法让file.choose()后弹出的对话窗口运行以显示自定义标题,类似于 X <- menu(files, graphics=TRUE, title="Choose file X") ? 现
我正在尝试使用 CHOOSE 函数从表中返回几个不相邻的列,based on this answer ,但结果看起来不正确。 模拟下表中的相同问题,发现结果是一样的。 可能会发生什么?这个公式与我的版
我必须承担一个项目,制作一个视频聊天应用程序。 该视频必须从一个位置进行流式传输,并且可供全局各地的多人观看。性能确实是一个问题,超过 2-3 秒的延迟是 Not Acceptable 。 据我所知,
您好,我的项目需要我使用 WebView 和相机。我在 HTML 中实现了代码,按下“选择文件”按钮后, WebView 将让我选择是使用相机捕获照片还是从文件管理器上传文件。但我不需要文件选项,我只
我想同时做接下来的事情: 在集合末尾添加新项目(经常) 从集合开始时删除项目(或少数项目)(经常) 从集合中间删除项目(或少数项目)(很少,通过迭代所有项目发生) 问题:哪个并发收集会给我最好的性能。
我使用 Bootstrap 框架创建了一个响应不同浏览器(台式机、平板电脑、移动设备)的在线网络应用程序。之后,我将网络应用程序转换为移动网络应用程序并在 Google Play 商店上发布,并在我的
经过大量阅读和实验后,似乎我需要 PRIMARY 搜索强度,但订购 TERTIARY 或 IDENTICAL。 主要问题:使用 H2(或任何其他数据库)可以实现吗? 次要问题:我是这里唯一的一个还是你
我是一名优秀的程序员,十分优秀!