From C to ?

Started by
19 comments, last by slicer4ever 10 years, 1 month ago

sorry for not specifying the array part. i meant the way it is represented in c++11.

int arr[3][4] is so much clearer than std::array <std::array <int, 3>, 4> arr, at least to me.


perhaps this well help you understand why std::array is preferred:

c++11 std::array way:
http://ideone.com/XpP7Zm

c++ array way:
http://ideone.com/ey6aC8

std::array allows you to use the entire suite of std container algorithms, that you can't use with a plain old array, it also includes a handy size field, so no longer do you have to keep track of how many elements you have. there's plenty of other reasons to be using std::array, but being able to use a static array with container algorithms should be enough imo.

[spoiler]


#include <iostream>
#include <algorithm>

int main() {
	std::array<int, 5> a = {1,2,3,4,5};
	for(int x : a) std::cout << "a: "<< x << std::endl;
	return 0;
}

    #include <iostream>
     
    int main() {
    const int ALen = 5;
    int A[ALen] = {1,2,3,4,5};
    for(int i=0;i<ALen;i++) std::cout << "a: " << A[i] << std::endl;
    return 0;
    }

[/spoiler]

Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

This topic is closed to new replies.

Advertisement