Вопрос по c++, arrays – Должны ли массивы использоваться в C ++?

95

Error: User Rate Limit Exceededstd::listError: User Rate Limit Exceededstd::vectorError: User Rate Limit Exceededmalloc?

Error: User Rate Limit Exceeded Jon Purdy
Error: User Rate Limit ExceededWhat is the difference between std::array and std::vector? When do you use one over other? Alok Save

Ваш Ответ

11   ответов
109
Error: User Rate Limit Exceeded
Error: User Rate Limit ExceededknowError: User Rate Limit ExceededexactlyError: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
2

std::vectorvector

Error: User Rate Limit Exceededswap.
1

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

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

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit ExceededwhenError: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
4

Error: User Rate Limit ExceedednoError: User Rate Limit Exceededvectors. If the code is new.

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceededvector.data()Error: User Rate Limit Exceeded&vector.front()Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
15

6

arrayError: User Rate Limit Exceededc++Error: User Rate Limit Exceededstd::vectorError: User Rate Limit Exceededstd::list. Error: User Rate Limit ExceededError: User Rate Limit Exceededc++11Error: User Rate Limit Exceeded

Error: User Rate Limit Exceededc++11Error: User Rate Limit Exceededstd::arrayError: User Rate Limit ExceededC++03.

85

std::arraystd::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.

std::array

Error: User Rate Limit Exceededstd::arrayError: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceededint i[] = { 1, 2, 3 };Error: User Rate Limit Exceededint i[] = { 1, 2, 3, 4 };. array<int, 3>Error: User Rate Limit Exceededarray<int, 4>.
Error: User Rate Limit Exceededmake_array functionError: User Rate Limit Exceededmake_pairError: User Rate Limit Exceeded@R. Martinho Fernandes.
3

2

Error: User Rate Limit Exceededstd::arrayError: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceededstd::arrayError: User Rate Limit Exceeded
4

Error: User Rate Limit Exceeded

11

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceededstd::array<T>Error: User Rate Limit Exceeded

Похожие вопросы