English 中文(简体)
C++ 中应该使用数组吗?
原标题:Should arrays be used in C++?

std:: list std:::Verctor 存在以来, C++ 中是否有理由使用传统的 C 阵列, 或者应该避免这些阵列, 就像 malloc ?

最佳回答

在 C++11 中, 有 < a href=" http://en.cppreference.com/w/cpp/container/array" rel= "noreferr"\\ code>std:::: array 可用, 答案是“ 是, 应该避免数组 ” 。 在 C++11 之前, 您可能需要使用 C 数组来分配自动存储中的数组( 即堆叠上 ) 。

问题回答

Definitely, although with std::array in C++11, practically only for static data. C style arrays have three important advantages over std::vector:

  • They don t require dynamic allocation. For this reason, C style arrays are to be preferred where you re likely to have a lot of very small arrays. Say something like an n-dimension point:

    template <typename T, int dims>
    class Point
    {
        T myData[dims];
    // ...
    };
    

    Typically, one might imagine a that dims will be very small (2 or 3), T a built-in type (double), and that you might end up with std::vector<Point> with millions of elements. You definitely don t want millions of dynamic allocations of 3 double.

  • The support static initialization. This is only an issue for static data, where something like:

    struct Data { int i; char const* s; };
    Data const ourData[] =
    {
        { 1, "one" },
        { 2, "two" },
        //  ...
    };
    

    This is often preferable to using a vector (and std::string), since it avoids all order of initialization issues; the data is pre-loaded, before any actual code can be executed.

  • Finally, related to the above, the compiler can calculate the actual size of the array from the initializers. You don t have to count them.

If you have access to C++11, std::array solves the first two issues, and should definitely be used in preference to C style arrays in the first case. It doesn t address the third, however, and having the compiler dimension the array according to the number of initializers is still a valid reason to prefer C style arrays.

但我也认为他们的角色会因为STL的真实数据结构而大大削弱。

我还会说, 内装封装在天体内应该最大限度地减少这种选择的影响。 如果数组是私人数据成员, 您可以在不影响您班级客户的情况下将其换成或换成 。

我曾在安全关键系统上工作过, 您无法使用动态内存分配 。 内存必须总是在堆叠中。 因此, 在此情况下, 您会使用数组, 因为在编译时会固定数组的大小 。

array > 中,给您提供动态大小 std:::vector sd:list .中的固定大小快速替代物::array 中添加的变量之一。它提供标准集装箱的好处,同时提供C型阵列的总型语义 。

所以在 c++11 i d 中,当然使用 std:: alary ,如果需要的话,在矢量之上使用。但是在 C+103 中,id避免了 C样式阵列。

多数情况下,no ,我找不到使用原始阵列的理由,比如 victors 如果代码是新的

如果您的图书馆需要符合要求数组和原始指针的代码, 您可能需要使用数组 。

我知道很多人在指出标准:在堆叠上分配阵列的阵列,在堆叠上分配阵列的阵列;在堆积上分配阵列的阵列:在堆积上分配阵列。但似乎也没有支持非本地对齐。如果你在做任何需要使用 SSE 或 VPX 指令的数值代码(分别需要128 或 256 字节对齐), C 阵列似乎还是你的最佳赌注。

我想说阵列还是有用的 如果你储存少量的静态数据 为什么不呢?

std:::victor 上,一个阵列(当然包裹在某件需要时会自动管理其交易位置的东西中)的唯一优点是, vistor 不能通过数据的所有权,除非您的编译器支持 C++11 并移动构建器。

C 样式数组是一个基本的数据结构, 所以有些情况下最好使用它。 但是, 在一般情况下, 使用更先进的数据结构环绕基础数据角。 C++ 允许您用内存来做一些非常有趣和有用的事情, 其中有许多是用简单的数组操作的 。

您应在国内使用STL集装箱,但不应在不同模块之间向此类集装箱传递指示器,否则最终会陷入依赖性地狱。例如:

std::string foo;
//  fill foo with stuff
myExternalOutputProc(foo.c_str());

是一个非常好的解决方案,但并不是

std::string foo;
//  fill foo with stuff
myExternalOutputProc(&foo);

原因是 std::字符串可以用多种不同的方式执行,但C型字符串总是C型字符串。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?