我对下面的简单程序有疑问:
def my_function(my_array = np.zeros(0)):
my_array = [1, 2, 3]
my_array = np.zeros(0)
my_function(my_array)
print my_array
Run Code Online (Sandbox Code Playgroud)
它打印一个空数组,好像my_array是通过复制传递而不是通过函数内部的引用传递.怎么纠正?
我有两个数组:
array_x = [x1, x2, x3, x4... xn]
array_y = [y1, y2, y3, y4... yn]
Run Code Online (Sandbox Code Playgroud)
我想有一个函数f(array_x, array_y, value_x)返回通过插值value_y关联到value_x数组.
怎么做 ?
考虑以下功能:
template <typename Type>
void f(const Type& x)
Run Code Online (Sandbox Code Playgroud)
无论传递的类型是空std::tuple还是空,我都想做一些特殊的事情(没有特化)std::array.对于nu元素的元组,我可以使用std::is_same<Type, std::tuple<>>::value但是我可以用什么技巧来检测零元素数组?
(我正在寻找一种不需要创建另一个函数或类的解决方案,......)
考虑以下测试:
std::is_same<T, bool>::value
std::is_same<T, char>::value
std::is_same<T, short int>::value
std::is_same<T, int>::value
std::is_same<T, long int>::value
std::is_same<T, long long int>::value
std::is_same<T, float>::value
std::is_same<T, double>::value
std::is_same<T, long double>::value
Run Code Online (Sandbox Code Playgroud)
问题是,如果T = const unsigned char所有测试都是假的,我希望这个std::is_same<T, char>::value是真的.或者,如果T = volatile signed long long int我想std::is_same<T, long long int>::value成为现实.怎么做type_traits?
我知道C++中的一些元编程技术可以在编译时计算常量.元函数中的大多数时间分支是通过三元运算符完成的,可以在编译时以与标准if/else相反的方式进行评估.
但是关于这种功能:
template <unsigned int N>
void f()
{
if (N == 0) {
// Some computations here
} else if (N <= 42) {
// Some computations here
} else {
// Some computations here
}
}
Run Code Online (Sandbox Code Playgroud)
编译器会做什么(假设-O3)?编译器知道f<0>()将始终在第一种情况下f<32>()分支,将始终在第二种情况下f<64>()分支,并且将始终在第三种情况下分支.
编译器会删除永远存在的分支false吗?它会直接分支到唯一有效的案例吗?
阅读三元运算符的文档,我意识到有两个我从未使用过的特殊情况:
bool ? void : void以下是有效的,完全定义的,经常使用的(假设这是一个类成员,并且该类拥有一个Type _data[Size])?
Type& at(const unsigned int i)
{
return (i < Size) ? (_data[i]) : (throw std::out_of_range("ERROR"));
}
Run Code Online (Sandbox Code Playgroud) 目前,我们只能operator[]用一个参数重载.
我想知道是否有一个根本原因,为什么标准不允许operator[]多个参数重载?
对于C++ 17,是否有这样的propoposals?
有人可以解释我为什么以下代码:
#include <iostream>
#include <bitset>
int main()
{
unsigned char i = 2;
std::cout<<std::bitset<8>((~static_cast<unsigned char>(0)) << i)<<std::endl;
std::cout<<std::bitset<8>((~static_cast<unsigned char>(0)) >> i)<<std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
生产:
11111100
11111111
Run Code Online (Sandbox Code Playgroud)
并不是:
11111100
00111111
Run Code Online (Sandbox Code Playgroud) 考虑以下问题:

我的问题是:如何优化以下独立功能:
// Computation of the coordinates of P
inline std::array<double, 3> P(const std::array<double, 3>& A,
const std::array<double, 3>& B,
const std::array<double, 3>& M)
{
// The most inefficient version in the world (to be verified)
std::array<double, 3> AB = {B[0]-A[0], B[1]-A[1], B[2]-A[2]};
std::array<double, 3> AM = {M[0]-A[0], M[1]-A[1], M[2]-A[2]};
double norm = std::sqrt(AB[0]*AB[0]+AB[1]*AB[1]+AB[2]*AB[2]);
double dot = AB[0]*AM[0]+AB[1]*AM[1]+AB[2]*AM[2];
double d1 = dot/norm;
std::array<double, 3> AP = {AB[0]/d1, AB[1]/d1, AB[2]/d1};
std::array<double, 3> P = {AP[0]-A[0], AP[1]-A[1], AP[2]-A[2]};
return P;
}
// …Run Code Online (Sandbox Code Playgroud) 考虑一个具有private std::vector数据成员的类:
class MyClass
{
private:
std::vector<double> _data;
public:
template <class... Args>
/* something */ insert(Args&&... args) /* something */
{
return _data.insert(std::forward<Args>(args)...);
}
};
Run Code Online (Sandbox Code Playgroud)
什么是正确的语法(使用C++ 14自动/可变参数模板/前进...)传输的给定函数_data来MyClass(例如insert这里),并为用户提供相同的接口?
c++ ×8
c++11 ×6
arrays ×3
numpy ×2
optimization ×2
python ×2
templates ×2
type-traits ×2
3d ×1
auto ×1
bit-shift ×1
c++14 ×1
forwarding ×1
geometry ×1
if-statement ×1
member ×1
qualifiers ×1
scipy ×1
standards ×1
subscript ×1
throw ×1