标签: stdarray

使用隐含长度初始化std :: array

在C中你可以做到int a[] = {1,2,3,4,5},但是C++ 11 std::array<int> a = {1,2,3,4,5}会给出"模板参数太少"的编译错误.有什么方法吗?

c++ arrays c++11 stdarray

0
推荐指数
1
解决办法
186
查看次数

编译错误:使用constexpr声明std :: array size

我正在学习,constexpr并且据我所知,它constexpr告诉编译器在编译期间计算函数而不是运行时间.我使用以下代码进行测试,但遇到了一个我真的不明白的错误.你能解释一下原因吗?

#include <iostream>
#include <array>
using namespace std;

constexpr int foo(int i)
{
    return i + 5;
}

int main()
{
    int i = 10;
    std::array<int, foo(5)> arr; // OK
    // But...
    std::array<int, foo(i)> arr1; // Error
}
Run Code Online (Sandbox Code Playgroud)

错误是:' i' 的值在常量表达式中不可用.为什么?i事先声明为什么它必须是一个const

c++ metaprogramming constexpr c++11 stdarray

0
推荐指数
1
解决办法
542
查看次数

将uint64_t Bitmask转换为bool的std :: array

目标是将a std::uint64_t(用作位掩码)转换为a std::array<bool>.

这个问题类似于C#问题如何将int转换为bool数组?,但对于C++而言,我正在寻找具有最佳性能的算法.

给定a std::uint64_t,用作位掩码,我知道可以按位循环遍历其内容,并将bitcompare值设置为位于相同位置的值std::array<bool>.

但是在全能的C++ 必须有一种更有效的方式!也许一些肮脏的演员,mallocs或诸如此类的东西?一切都好; 我在Windows/GCC上,所以即使是仅GCC功能也是完全允许的.

c++ arrays performance gcc stdarray

0
推荐指数
1
解决办法
115
查看次数

为什么std :: array没有运算符T *?

对于C数组,通常情况下,简单地命名数组与编写&foo[0]将近50年的东西具有相同的效果。

在我正在处理的项目中,从C样式数组转换为std :: array <>时,出现的绝大多数“错误”是由于C数组的上述属性所致。

在所有情况下,解决方案都很简单,只需追加即可.data()。但是,我想到精心设计operator T*应该可以直接解决这个问题。

有任何技术原因无法创建此运算符?

c++ language-lawyer implicit-conversion c++11 stdarray

0
推荐指数
1
解决办法
155
查看次数

为什么我必须为 std::array 分配一个大小,而一个普通数组却不必如此?

std::array<int, 3> foo = {1,2,3};
int bar[] = {1,2,3}; // the size is 3
Run Code Online (Sandbox Code Playgroud)

对于普通数组,我不必分配大小。但我必须为std::array. 为什么?

c++ arrays c++11 stdarray

0
推荐指数
1
解决办法
68
查看次数

由于 cli::array 关键字 Visual Studio,C++ 无法使用 std::array

我想声明 a std::array,但数组部分被识别为cli::array关键字(请参阅Why is "array" mark as a returned word in Visual-C++?),这意味着 thestd::不会影响它。如何自动停止Visual Studiousing namespace cli,或指定我要使用std::array

蓝色数组字被识别为关键字

蓝色数组字被识别为关键字

c++ c++-cli visual-studio stdarray

0
推荐指数
1
解决办法
388
查看次数

std::array 结构初始化器列表语法

考虑以下 C++ 代码:

struct My_Struct
{
  int a;
  int b;
};
Run Code Online (Sandbox Code Playgroud)

现在我想声明这些结构的常量 std::array :

选项A:

const std::array<My_Struct,2> my_array =
{
  {1,2},
  {2,3}
};
Run Code Online (Sandbox Code Playgroud)

选项B:

const std::array<My_Struct,2> my_array =
{
  My_Struct(1,2),
  My_Struct(2,3)
};
Run Code Online (Sandbox Code Playgroud)

问题:为什么选项A无法编译而只有选项B可以编译?初始化 std::array 时嵌套花括号有什么问题?

在我看来,编译器应该拥有成功编译选项 A 所需的所有信息,但它会产生“太多初始值设定项值”错误。

同时,嵌套花括号在初始化普通数组时效果非常好:

const My_Struct my_array[] =
{
      {1,2},
      {2,3}
};
Run Code Online (Sandbox Code Playgroud)

编译器是MSVC 17.2,C++20模式。

我可以接受选项 B,但我想了解在 C++ 知识方面我在选项 A 中到底无法理解什么。

c++ stdarray

0
推荐指数
1
解决办法
119
查看次数

基于多维 std::array 的范围

尝试四处寻找,但只找到了使用内置数组而不是 std::array obj 的示例。

// array arr of size 5
array< array<int, 10>, 10> arr = { 0 };

srand((unsigned)time(0));

// initialize elements
for ()
{
    for()
    {
        item = rand() % 100 + 1;
    }
}
Run Code Online (Sandbox Code Playgroud)

尝试将二维数组 obj 初始化为随机值的基本示例。我不知道在 for 循环的 () 之间放什么

c++ stdarray

-1
推荐指数
1
解决办法
1242
查看次数

这个片段有什么问题?

我不知道这个片段有什么问题.我收到这个错误:

错误:成员函数'swap'不可行:'this'参数的类型为'const array',但函数未标记为const

#include <algorithm>
#include <memory>
#include <iostream>
#include <array>

struct MyClass {
  std::array<float, 4> arr;  
  float carr[4];

  std::array<float, 4> getArrElement() {
    std::array<float, sizeof(carr) / sizeof(float)> out;
    return out;
  }

  void fun() {
    auto vec = { getArrElement(), getArrElement(), getArrElement() };
    std::reverse(vec.begin(), vec.end()); // <-- error line here
  }


};

int main()
{
    MyClass obj;
    obj.fun();
}
Run Code Online (Sandbox Code Playgroud)

getArrElement没有返回一个const数组.auto应该推断,std::initializer_list但我也认为没有坏处.

怎么了?

c++ c++11 stdarray c++14

-2
推荐指数
1
解决办法
102
查看次数

C++ 11中std :: to_string的奇怪输出

我有一小段C++代码:

#include <array>
#include <string>
#include <iostream>

int main() 
{
  std::string name = "mario";
  std::cerr << "Hello world! " + name + "\n";

  std::array<float, 4> arr = {12, 12.3, 13, 14};
  std::cerr << "first item is: " + std::to_string(arr.front()) << std::endl;
  std::cerr << "last item is: " + std::to_string(arr[-1]) << std::endl;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

它编译并输出以下内容:

work ? c++ -std=c++11 -o hello_world hello.cpp
work ? ./hello_world
Hello world! mario
first item is: 12.000000
last item is: 0.000000
Run Code Online (Sandbox Code Playgroud)

但是,如果我注释掉前两行,如:

#include <array>
#include …
Run Code Online (Sandbox Code Playgroud)

c++ tostring stdstring c++11 stdarray

-3
推荐指数
1
解决办法
163
查看次数