78 lines
2.7 KiB
C++
78 lines
2.7 KiB
C++
|
// fig06_10.cpp
|
||
|
// Initializing multidimensional arrays.
|
||
|
#include <iostream>
|
||
|
#include <array>
|
||
|
#include <vector>
|
||
|
|
||
|
constexpr size_t rows{2};
|
||
|
constexpr size_t columns{3};
|
||
|
void printArray(const std::array<std::array<int, columns>, rows>& a);
|
||
|
|
||
|
template <typename T>
|
||
|
void printArrayTemplate(const T& a);
|
||
|
|
||
|
int main() {
|
||
|
constexpr std::array values1{std::array{1, 2, 3}, std::array{4, 5, 6}};
|
||
|
constexpr std::array values2{std::array{1, 2, 3}, std::array{4, 5, 0}};
|
||
|
|
||
|
constexpr std::array < std::array<int,3>, 2> values3{std::array{1, 2, 3}, std::array{4, 5, 6}};
|
||
|
|
||
|
std::vector < std::vector<int>> values3_vec{std::vector<int>{1, 2, 3}, std::vector<int>{4, 5, 6}};
|
||
|
|
||
|
std::cout << "values1 by row:\n";
|
||
|
printArray(values1);
|
||
|
|
||
|
std::cout << "\nvalues2 by row:\n";
|
||
|
printArray(values2);
|
||
|
|
||
|
std::cout << "\nvalues3 by row:\n";
|
||
|
printArrayTemplate(values3);
|
||
|
|
||
|
std::cout << "\nvalues3_vec by row:\n";
|
||
|
printArrayTemplate(values3_vec);
|
||
|
|
||
|
}
|
||
|
|
||
|
// output array with two rows and three columns
|
||
|
void printArray(const std::array<std::array<int, columns>, rows>& a) {
|
||
|
// loop through array's rows
|
||
|
for (const auto& row : a) {
|
||
|
// loop through columns of current row
|
||
|
for (const auto& element : row) {
|
||
|
std::cout << element << ' ';
|
||
|
}
|
||
|
|
||
|
std::cout << '\n'; // start new line of output
|
||
|
}
|
||
|
}
|
||
|
|
||
|
template <typename T>
|
||
|
void printArrayTemplate(const T& a) {
|
||
|
// loop through array's rows
|
||
|
for (const auto& row : a) {
|
||
|
// loop through columns of current row
|
||
|
for (const auto& element : row) {
|
||
|
std::cout << element << ' ';
|
||
|
}
|
||
|
|
||
|
std::cout << '\n'; // start new line of output
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
/**************************************************************************
|
||
|
* (C) Copyright 1992-2022 by Deitel & Associates, Inc. and *
|
||
|
* Pearson Education, Inc. All Rights Reserved. *
|
||
|
* *
|
||
|
* DISCLAIMER: The authors and publisher of this book have used their *
|
||
|
* best efforts in preparing the book. These efforts include the *
|
||
|
* development, research, and testing of the theories and programs *
|
||
|
* to determine their effectiveness. The authors and publisher make *
|
||
|
* no warranty of any kind, expressed or implied, with regard to these *
|
||
|
* programs or to the documentation contained in these books. The authors *
|
||
|
* and publisher shall not be liable in any event for incidental or *
|
||
|
* consequential damages in connection with, or arising out of, the *
|
||
|
* furnishing, performance, or use of these programs. *
|
||
|
**************************************************************************/
|