我试图初始化一个int数组,使每个元素都设置为-1。
我尝试了以下方法,但它不起作用。它只将第一个值设置为-1。
int directory[100] = {-1};
为什么它不能正常工作?
我惊讶于所有建议使用vector
的答案。它们甚至不是同一件事情!
使用<algorithm>
中的std::fill
:
int directory[100];
std::fill(directory, directory + 100, -1);
我不直接涉及这个问题,但你可能需要一个好的辅助函数来处理数组:
template <typename T, size_t N>
T* end(T (&pX)[N])
{
return pX + N;
}
提供:
int directory[100];
std::fill(directory, end(directory), -1);
std::fill_n
更简单一些:std::fill_n(directory, 100, -1);
。 - Jerry Coffinmemset(directory, -1, ...)
。在二进制补码体系结构中,这实际上会将'int'值设置为'-1' :) - AnT stands with Russiastd::fill(std::begin(directory), std::end(directory), -1);
可以使用。 - Hindolsizeof(A)/sizeof(A[0])
。 - gerardwstd::array
,有以下三个原因:operator[]
)中的运行时安全性,避免数组越界;fill()
方法。#include <array>
#include <assert.h>
typedef std::array< int, 100 > DirectoryArray;
void test_fill( DirectoryArray const & x, int expected_value ) {
for( size_t i = 0; i < x.size(); ++i ) {
assert( x[ i ] == expected_value );
}
}
int main() {
DirectoryArray directory;
directory.fill( -1 );
test_fill( directory, -1 );
return 0;
}
使用数组需要使用"-std=c++0x"进行编译(适用于上述代码)。
如果该选项不可用或不是一个选项,则可以选择其他选项,如std::fill()(由GMan建议)或手动编写fill()方法。
boost::array
或自己封装的C数组也可以。 - Johannes Schaub - litboperator[]
不执行边界检查,只有.at()
方法可以(除非您使用某些带有调试选项的stdlib实现,在这种情况下它也会在operator[]
上执行,但这绝对不是标准行为)。#2与std::array
无关:数组可以通过引用传递,因此一个简单的模板函数可以确定大小,在C++17中我们有一个重载的std::size()
。 #3是一个无关联的论点:我们也可以使用指向其第一个和最后一个元素的指针来填充普通数组,自C++11以来可以使用std::begin()
和std::end()
获取它们。 - underscore_dint x[3] = {-1, -1, -1 };
&v.front()
来访问原始数组缓冲区。std::vector directory(100, -1);
还有一种用C语言的方式,可以使用memset
或其他类似的函数。 memset
会对指定缓冲区中的每个字符进行操作,因此对于像0
这样的值来说,它可以正常工作,但是对于-1
这样的负数,可能不起作用,具体取决于其存储方式。
您也可以使用STL通过fill_n来初始化数组。如果要对每个元素执行通用操作,则可以使用for_each。
fill_n(directory, 100, -1);
directory[i] = -1;
。boost::array<int,N> array; // boost arrays are of fixed size!
array.assign(-1);
它确实可以工作。你对初始化器的期望是不正确的。如果你真的想采用这种方法,你需要在初始化器中使用100个逗号分隔的-1。但是当你增加数组的大小时会发生什么呢?
您可以简单地使用以下代码中的for循环:
for (int i=0; i<100; i++)
{
a[i]= -1;
}
根据您的要求,您可以获得所需的结果。 A[100]={-1,-1,-1..........(100次)}
使用 int 向量代替数组。
vector<int> directory(100,-1); // 100 ints with value 1
它正在正常工作。这就是列表初始化程序的工作方式。
我相信C99标准的6.7.8.10部分涵盖了这个问题:
如果一个具有自动存储期限的对象没有明确初始化,则其值是不确定的。如果一个具有静态存储期限的对象没有明确初始化,则:
- 如果它具有指针类型,则初始化为null指针;
- 如果它具有算术类型,则初始化为(正数或无符号)零;
- 如果它是一个聚合体,则每个成员都按照这些规则递归地初始化;
- 如果它是一个联合体,则第一个命名成员按照这些规则递归地初始化。
如果您需要使数组中的所有元素具有相同的非零值,则必须使用循环或memset。
还要注意,除非您真的知道自己在做什么,否则在C++中向量优于数组:
以下是关于容器和数组的一些需要了解的事情:我有同样的问题,我找到了解决方法,文档提供了以下示例:
std::array<int, 3> a1{ {1, 2, 3} }; // double-braces required in C++11 (not in C++14)
std::array<int, 3> a1{ {1} }; // double-braces required in C++11 (not in C++14)
当所有元素的值都为1时,它可以正常工作。但使用=运算符时无法正常工作。这可能是C++11的问题。
#include <iostream>
#include <algorithm>
#include <array>
using namespace std;
template <class Elem, Elem pattern, size_t S, size_t L>
struct S_internal {
template <Elem... values>
static array<Elem, S> init_array() {
return S_internal<Elem, pattern, S, L - 1>::init_array<values..., pattern>();
}
};
template <class Elem, Elem pattern, size_t S>
struct S_internal<Elem, pattern, S, 0> {
template <Elem... values>
static array<Elem, S> init_array() {
static_assert(S == sizeof...(values), "");
return array<Elem, S> {{values...}};
}
};
template <class Elem, Elem pattern, size_t S>
struct init_array
{
static array<Elem, S> get() {
return S_internal<Elem, pattern, S, S>::init_array<>();
}
};
void main()
{
array<int, 5> ss = init_array<int, 77, 5>::get();
copy(cbegin(ss), cend(ss), ostream_iterator<int>(cout, " "));
}
输出结果为:
77 77 77 77 77
int a[100] = { [0 ... 99] = -1 };
- Johannes Schaub - litb