我有一个代码,其中在函数的末尾我需要从 int 转换为数组的所有元素的两倍,以便能够在退出函数之前执行最终 push_back。我现在拥有的代码是:
template <class T, size_t dims> class A {
typedef typename std::array<int, dims> ArrayInt;
typedef typename std::array<double, dims> ArrayDouble;
typedef typename std::vector <ArrayDouble> VectorDouble;
/* ...*/
foo() {
/* ...*/
ArrayInt myArrayInt;
ArrayDouble myArrayDouble;
VectorDouble myVectorDouble;
/* Initialize myArrayInt
Do some other stuff */
for (int i = 0; i < dims; ++i)
myArrayDouble[i] = static_cast<double>(myArrayInt[i]);
myVectorDouble.push_back(myArrayDouble);
}
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,但我对这些行感到不舒服:
for (int i = 0; i < dims; ++i)
myArrayDouble[i] = static_cast<double>(myArrayInt[i]);
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来做到这一点?
谢谢你。
假设我有一个std::vector在编译时已知的大小,我想把它变成一个std::array. 我该怎么做?是否有标准功能可以做到这一点?
到目前为止,我拥有的最佳解决方案是:
template<class T, std::size_t N, class Indexable, std::size_t... Indices>
std::array<T, N> to_array_1(const Indexable& indexable, std::index_sequence<Indices...>) {
return {{ indexable[Indices]... }};
}
template<class T, std::size_t N, class Indexable>
std::array<T, N> to_array(const Indexable& indexable) {
return to_array_1<T, N>(indexable, std::make_index_sequence<N>());
}
std::array<Foo, 123> initFoos {
std::vector<Foo> lst;
for (unsigned i = 0; i < 123; ++i)
lst.push_back(getNextFoo(lst));
return to_array<Foo, 123>(lst); // passing lst.begin() works, too
}
Run Code Online (Sandbox Code Playgroud)
该应用程序类似于使用非默认可构造类型(无可变参数模板)填充 std::array:我也有一个不可默认构造的类型,因此我需要在数组初始化时计算实际值. 然而,与这个问题相反,对我来说,这些值不仅是指数的函数,也是前面值的函数。使用循环比使用一系列函数调用更容易构建我的值。所以我在一个循环中构造元素并将它们放在一个向量中,然后我想使用该向量的最终状态来初始化数组。
以上似乎编译和工作正常,但也许有办法改进它。
我想定义两个2d矩阵:f和f_transpose类型:std::array <std::array <float, 3>, dim>和 std::array <std::array <float, dim>, 3>.价值dim是23.
我希望元素f[0][0],f[1][1],f[2][2],f_transpose[0][0],f_transpose[1][1]和f_transpose[2][2]是1和其余元素0.
这些数组是全局变量,我尝试了以下内容:
static array <array <float, 3>, dim> f = {{{{1}}, {{0, 1}}, {{0, 0, 1}}}};
static array <array <float, dim>, 3> f_transpose = {{{{1, 0, 0}}, {{0, 1, 0}}, {{0, 0, 1}}}};
Run Code Online (Sandbox Code Playgroud)
这确实给了我1必要的地方,但应该是的一些值0是1或-1.我的理解是,任何未定义的内容都将被考虑0 …
这是获取std::array::data()返回内容大小的最简单/最短的方法吗?
arr.size() * sizeof(arr.value_type)
Run Code Online (Sandbox Code Playgroud)
编辑:我的问题不准确."内存中的大小"是指数组中包含的所有元素(本身)的大小,因此,如果它们是指向结构的指针,我只需要指针的大小,而不是指向的结构.我也不想包含实现的任何可能开销的大小std::arr.只是数组元素.
有人建议sizeof(arr).这个:stof :: array <char,N>的sizeof是多少?乞求不同意.虽然它似乎在我的机器上工作,但我想知道标准保证什么.
我想用它std::array来存储N维向量的数据,并为这些向量实现算术运算.我想,既然std::array现在有一个constexpr size()成员函数,我可以使用它来展开算术运算所需的循环.
这是一个最小的例子:
#include <array>
#include <type_traits>
#include <iostream>
#include <cassert>
template<std::size_t N=0, typename Vector>
void plus_equals(Vector& result, Vector const& input)
{
result[N] += input[N];
if constexpr (N + 1 < result.size())
plus_equals<N+1>(result, input);
}
template<typename INT, size_t N>
class Vector
{
std::array<INT, N> data_;
public:
template<typename ... BracketList>
Vector(BracketList ... blist)
:
data_{std::forward<BracketList>(blist)...}
{}
INT& operator[](std::size_t i)
{
return data_[i];
}
INT operator[](std::size_t i) const
{
return data_[i];
}
decltype(auto) begin() const
{ …Run Code Online (Sandbox Code Playgroud) 根据这个问题 std::array分配在堆栈上.但是,当它与它一起使用时,Valgrind它显示了堆分配,即使对于在堆栈上分配的元素也是如此.这是假阳性还是真实?
这里按照两个mwe来说明行为.
没有堆:
以下代码:
#include <array>
int main() {
std::array<int*, 1> map;
int value = 0;
}
Run Code Online (Sandbox Code Playgroud)
产生预期的以下Valgrind输出:
==14425== HEAP SUMMARY:
==14425== in use at exit: 0 bytes in 0 blocks
==14425== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
Run Code Online (Sandbox Code Playgroud)
用堆:
但是,如果我尝试这个代码:
#include <array>
int main() {
std::array<int*, 1> map;
int value = 0;
map.at(0) = &value;
}
Run Code Online (Sandbox Code Playgroud)
Valgrind 给我
==14539== HEAP SUMMARY:
==14539== in use at exit: 72,704 …Run Code Online (Sandbox Code Playgroud) 我正在尝试构建一个嵌套的迭代器模板,并依赖于具有各种特征的迭代器,例如value_type. 但事实证明,并非所有 STL 类型都返回具有这些特征的迭代器。例如:
#include <array>
#include <type_traits>
template <typename T>
using iterator_t = decltype(std::declval<T>().begin());
static_assert(std::is_same_v<iterator_t<std::array<int, 3>>, int*>);
Run Code Online (Sandbox Code Playgroud)
此代码编译并显示数组迭代器的实际类型是int*. 在这种情况下,我如何仍然可以访问诸如value_typeetc 之类的特征?
我正在尝试制作一个固定大小的矩阵类。目的是让它继承或利用 a std::arrayof std::array:
template <size_t Rows, size_t Columns, class T=double>
struct Matrix : public std::array<std::array<T, Columns>, Rows> {
};
Run Code Online (Sandbox Code Playgroud)
我想提供一个可以自动推断大小的初始化程序,就像std::arrayC++17(我正在使用)中的 can一样。我也可以使用函数来进行Matrix而不是使用类模板参数推导。
// What I want, but does not work:
Matrix matrix {{1., 2.},
{3., 4.}};
// Or:
auto matrix = MakeMatrix ({1., 2.},
{3., 4.});
Run Code Online (Sandbox Code Playgroud)
我未能使其中任何一个成为可能。相反,只有以下工作:
// Requires specifying the size and repeating `std::array`, both undesired
Matrix<2,2> mat {
std::array{1., 2.},
std::array{3., 4.}
};
// OR this, which requires specifying the size and …Run Code Online (Sandbox Code Playgroud) 我想在编译时对 std::array 执行范围检查。下面是一个例子:
#include <iostream>
#include <array>
void rarelyUsedFunction(const std::array<double, 2>& input)
{
std::cout << input[5] << std::endl;
}
int main()
{
std::array<double, 2> testArray;
rarelyUsedFunction(testArray);
}
Run Code Online (Sandbox Code Playgroud)
如果我用 g++ 编译它,则没有警告或错误,尽管对不在数组中的元素进行了未定义的访问。编译后的程序只是打印一些随机值。
g++ 中是否有编译器选项可用于在编译时执行的合适的范围/边界检查?我知道我可以添加“-D_GLIBCXX_DEBUG”,但这只会在运行时执行检查。如果我有一个不经常调用的函数,则不会触发。
我知道,不能在所有情况下都执行这样的范围检查,但在上述情况下,编译器应该能够发现问题!?
我正在一个有类的图书馆工作foo。foo有一个不平凡的构造函数。当我创建std::arrayof foo( std::array<foo, 10>) 时,构造函数被调用 10 次。我想实现一种单独的方式来初始化foo. 定义专业化是否会std::array<foo, N>导致未定义的行为或任何其他问题?如果可以的话,我的专业需要具备哪些属性?
https://en.cppreference.com/w/cpp/language/extending_std表示允许自定义类型的专业化,除非明确禁止,并且https://en.cppreference.com/w/cpp/container/array没有说什么关于它。