Arrays in C++
#include <iostream>
#include <array>
int main() {
// Here we create an array 'a' that will hold exactly
// 5 ints. The type of elements and length are both
// part of the array's type. By default an array is
// zero-initialized, which for ints means 0s.
std::array<int, 5> a = {};
std::cout << "emp:";
for (int i : a) {
std::cout << " " << i;
}
std::cout << std::endl;
// We can set a value at an index using the
// array[index] = value syntax, and get a value with
// array[index].
a[4] = 100;
std::cout << "set:";
for (int i : a) {
std::cout << " " << i;
}
std::cout << std::endl;
std::cout << "get: " << a[4] << std::endl;
// The size() function returns the length of an array.
std::cout << "len: " << a.size() << std::endl;
// Use this syntax to declare and initialize an array
// in one line.
std::array<int, 5> b = {1, 2, 3, 4, 5};
std::cout << "dcl:";
for (int i : b) {
std::cout << " " << i;
}
std::cout << std::endl;
// C++ doesn't have a direct equivalent to Go's `...` syntax,
// but you can use initializer lists with std::array.
std::array<int, 5> c = {1, 2, 3, 4, 5};
std::cout << "init list:";
for (int i : c) {
std::cout << " " << i;
}
std::cout << std::endl;
// C++ doesn't have a direct equivalent to Go's `3: 400` syntax,
// but you can initialize specific elements manually.
std::array<int, 5> d = {100, 0, 0, 400, 500};
std::cout << "manual init:";
for (int i : d) {
std::cout << " " << i;
}
std::cout << std::endl;
// Array types are one-dimensional, but you can
// compose types to build multi-dimensional data
// structures.
std::array<std::array<int, 3>, 2> twoD = {};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
twoD[i][j] = i + j;
}
}
std::cout << "2d: ";
for (const auto& row : twoD) {
for (int val : row) {
std::cout << val << " ";
}
}
std::cout << std::endl;
// You can create and initialize multi-dimensional
// arrays at once too.
std::array<std::array<int, 3>, 2> twoD2 = {{{1, 2, 3}, {1, 2, 3}}};
std::cout << "2d: ";
for (const auto& row : twoD2) {
for (int val : row) {
std::cout << val << " ";
}
}
std::cout << std::endl;
}
This C++ code demonstrates the usage of arrays, which are implemented using the std::array
container from the C++ Standard Library. The std::array
provides a fixed-size array with bounds checking.
Key points in the C++ version:
We use
std::array<T, N>
instead of Go’s built-in array type.T
is the type of elements, andN
is the size of the array.To print array contents, we use range-based for loops or regular for loops, as C++ doesn’t have a direct equivalent to Go’s
fmt.Println
for arrays.The
len()
function in Go is replaced by thesize()
member function ofstd::array
.C++ doesn’t have the
...
syntax for array initialization, but we can use initializer lists.C++ doesn’t have a direct equivalent to Go’s
3: 400
syntax for sparse array initialization. We initialize such arrays manually.For multi-dimensional arrays, we use nested
std::array
types.
When you compile and run this program, it will produce output similar to the Go version, demonstrating various ways to create, initialize, and manipulate arrays in C++.