我有一些代码:
std::array<JNINativeMethod, 26> methods = {
{ "nativeCreate", "(Ljava/lang/String;)J", reinterpret_cast<void*>(&nativeCreate) },
{ "nativeDestroy", "(J)V", reinterpret_cast<void*>(&nativeDestroy) },
...
{ "nativeToggleDebug", "(J)V", reinterpret_cast<void*>(&nativeToggleDebug) }}
};
Run Code Online (Sandbox Code Playgroud)
我试图用Android NDKs clang 3.4编译器编译.
但是,该代码给了我这个错误:
jni/JNI.cpp:252:9: error: excess elements in struct initializer
{ "nativeDestroy", "(J)V", reinterpret_cast<void*>(&nativeDestroy) },
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
除非我添加另一组括号:
std::array<JNINativeMethod, 26> methods = {{
{ "nativeCreate", "(Ljava/lang/String;)J", reinterpret_cast<void*>(&nativeCreate) },
{ "nativeDestroy", "(J)V", reinterpret_cast<void*>(&nativeDestroy) },
...
{ "nativeToggleDebug", "(J)V", reinterpret_cast<void*>(&nativeToggleDebug) }}
}};
Run Code Online (Sandbox Code Playgroud)
这对我来说似乎很奇怪,但在找到关于Visual C++的讨论后:http: //social.msdn.microsoft.com/forums/vstudio/en-US/e5ad8fa5-c9e8-4328-a7fa-af7a47ce2492/initialising-a-stdarray-的-结构
我想知道这是不正确的C++ 11语法,还是仅仅是clang 3.4中的缺陷.
它是否与使用带有clang的初始化列表初始化简单结构中提到的错误有关
我有一个简单的例子,我创建了std::array一些Foo元素:
struct Foo
{
Foo(int bar=0) : m_bar(bar)
{
// Code depending on the value of bar
}
int m_bar;
};
const unsigned int num = // get array size
std::array<Foo, num> fooArr;
Run Code Online (Sandbox Code Playgroud)
当我在构造函数中使用初始化列表时,m_bar(bar)它将所有设置Foo.m_bar为0(因为这是默认的构造函数参数值).如果我不这样做,那么垃圾值就满了.
我的问题是如何在不知道数据大小之前将另一个与默认值不同的值传递给数组中每个元素的构造函数?
我在创建数组时尝试使用init列表,如下所示:std::array<Foo, 5> fooArr{3}但是只将第一个元素设置m_bar为3.
我有两个std :: array相同的大小并存储相同的元素类型(我写的一个类)当我使用==运算符编译器比较它时抛出这个错误:...\include\xutility(2919): error C2672: 'operator __surrogate_func': no matching overloaded function found.
我尝试将两个数组与矢量作为它们的元素进行比较并且它有效但是将数组与我写的任何类进行比较我得到了这个错误.
测试类:
class Class {
int i;
public:
Class() {}
Class(const Class& other) {}
Class(Class&& other) {}
~Class() {}
Class operator= (const Class& other) {}
Class operator= (Class&& other) {}
BOOL operator== (const Class& other) {}
};
Run Code Online (Sandbox Code Playgroud)
比较:
std::array<Class, 3> a0 = {};
std::array<Class, 3> a1 = {};
if (a0 == a1)
"COOL";
Run Code Online (Sandbox Code Playgroud)
我得到的错误:
...\include\xutility(2919): error C2672: 'operator __surrogate_func': no matching overloaded function found
Run Code Online (Sandbox Code Playgroud) 我正在创建一个 2D std::array,并且想要按记录(而不是按索引)迭代 2D std::array 记录
std::array<std::array<int, 2>, 6> m_setSockarr;
m_setSockarr = {{{0,1},{1,2},{2,3},{4,5},
{5,6},{6,7}}};
for(auto i=m_setSockarr.begin();i !=m_setSockarr.end();i++)
{
//code here to print record wise.
}
Run Code Online (Sandbox Code Playgroud)
我想以记录方式打印值,而不是以索引方式打印值(不使用另一个循环并最终打印 m_setScokarr[i][j])。
在 std::Map 中,我们可以使用 it->first 和 it->second 逐条打印记录(Map 是关联容器)
尝试以不同的方式打印,例如新的索引增量等,但没有进展。
我想要的输出类似于在一个 for 循环中使用 std::array 的迭代器
0,1
1,2
2,3
4,5
5,6
6,7
Run Code Online (Sandbox Code Playgroud)
想知道我们是否可以使用 std::array(SequenceContainer) 来打印记录,类似于我们使用 std::map 。
编写一个接口,我必须将 的实例转换std::vector<double>为boost::array<double,N>. 每次,通过构造(没有错误),我确定向量具有正确的大小。
这是我目前正在做的一个例子(我有大约 100 个这样的函数要写在界面中):
double foo1(const std::vector<double>& x)
{
assert(x.size() == 4);
boost::array<double,4> x_;
for(std::size_t i = 0; i < 4; ++i)
x_[i] = x[i];
return foo1_(x_);
}
double foo2(const std::vector<double>& x)
{
assert(x.size() == 6);
boost::array<double,6> x_;
for(std::size_t i = 0; i < 6; ++i)
x_[i] = x[i];
return foo2_(x_);
}
Run Code Online (Sandbox Code Playgroud)
有没有更短的方法来做到这一点?
注 1:出于兼容性原因,我不使用 C++11。
注 2:我添加了标签,stdarray因为即使它是 C++11 标签,它也可能有所帮助。
我试图为std::array类型添加我自己的构造函数,但我不确定是否可能以及如何做到这一点......
我试过这样的事:
typedef unsigned char byte_t;
namespace std {
template<std::size_t _Nm>
array::array(std::vector<byte_t> data)
{
// Some content
}
}
Run Code Online (Sandbox Code Playgroud)
我想创建一个非常简单的机制来转换std::vector<byte_t>到std::array<byte_t, size>.
我正在使用C++ 14(我不能在我的项目中使用更新的标准)
是否可以删除迭代器指向的特定元素std array?我知道std vector提供了erase()方法。是否也可以实现相同的逻辑std array?
我想要一个std:arrayof std::function,但是我要确保该数组的所有元素都已初始化。为此,我构建了一个包装器类,该包装器类将a std::function作为构造参数。
但是,当我直接使用函数(应该在inside的函数)初始化包装器类的数组时,std::function它将无法编译。
这是问题,经过提炼:
#include <functional>
#include <array>
static void f() {}
using F = std::function<void(void)>;
enum { Count = 4 };
struct C
{
//To get a compilation error when some
// elements of the array are not initialized.
C() = delete;
C(F) {}
};
//OK
static const C c {f};
//OK
static const std::array<F,Count> direct
{
F{f},
{f},
f,
f
};
static const std::array<C,Count> wrapper
{
F{f}, //OK
C{f}, //OK
{f}, …Run Code Online (Sandbox Code Playgroud) 我有一个类定义为
class Edgelet
{
private:
const int e_size;
//Other private members...
public:
//The constructor
explicit Edgelet (int size, Color cl1 = Color::NA, Color cl2 = Color::NA);
//Other public members...
Run Code Online (Sandbox Code Playgroud)
由于类中有一个const成员,因此编译器隐式删除了默认的复制构造函数。需要给它一个参数来初始化。
问题出在课堂上
class Cube
{
private:
std::array <Edgelet, 12> c_edgelets;
//Other members...
public:
//The constructor
Cube(int size)
//Other public members...
Run Code Online (Sandbox Code Playgroud)
此类包含先前类对象的std::array。我如何初始化这个数组?所述尺寸参数需要被提供给的每个元素的std ::阵列初始化。我想将std::array 的每个元素设置为Edgelet(size - 2).
当然,我可以使用初始化列表,但由于构造函数有 12 个元素和其他参数,而不是显示,代码变得很难看。此外,我用 6 个元素而不是 12 个元素进行了类似的处理。
我还尝试为参数提供默认值,但由于有一个const成员,因此以后无法更改该值。我也尝试查看std::initializer_list但似乎您无法向其中添加新元素(或者您可以??)。有没有一种有效的方法来做到这一点?
我试图在编译时计算一个数组以加速某些函数,但遇到一个错误,但无法在 cppreference 的帮助下解决该错误。
代码归结为:
#include <cstddef>
#include <array>
template<typename T, size_t size>
constexpr auto giveArray()
{
std::array<T, size> arr;
for(size_t i = 0; i < size; ++i)
arr[i] = 0;
return arr;
}
constexpr auto arr = giveArray<int,10>().data();
Run Code Online (Sandbox Code Playgroud)
在 ubuntu 上使用“$ g++ -std=c++20 code.cpp”进行编译时,出现错误:.data() 不是 constexpr 函数,但它确实是。为什么我会收到此错误以及如何修复它,同时仍在编译时运行此函数并仅存储指针,而不存储 std::array 对象?