我的问题是关于如何模板化应该使用的类成员的名称.
也许是一个简化的伪示例:
/**
Does something with a specified member of every element in a List.
*/
template<membername MEMBER> // <-- How to define such thing?
void doSomething(std::vector<MyClass> all){
for( i=0; i < all.size(); i++)
all[i].MEMBER++; // e.g.; use all[i].MEMBER in same way
}
Run Code Online (Sandbox Code Playgroud)
和
class MyClass{
public:
int aaa, bbb, ccc;
}
Run Code Online (Sandbox Code Playgroud)
和申请:
main(){
vector<MyClass> all = ....
// applicate doSomething() to all aaa's
doSomething<aaa>(all); // or:
doSomething<MyClass::aaa>(all); // or:
doSomething<?????>(all);
}
Run Code Online (Sandbox Code Playgroud)
模板定义应该如何,我可以在doSomething(.)中切换访问/修改MyClass的哪个成员变量(aaa,bbb或ccc)?
在我的真实世界任务中,所有成员都是相同的类型,如上所述.
谢谢,Tebas
几个小时的文件/委员会/邮件列表并且没有进展我可能会问你:我如何'编码'我的数据用于使用libpq进行二进制传输PQexecParams(.)?
简单变量只是大端序:
PGconn *conn;
PGresult *res;
char *paramValues[1];
int paramLengths[1];
int paramFormats[1];
conn = PQconnectdb(CONNINFO);
// -- (1) -- send a float value
float val_f = 0.12345678901234567890; // float precision: ~7 decimal digits
// alloc some memory & write float (in big endian) into
paramValues[0] = (char *) malloc(sizeof(val_f));
*((uint32_t*) paramValues[0]) = htobe32(*((uint32_t*) &val_f)); // host to big endian
paramLengths[0] = sizeof(val_f);
paramFormats[0] = 1; // binary
res = PQexecParams(conn, "SELECT $1::real ;", //
1, // number parameters …Run Code Online (Sandbox Code Playgroud) 谁能解释我如何在运行时切片numpy.array?我不知道'编码时'的等级(维数).
一个最小的例子:
import numpy as np
a = np.arange(16).reshape(4,4) # 2D matrix
targetsize = [2,3] # desired shape
b_correct = dynSlicing(a, targetsize)
b_wrong = np.resize(a, targetsize)
print a
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
print b_correct
[[0 1 2]
[4 5 6]]
print b_wrong
[[0 1 2]
[3 4 5]]
Run Code Online (Sandbox Code Playgroud)
而我丑陋的 dynSlicing():
def dynSlicing(data, targetsize):
ndims = len(targetsize)
if(ndims==1):
return data[:targetsize[0]],
elif(ndims==2):
return data[:targetsize[0], :targetsize[1]]
elif(ndims==3): …Run Code Online (Sandbox Code Playgroud)