是否可以在标准 C++ 11中获得系统(x86,x64,PowerPC/Windows,Linux或MacOS)上的剩余可用内存而不会崩溃?
一种天真的方式是尝试从太大的大小开始分配非常大的数组,每次失败时捕获异常并减小大小直到没有抛出异常.但也许有一种更有效/更聪明的方法......
编辑1:事实上我不需要确切的内存量.我想知道大约(100MB的错误条)我的代码在启动时可以使用多少.
编辑2:您如何看待这段代码?在程序开始时运行它还是可以破坏内存是否安全?
#include <iostream>
#include <array>
#include <list>
#include <initializer_list>
#include <stdexcept>
int main(int argc, char* argv[])
{
static const long long int megabyte = 1024*1024;
std::array<char, megabyte> content({{'a'}});
std::list<decltype(content)> list1;
std::list<decltype(content)> list2;
const long long int n1 = list1.max_size();
const long long int n2 = list2.max_size();
long long int i1 = 0;
long long int i2 = 0;
long long int result = 0;
for (i1 = 0; i1 < n1; ++i1) {
try …Run Code Online (Sandbox Code Playgroud) 我已经从ubuntu-developers存储库安装了Qt5和Qt3d(我在Ubuntu 13.04下),我想用CMake编译一个非常简单的应用程序(我的版本是2.8.10.1).Qt helloworld的工作CMakeLists.txt如下:
cmake_minimum_required(VERSION 2.8.8)
project(testproject)
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
# Find the QtWidgets library
find_package(Qt5Widgets)
# Tell CMake to create the helloworld executable
add_executable(helloworld helloworld.cpp)
# Use the Widgets module from Qt 5.
qt5_use_modules(helloworld Widgets)
Run Code Online (Sandbox Code Playgroud)
但是像这个例子的基本Qt3d程序的CMakeLists.txt是什么:https://gitorious.org/wiki-sources/wiki-sources/trees/master/qt3d/glview
考虑以下愚蠢的例子:
class MyClass
{
public:
template <class Function>
inline double f(double x, Function&& function)
{
return function(x);
}
};
Run Code Online (Sandbox Code Playgroud)
有了这个类,我可以MyClass::f(x, function)使用lambda函数调用它来执行它x,我希望没有开销.我的问题是:function作为一个可设置成员的等价物是什么MyClass?
class MyClass
{
public:
inline double f(double x)
{
return _function(x);
}
// What are the setter and the type of the protected member _function ?
};
Run Code Online (Sandbox Code Playgroud) 如果我们看一下C语言的委员会草案:n1570
,特别是Annex G关于复杂数学函数的行为,我们可以看到复指数在无穷大处有以下行为:
cexp(+infinity+I*infinity)=+/-infinity+I*NaN
(where the sign of the real part of the result is unspecified).
Run Code Online (Sandbox Code Playgroud)
我的问题是:为什么?
从数学的角度来看,如果我们以相同的方式接近实部和虚部的无穷大,则限制是复数无穷大(例如参见Wolfram Alpha),它对应于无限模数和未定义的参数.
此外,如果我们查看cexp函数的行为,它的实部和虚部非常可比(参见Wolfram Alpha上的3D图).
所以,我原以为:
cexp(+infinity+I*infinity)=+/-infinity+/-I*infinity
Run Code Online (Sandbox Code Playgroud)
代替:
cexp(+infinity+I*infinity)=+/-infinity+I*NaN
Run Code Online (Sandbox Code Playgroud)
我知道有一个很好的理由,但我不明白.有人能解释一下这背后的逻辑吗?
编辑:这里是链接的摘要:

是否在std::string::erase(0)空字符串上定义了行为.因为cppreference说:
Removes count characters starting at index.
Run Code Online (Sandbox Code Playgroud)
但对于空字符串,索引0处的字符不存在.
目前我有两个numpy的数组:x与y相同尺寸的.
我想写一个函数(可能调用numpy/scipy ...函数,如果它们存在):
def derivative(x, y, n = 1):
# something
return result
Run Code Online (Sandbox Code Playgroud)
其中result是的相同大小的numpy的阵列x和包含的值n的第衍生物y关于到x(我想要评价的衍生物使用若干值y,以避免非平滑结果).
在Bit Twiddling Hacks网站上,提供了以下算法来将整数舍入到下一个2的幂:
unsigned int v; // compute the next highest power of 2 of 32-bit v
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
Run Code Online (Sandbox Code Playgroud)
我想编写一个元编程函数来计算相同的操作:
这是预期功能的形式:
template <typename Type,
// Something here (like a recursion index)
class = typename std::enable_if<std::is_integral<Type>::value>::type,
class = typename std::enable_if<std::is_unsigned<Type>::value>::type>
constexpr Type function(const Type value)
{
// Something here
}
Run Code Online (Sandbox Code Playgroud)
怎么做 ? …
c++ recursion bit-manipulation template-meta-programming c++11
考虑以下两点:
template <class Function>
void apply(Function&& function)
{
std::forward<Function>(function)();
}
Run Code Online (Sandbox Code Playgroud)
和
template <class Function>
void apply(Function&& function)
{
function();
}
Run Code Online (Sandbox Code Playgroud)
在什么情况下有差异,它有什么具体的区别?
考虑使用numpy数组的以下代码非常慢:
# Intersection of an octree and a trajectory
def intersection(octree, trajectory):
# Initialize numpy arrays
ox = octree.get("x")
oy = octree.get("y")
oz = octree.get("z")
oe = octree.get("extent")/2
tx = trajectory.get("x")
ty = trajectory.get("y")
tz = trajectory.get("z")
result = np.zeros(np.size(ox))
# Loop over elements
for i in range(0, np.size(tx)):
for j in range(0, np.size(ox)):
if (tx[i] > ox[j]-oe[j] and
tx[i] < ox[j]+oe[j] and
ty[i] > oy[j]-oe[j] and
ty[i] < oy[j]+oe[j] and
tz[i] > oz[j]-oe[j] and
tz[i] < oz[j]+oe[j]):
result[j] += …Run Code Online (Sandbox Code Playgroud) 我是一名数字物理学家,我在社区中看到了一些模拟代码,这些代码使用的是一个三维模拟框,其中心位于中心,[0.5, 0.5, 0.5]标准化长度为1(因此框坐标从中0.到1.).在此框中,执行了许多物理计算,通常需要尽可能高的精度.
我认为做这样的事情可以被视为一种缺陷,但我想得到确认.我倾向于认为这是一个缺陷,因为由于我们附近有更多的数值精度0.,所以在整个方框中数值精度没有很好地平衡.
为了获得良好的平衡,我认为这样一个盒子:
0.(从-0.5到0.5)为中心1.5(从1.到2.)为中心我是正确还是完全错误?
c++ floating-point precision floating-accuracy numerical-methods