Ok so i just got done learning about containers, and i was wondering from the perspective of more experienced C programers, why even use arrays, when containers can do so much more, with so much ease?
Cause arrays are the most space efficient and fastest to traverse.
Well, you don't have to use them, but it's a language construct that has to exist, or else most containers wouldn't even be possible.
[QUOTE=BRadNowacki;35273701]Ok so i just got done learning about containers, and i was wondering from the perspective of more experienced C programers, why even use arrays, when containers can do so much more, with so much ease?[/QUOTE]
Most of the time there's really no reason. But you'll find edge cases where containers won't work, or where they don't offer any extra functionality. In that case you're usually fine with an array.
A vector is better than an array in 100% of cases requiring heap allocation.
[QUOTE=Z_guy;35275470]Well, you don't have to use them, but it's a language construct that has to exist, or else most containers wouldn't even be possible.[/QUOTE]
That much i understand, i was just curious if most people use it
Arrays are quicker to to work with ( as in, the actual time typing the code ) for trivial things.
[QUOTE=BRadNowacki;35319751]That much i understand, i was just curious if most people use it[/QUOTE]
It really depends on in which situation you use them in.
In most cases, if you're storing stuff, you'll be using containers.
But if you're doing some calculation on, for example, some matrices. You're better off using arrays because speed is an issue and the size of the array is static (2d 3x3, 3d 4x4).
Arrays are a primitive data structure that many STL containers are based upon.
Accessing an element in a container often involves three parts: a call to the accessor function, container-specific logic, and an array access from the internal array. Calling the accessor function and performing container-specific logic introduces additional overhead that can be avoided by simply using an array.
Arrays and each type of container have specific strengths / weaknesses. One is not necessarily "better" than another in all cases. Good programmers will try to choose a data structure that works best in the particular context it's being used in.
I use std::vector for pretty much everything, because to be honest I get good enough performance out of it for everything I do anyway, and it's so flexible and easy to work with.
There are some situations where arrays come in useful, specifically API calls. For example, the Win32 API often expects you to use LPCSTR or LPWCSTR which are basically char[] and wchar_t[]'s. (top tip, the A functions let you use standard char's rather than wchar's which is very useful in many situations, e.g. MessageBoxA() rather than MessageBox() )
Another example is D3D9, which expects arrays of your vertex struct for constructing vertex buffers and arrays of unsigned shorts or unsigned ints for constructing index buffers (for 16 bit and 32 bit index buffers respectively)
So I pretty much use std::string and std::vector in every situation except where an API requires you use an array.
[QUOTE=Jallen;35358322]I use std::vector for pretty much everything, because to be honest I get good enough performance out of it for everything I do anyway, and it's so flexible and easy to work with.
There are some situations where arrays come in useful, specifically API calls. For example, the Win32 API often expects you to use LPCSTR or LPWCSTR which are basically char[] and wchar_t[]'s. (top tip, the A functions let you use standard char's rather than wchar's which is very useful in many situations, e.g. MessageBoxA() rather than MessageBox() )
Another example is D3D9, which expects arrays of your vertex struct for constructing vertex buffers and arrays of unsigned shorts or unsigned ints for constructing index buffers (for 16 bit and 32 bit index buffers respectively)
So I pretty much use std::string and std::vector in every situation except where an API requires you use an array.[/QUOTE]
But even then your just using an array to pass something, its not like your using them for storage and manipulation. Arrays are really only good when the function's call for them i have learned
[QUOTE=BRadNowacki;35373147]But even then your just using an array to pass something, its not like your using them for storage and manipulation. Arrays are really only good when the function's call for them i have learned[/QUOTE]
When you know the number of elements you'll need there's no reason not to use a simple array. They are easy to write and use and when it comes down to character count, it takes less typing to work with one.
Another top tip when it comes to containers - in my experience, debug assertion failed almost always means you're trying to access elements in a vector which don't exist, i.e. with an invalid index.
-wrong thread-
[editline]1st April 2012[/editline]
Poobread
[QUOTE=Darwin226;35374540]When you know the number of elements you'll need there's no reason not to use a simple array.[/QUOTE]
That is wrong because it is a gross simplification. There are plenty of reasons to not use arrays even though you know the exact number of elements.
[QUOTE=gparent;35392936]That is wrong because it is a gross simplification. There are plenty of reasons to not use arrays even though you know the exact number of elements.[/QUOTE]
Yeah, I agree. I wasn't really exact but what I meant is when you don't need any of the features containers provide, arrays are simpler to work with.
[QUOTE=gparent;35392936]That is wrong because it is a gross simplification. There are plenty of reasons to not use arrays even though you know the exact number of elements.[/QUOTE]
like what
[QUOTE=swift and shift;35395263]like what[/QUOTE]
Maybe if you want bounds checking. I can only come up with that one reason though.
[QUOTE=shill le 2nd;35395343]Maybe if you want bounds checking. I can only come up with that one reason though.[/QUOTE]
you know the exact number of elements so you just compare the index against the length
[QUOTE=swift and shift;35395263]like what[/QUOTE]
Well there are plenty of members functions on the containers that are still useful with either a constant number of elements or a constant maximum number of elements.
EDIT: For those rating me disagree, post in this thread or just don't bother. People won't guess how I'm supposedly wrong.
[QUOTE=shill le 2nd;35395343]Maybe if you want bounds checking. I can only come up with that one reason though.[/QUOTE]
You'd use assertions for that if you knew the number of elements.
Maybe you know the size at compile-time but not run-time or something.. I dunno, stop pointing out my flawed reasoning :(
[QUOTE=shill le 2nd;35399484]Maybe you know the size at compile-time but not run-time or something.. I dunno, stop pointing out my flawed reasoning :([/QUOTE]
No worries, all I'm saying is that it's a bit naive to say that the choice between container or no container depends solely on you knowing the number of elements in it beforehand :)
If the size is static go for a simple array, if you need to add/remove elements you are probably better off with a container, if you want to go for speed then write your own or look for alternatives such as sparse vectors
Sorry, you need to Log In to post a reply to this thread.