For example,
class JJ
{
public:
JJ(int c)
{
a = c;
}
int a;
};
I can write
JJ z[2] = {6,6};
But for JJ z[100], I can't write {6,6,6....,6}, so what can I do with that?
If std::vector doesn't fit your needs with
std::vector<JJ> v(100, JJ(6));
You may use std::array
namespace detail
{
template <std::size_t N, std::size_t...Is, typename T>
std::array<T, N> make_array(std::index_sequence<Is...>, const T& t)
{
return {{(static_cast<void>(Is), t)...}};
}
}
template <std::size_t N, typename T>
std::array<T, N> make_array(const T& t)
{
return detail::make_array<N>(std::make_index_sequence<N>{}, t);
}
And then
std::array<JJ, 100> a = make_array<100>(JJ(6));
std::vector<JJ> z(100, 6);to create 100 elements with value 6.std::arraytemplated container type in the standard library, possibly even usage of other standard containers (likestd::vector). Each choice has different advantages, depending on what you are ACTUALLY doing. Containers are often considered vastly preferable to C-style arrays, even if beginners think otherwise.