出于好奇,我看了一下std :: array的LLVM实现,并注意到它是一个结构体.我看过的大多数其他STL容器(vector,queue,map)都是类.并且它作为结构出现在标准中,因此是故意的.
谁知道为什么会这样?
在以下代码中,编译器抱怨为:struct std::array<double,5ul> has no member named 'assign'.这里似乎有可能.为什么会这样?(编译:g ++ 4.8.2)
#include <array>
int main()
{
std::array<double,5> arr;
arr.assign(4.); // error: has no member named 'assign'
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我最近偶然发现了一个非常奇怪的代码膨胀效应,我真的无法向自己解释......以下是一个有用的最小例子:
#include <array>
const int SIZE = 4000000;
struct Foo
{
static Foo& GetInstance()
{
static Foo instance;
return instance;
}
std::array<float, SIZE> Bar;
};
int main()
{
Foo::GetInstance().Bar[0] = 1.0f;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
生成的二进制文件(使用GCC MinGW 4.9.2 x86_64 posix sjlj构建)的大小为15.28 MB.但是,如果设置为eg,SIZE = 1则会获得17 KB二进制文件.
那么为什么二进制的大小取决于数组的大小呢?显然,效果是由结构为单身人士引起的.然而,我仍然没有看到任何合理的推理,为什么编译器会膨胀二进制文件.谢谢你的帮助!
(使用和不使用优化测试并-std=c++11仅使用标志.顺便说一下,这也适用于C风格的数组...)
我有以下问题:
template< size_t... N_i >
class A
{
// ...
std::array< float, /* product of the unpacked elements of N_i */ > arr;
};
Run Code Online (Sandbox Code Playgroud)
如上所示,我尝试声明一个std::array名为arr一个类的成员A.在这里,我想要arr具有解包元素N_i的大小产品,例如,在A<2,3,4>"arr"的情况下应该具有大小2*3*4=24.
有谁知道这是如何实现的?
std::array当初始数组值是构造函数的参数时,我想知道在构造函数中初始化类成员的正确方法是什么?
更具体地说,请考虑以下示例:
class Car {
public:
Car(const std::string& color, int age): color_(color), age_(age) {}
// ...
private:
std::string color_;
int age_;
};
class ThreeIdenticalCars {
private:
std::array<Car, 3> list;
public:
ThreeIdenticalCars(const std::string& color, int age):
// What to put here to initialize list to 3 identical Car(color,age) objects?
{}
};
Run Code Online (Sandbox Code Playgroud)
显然,一种方法是写作list({Car(color,age), Car(color,age), Car(color,age)}),但如果我们想要30辆相同的汽车而不是3辆,这显然无法扩展.
如果不是std::array我使用std::vector的解决方案list(3, Car(color,age)(或者list(30, Car(color, age))在我的问题中,列表的大小已知,我认为使用更正确)std:array.
在c ++ 14中,我有以下类型:
std::tuple<int[2], int>;
Run Code Online (Sandbox Code Playgroud)
我该如何正确初始化它?这个
std::tuple<int[2], int> a {{2,2},3};
Run Code Online (Sandbox Code Playgroud)
给我这个错误:
/ usr/include/c ++/5/tuple:108:25:error:用作初始化程序的数组
这个:
std::tuple<std::array<int,2>, int> a {{2,2},3};
Run Code Online (Sandbox Code Playgroud)
有效,但我希望能够使用标准的C风格数组
我希望我的代码根据运行时值使用数组的短版本或长版本(其中包含更多元素)。
constexpr std::array<int, 10> longArray{0,1,2,3,4,5,6,7,8,9};
constexpr std::array<int,4> shortArray{0,3,6,9};
auto const& myArray = useShortArray ? shortArray : longArray;
for( auto n : myArray ) {
// Do the stuff
}
Run Code Online (Sandbox Code Playgroud)
如上所述,有一个错误,因为三元运算符的参数不同。
我怎样才能做到这一点?
唯一的方法是声明两个分配的开始和结束迭代器。但这会导致for在迭代器上使用旧的,并且需要在for块内的每次使用时取消引用迭代器。
auto const& myBegin = useShortArray ? shortArray.begin() : longArray.begin();
auto const& myEnd = useShortArray ? shortArray.end() : longArray.end();
for( auto it = myBegin ; it != myEnd ; ++it ) {
// use *it
}
Run Code Online (Sandbox Code Playgroud)
有没有办法编写它(也许将数组复制到向量?)以避免恢复到开始/结束版本?
我想使用 2d std::array 因为我必须在程序中的某个点使用绑定检查。对于一维数组,我会这样做:
#include <iostream>
#include <array>
int main (){
std::array<int,4> myarray;
for (int i=0; i<10; i++){
myarray.at(i) = i+1;
}
}
Run Code Online (Sandbox Code Playgroud)
我如何为二维数组做到这一点。我可以在其中使用 auto 吗?
我对 C++ 比较陌生,我尝试做一些研究,但是在网上搜索时,我主要遇到的是 C 数组而不是std::array. 将 std::array 元素附加到 std::vector 并将std::array元素插入到 a中的最有效方法是std::vector什么?我应该使用 STL 函数,例如std::copy? 我目前正在使用 C++17,MinGW64。
我想初始化std::array的std::pair通过std::initializer_list.
pair<int, int> p = {3,4};//ok
array<pair<char,char>, 3> p = { make_pair('{','}'), make_pair('[',']'), make_pair('(',')') };//ok
array<pair<char,char>, 3> p = { {'{','}'}, {'[',']'}, {'(',')'} };//not ok
Run Code Online (Sandbox Code Playgroud)
为什么我的第三个选项不起作用?此外,这也很好:
vector<pair<char, char>> brackets = { {'{','}'}, {'[',']'}, {'(',')'} };
Run Code Online (Sandbox Code Playgroud)