- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
给出以下类:
template< class T> class table2D
{
...
public:
bool copy(int r, int c, int rows , int cols, table2D & s ) ;
};
其中,方法 copy() 将 block 或元素从 s 复制到 this,
我如何编写上述代码(使用模板???)以便我可以使用如下方法:
table2D s, d ;
d.copy(0, 0, 3, 3) = s ;
最佳答案
我不同意 tadman 的评论,所以我粗略地编写了代码(可能有一些错误,但我认为这些概念是正确的)。
我确信有更优雅和更通用的方法来对此进行编码。但我认为这种事情正是 C++ 旨在发挥作用的方式。
创建占位符对象以保留函数的输入是一种非常标准的 C++ 技术,该函数的真正工作需要稍后完成。
template< class T> class copier4i
{
copier4i(T& t, int a, int b, int c, int d) : m_t(t), m_a(a), m_b(b), m_c(c), m_d(d) {}
bool operator=(T& s) { return m_t.copy(m_a,m_b,m_c,m_d,s); }
T& m_t;
int m_a, m_b, m_c, m_d;
};
...
template< class T> class table2D
{
...
public:
bool copy(int r, int c, int rows , int cols, table2D & s ) ;
copier4i<table2D> copy(int r, int c, int rows , int cols) {
return copier4i<table2D>(*this,r,c,rows,cols); }
};
我没有测试这个的好地方,所以我不确定额外的 <table2D>
我刚刚在上面添加的是正确的解决方案(根据我的预期推断)或者问题是否是(我从未完全清楚的)名称 table2D
在其自己的定义中使用,什么时候隐式表示 table2D<T>
(正如我在这里打算的那样)vs. 什么时候需要明确(如果在 table2D
的定义之外使用它会是这样。所以也许应该是:
auto copy(int r, int c, int rows , int cols) {
return copier4i(*this,r,c,rows,cols); }
或者也许
copier4i<table2D<T> > copy(int r, int c, int rows , int cols) {
return copier4i<table2D<T> >(*this,r,c,rows,cols); }
新版本,包括来自 bool
的更改至 void
George T. 制作的,这次在 ideone 进行了测试(作为 C++14 代码)
#include <stdio.h>
#include <stdlib.h>
template< class T > class copier4i
{
public:
T& m_t;
int m_a, m_b, m_c, m_d;
copier4i(T& t, int a, int b, int c, int d) : m_t(t), m_a(a), m_b(b), m_c(c), m_d(d) {}
copier4i& operator=(T& s) { m_t.copy(m_a,m_b,m_c,m_d,s); return *this;}
} ;
template< class T> class table2D
{
public:
int m_rows ;
int m_cols ;
T * m_obj ;
public:
table2D( int r, int c ) {
m_rows = r ;
m_cols = c ;
m_obj = ( T * )malloc( m_rows * m_cols * sizeof( T ) ) ;
}
~table2D() {
free( m_obj );
}
inline T * operator[](int r) {
return (this->m_obj + (r * m_cols ) ) ;
}
void copy( int r, int c, int cr, int cc, table2D &s) {
(*this)[r][c] = s[0][0] ;
}
copier4i< table2D > copy(int r, int c, int rows , int cols)
{
return copier4i<table2D>( *this, r, c, rows, cols );
}
} ;
int main()
{
table2D<int> t(5, 5);
table2D<int> s(5, 5);
s.copy(0, 0, 2, 2, t );
s.copy(0, 0, 2, 2) = t ;
}
关于lhs 上的 C++ 模板和函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33883390/
我知道 a = val1 if condition else val2 但是有没有办法做类似的事情 a if condition else b = val 抛出一个SyntaxError(我想这是可以
我遇到了一些归结为以下内容的代码: enum BAR { /* enum values omitted */ } class Foo{ public: void set(const BAR& ba
我是 R 新手,我想用 *apply 改进以下脚本函数(我已经阅读了 apply ,但我无法使用它)。我想用lm在多个自变量(数据框中的列)上运行。我用了 for (i in (1:3) { as
我知道要问这种有趣的问题。我想知道这是否可以做到? Class foo { public static void main(String [] args){ for (int i=0; i = new
分配给表达式(而不是名称)在 Python 中很常见。例如,这是完全有效的语法: my.object["with_some"].very_long["expression"] = func(my.ob
我想在 R 中的一行中分配多个变量。可以这样做吗? values # initialize some vector of values (a, b) = values[c(2,4)] # assign
以下程序的输出未给出预期结果: #include int main() { int *x; int *y; *x = 10; *y = 45; printf
我正在学习流口水并试图了解最佳实践。之间有什么实际区别: when $event : Event(Helper.notNull(foo),...) 和 when $event : Ev
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 7 年前。 Improve
给出以下类: template class table2D { ... public: bool copy(int r, int c, int rows , int cols, ta
我知道以下关于引用 例如。 int &ref=x;然后 ref 和 x 是内存中相同位置的名称。与指针不同,内存不是为引用分配的。 我正在使用我成功编写的引用在 C++ 中编写一个简单的交换程序。 然
尝试在 Python + Gurobi 中实现指标约束,其中指标(LHS)是二元决策变量的总和。 嗨,我想在 Python + Gurobi 中实现以下功能: Y_i_d and U_d are bi
假设我有这样的代码: #include "boost/thread/mutex.hpp" using boost::mutex; typedef mutex::scoped_lock lock; mu
这个问题在这里已经有了答案: In f(x), can x be evaluated before f? (2 个答案) 关闭 5 年前。 我已阅读 Order of evalution来自cppr
我正在学习 C++ 异常,我想对场景进行一些说明: T function() throw(std::exception); ... T t = value; try { t = function();
有谁知道 Mathematica 中是否有一个内置函数来获取降值规则的 lhs(没有任何持有)?我知道如何编写代码来做到这一点,但对于内置的 例如: a[1]=2; a[2]=3; BuiltInID
我碰巧真的很喜欢 Markdown(可能是因为 SO)而且我喜欢用 Haskell 编程。我最近发现了Literate Haskell (LHS) 我想同时使用 Markdown 和 LHS。让我给你
我想从 arules 生成的规则中提取 lhs 项。 例如, {a,b,c} => {d} 我希望能够提取 a,b,c 并将其放入字符向量中,以便我可以根据这些项目进行迭代和进一步处理。 目前,我可以
sap.ui.core.Control.extend("control.linechart", { /* the control API */ metadata : {
我正在尝试编写一个使用迭代深化算法来解决规划问题的 CLIPS 程序。出于同样的原因,我想保持较低的分支因子。 在以下代码中?s是表示树的级别的变量;我想使用一个规则来进行不同的检查。这就是我试图做的
我是一名优秀的程序员,十分优秀!