How to declare, initialize and print array object in C++

2 Answers

0 votes
#include <iostream>
#include <array>

using std::cout;
using std::endl;
using std::array;

template <typename T>
inline void print(const T &data)
{
	for (const auto &element : data) {
		cout << element << ' ';
	}
	cout << endl;
}

int main()
{
	array<int, 5> arr = { 1, 2, 3, 4, 5 };

	print(arr);

	return 0;
}


/*
run:

1 2 3 4 5

*/

 



answered Feb 1, 2018 by avibootz
0 votes
#include <iostream>
#include <array>

using std::cout;
using std::endl;
using std::array;

template <typename T>
inline void print(const T &data)
{
	for (const auto &element : data) {
		cout << element << ' ';
	}
	cout << endl;
}

int main()
{
	array<int, 6> arr = { 1, 2, 3 };

	print(arr);

	return 0;
}


/*
run:

1 2 3 0 0 0

*/

 



answered Feb 1, 2018 by avibootz

Related questions

1 answer 100 views
1 answer 146 views
1 answer 159 views
1 answer 136 views
...