c++ - Valarray 和自定义分配器

标签 c++ stl containers valarray

为什么 std::valarray 不支持自定义分配器?它的内存管理是怎么设计的?是否使用了基于newmalloc 的分配器? 所有其他容器通常都提供指定自定义分配器的可能性。比如说,libstdc++ 中的 std::vector 有相应的模板参数 Allocator 并且我可以将 __gnu_cxx::__mt_alloc 指定为分配器。

最佳答案

Why std::valarray does not support custom allocators?

因为 std::valarray 不是动态数组(像 std::vector)。它是一种用于数值计算的 vector 优化版本,旨在依赖 SIMD 和类似的优化。

来自 the documentation :

std::valarray and helper classes are defined to be free of certain forms of aliasing, thus allowing operations on these classes to be optimized similar to the effect of the keyword restrict in the C programming language. In addition, functions and operators that take valarray arguments are allowed to return proxy objects to make it possible for the compiler to optimize an expression [...] avoiding any temporaries or multiple passes.

如您所见,它不是一个简单的动态数组。它是一个动态数组,旨在通过许多优化来保存数值并对其进行矢量化计算。

关于c++ - Valarray 和自定义分配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24351425/

相关文章:

ios - 容器 View 中的 Split View失去导航 Controller 的功能

docker - 无法在kubernetes v1中生成服务帐户 token

c++ - 大多数基本类型的高效二进制序列化

c++ - 查找 std::vector 中每个唯一值的频率的有效方法

c++ - WinAPI - 如果 .ini 文件不存在则创建它

c++ - 如何迭代打印 SET (STL) 中的元素

c++11 - unordered_set<int> find 方法的时间复杂度

azure - 如何在azure blob存储的容器中添加自定义列?

c++ - 在 C++ 中同时使用 IFileDialog 和 SHBrowseForFolder

C++ - OpenGL - 带有 std::vector 的 glBufferData