1

When I need to store multiple values of same type in c++ I can use a vector or array. So far I have mainly only used vectors, but I have heard that arrays are faster to work with (read and write values). If this is true I would like to know how I could make an array with the size that I read from standard input. I currently use something like this:

int N;
cin >> N;
vector<int> myVector(N);

I have tried:

int N;
cin >> N;
array<int, N> myArray;

but this gives me an error because:
error: the value of 'N' is not usable in a constant expression
I have also tried:

int N;
cin >> N;
int myArray[N];

This will compile, but when I try to ask for the size of this array (myArray.size()) to iterate over it with a for loop then I get an error:
error: request for member 'size' in 'myArray', which is of non-class type 'int [N]'

So should I replace vectors with arrays to make my code faster and if so the how should I do it?

3
  • 1
    should I replace vectors with arrays to make my code faster No you need a dynamic array so use a vector. Commented Dec 27, 2017 at 14:17
  • 2
    std::array’s speed advantage (and its size limitation for stack-based objects) come from the fact that std::arrays size is known at compile-time and it can, thus, avoid allocation and, more importantly, an indirection. There may be a place for an std::array-like data structure which has a known upper size but a dynamic size up to this upper likit. However, there is no such data structure in the standard C++ library. Commented Dec 27, 2017 at 14:18
  • 1
    int myArray[N]; is a VLA. This is not legal c++ but a compiler extension. I doubt it is any faster than vector anyways and it makes your code only work on compilers that support this non standard extension. Commented Dec 27, 2017 at 14:20

2 Answers 2

2

N should be known at compile time. So, if you want to use array for I/O operations you should specify maximum number of ints that is allowed to read. Also, you should track number of read integers on your own.

So should I replace vectors with arrays to make my code faster and if so the how should I do it?

No, because operator>> would be the bottleneck in your case. And working with vector when number of ints is not known is just fine.

Sign up to request clarification or add additional context in comments.

Comments

0

an array is not an object in c++ so there is no .size() function. You should instead have another variable holding it's size.

int* array = new int[size];

it's the way. But you should have solid reasons to do this as vector it's much more safer and easer to use.

after using the array you must delete it:

delete [] array;

and remember you can always get an array from a vector just by using the vector::data() function

4 Comments

Everything else is good... but I downvoted for the last point, which is dangerously false. Does that even compile? It shouldn't, because that's not a valid implicit cast. Anyway, std::vector does not provide an overridden operator& that would access its buffer, and nor should it. If such code happened to work on your compiler, then (A) I have no idea how and (B) you need to replace that code immediately, because it shouldn't work and may blow up horribly at any moment.
@underscore_d I think he meant the vector::data() function. That would be, indeed, a pointer to the underlying data array. en.cppreference.com/w/cpp/container/vector/data
Right, and -1 removed. Of course, that underlying array is managed by the vector, so we can't substitute it into all cases where a raw array would be used, especially if they might try to delete it. I think it's best only to use .data() for passing to APIs that expect a raw pointer, and preferably a const one that they only read. Anything else is kind of asking for trouble and somewhat defeating the point of using vector. Still, there are cases where it can be convenient to use .data() and write, but I think they're best confined to whoever creates the vector & maintains its size
Arrays are objects in C++. You probably meant to say they are not classes and so can't have member functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.