在C中你可以做到int a[] = {1,2,3,4,5},但是C++ 11 std::array<int> a = {1,2,3,4,5}会给出"模板参数太少"的编译错误.有什么方法吗?
我正在学习,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?
目标是将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数组,通常情况下,简单地命名数组与编写&foo[0]将近50年的东西具有相同的效果。
在我正在处理的项目中,从C样式数组转换为std :: array <>时,出现的绝大多数“错误”是由于C数组的上述属性所致。
在所有情况下,解决方案都很简单,只需追加即可.data()。但是,我想到精心设计operator T*应该可以直接解决这个问题。
有任何技术原因无法创建此运算符?
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. 为什么?
我想声明 a std::array,但数组部分被识别为cli::array关键字(请参阅Why is "array" mark as a returned word in Visual-C++?),这意味着 thestd::不会影响它。如何自动停止Visual Studiousing namespace cli,或指定我要使用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 中到底无法理解什么。
尝试四处寻找,但只找到了使用内置数组而不是 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 循环的 () 之间放什么
我不知道这个片段有什么问题.我收到这个错误:
错误:成员函数'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++代码:
#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)