What's a c++ style array?

I am getting heat from any body of c++ coder for using "C-Style Arrays." How do I make C++ Style arrays?

Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//C-Style arrays.
#include <iostream>

int main()
{
    int i;
    int someNums[5]{10,8,6,4,2};
    
    std::cout << "Type a number 1 - 5.";
    std::cin  >> i;
    
    if(i > 0 && i < 6)
    {
        --i;
    }else{
        std::cout << "Unable to use your input.";
        return -1;
    }
    
    std::cout << someNums[i];
}
Line 7 is missing = .

In c++ there is template , so you would use the stl (standard template libraries)

1
2
#include <array>
std::array<Type , size> name;
closed account (E0p9LyTq)
How do I make C++ Style arrays?

Use std::array if you want/need a compile-time fixed size, otherwise use std::vector.
http://www.cplusplus.com/reference/array/
Ericool wrote:
Line 7 is missing = .

Since C++11 you don't need it.

Ericool wrote:
std::array<Type , size> name;

Note that the size need to be a compile time constant (constexpr).

If you don't know the size at compile time or you want the size to vary you can use std::vector instead.
http://www.cplusplus.com/reference/vector/vector/

To be honest, I use std::vector much more often than I use std::array.
Last edited on
closed account (E0p9LyTq)
Since C++11 you don't need it.

C++11 changed so many things, it is almost an entirely different language from previous standards.
Last edited on
@Peter87 I did not know that one :) .
And yes size must be const like a normal c-array.
Then use std::array. It is no less efficient than C-arrays, but much safer.
In C++17 a helper function make_array will be introduced, allowing array dimensions (and optionally type) be deduced from argument list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <array>

using namespace std::literals;

int main()
{
    //Explicitely mentioning size
    int foo[5] {1, 2, 3, 4, 5};
    std::array<int, 5> bar {1, 2, 3, 4, 5};

    //Deducing size from init list
    int c_array[] = {0, 1, 0}; //arr has type int[3]
    auto cpp_array = std::make_array(0, 1, 0); //C++17


    //String literals converted to constant character pointers
    auto cp_array = std::make_array("Hello", "World"); //cp_array value_type is const char*
    //Explicitely mentioning type:
    auto str_array = std::make_array<std::string>("Hello", "World") //str_array value_type is std::string
    //Using type deduction and string literals:
    auto s_array = std::make_array("Hello"s, "World"s); //s_array value_type is std::string
}

In C++17 a helper function make_array will be introduced

It's in Library Fundamentals TS version 2 ( http://en.cppreference.com/w/cpp/experimental/make_array ). It may not make it to C++17 proper since even version 1 isn't part of C++17 yet... which doesn't mean compilers wont' support it, after all gcc already supports most of version 1.
Last edited on
Topic archived. No new replies allowed.