array元素的获取 图片看不了?点击切换HTTP 返回上层
可以通过在方括号中使用索引表达式汸问和使用数组容器的元素,这和标准数组的访问方式相同,例如:
1 | values [4] = values [3] + 2.O* values [1]; |
1 | values . at (4) = values . at (3) + 2.O* values . at (1); |
数组对象的 size() 函数能够返回 size_t 类型的元素个数值,所以能够像下面这样去计算数组所有元素的和:
1 2 3 4 5 | double total {}; for (size_t i {} ; i < values . size () ; ++i) { total += values [i]; } |
1 2 3 4 | if( values .empty()) std::cout << "The container has no elements.\n" ; else std::cout << "The container has " << values . size ()<< "elements.\n" ; |
对于任何可以使用迭代器的容器,都可以使用基于范围的循环,因此能够更加简便地计算容器中所有元素的和:
1 2 3 | double total {}; for (auto&& value : values ) total += value; |
模板函数 get<n>() 是一个辅助函数,它能够获取到容器的第 n 个元素。模板参数的实参必须是一个在编译时可以确定的常量表达式,所以它不能是一个循环变量。它只能访问模板参数指定的元素,编译时会对它进行检查。get<n>() 模板提供了一种不需要在运行时检查,但能用安全的索引值访问元素的方式。下面展示如何使用它:
1 2 3 | std::array<std::string, 5> words { "one" , "two" , "three”," four "," five" }; std::cout << std::get<3>(words) << std::endl; // Output words[3] std::cout << std::get<6>(words) << std::endl; // Compiler error message! |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | /**示例 1**/ #include <iostream> // For standard streams #include <iomanip> // For stream manipulators #include <array> // For array<T,N> int main() { const unsigned int min_wt {100U}; const unsigned int max_wt {250U}; const unsigned int wt_step {10U}; const size_t wt_count {1 + (max_wt - min_wt) / wt_step}; const unsigned int min_ht {48U}; const unsigned int max_ht {84U}; const unsigned int ht_step {2U}; const size_t ht_count { 1 + (max_ht - min_ht) / ht_step }; const double lbs_per_kg {2.20462}; const double ins_per_m {39.3701}; std::array<unsigned int , wt_count> weight_lbs; std::array<unsigned int , ht_count> height_ins; // Create weights from lOOlbs in steps of lOlbs for (size_t i{}, w{min_wt} ; i < wt_count ; w += wt_step, ++i) { weight_lbs. at (i) = w; } // Create heights from 48 inches in steps of 2 inches unsigned int h{min_ht}; for (auto& height : height_ins) { height = h; h += ht_step; } // Output table headings std::cout << std:: setw (7) << " |" ; for (const auto& w : weight_lbs) std::cout << std:: setw (5) << w<< "11" ; std::cout << std::endl; // Output line below headings for (size_t i{1} ; i < wt_count ; ++i) std::cout<< "---------" ; std::cout << std::endl; double bmi {}; unsigned int feet {}; unsigned int inches {}; const unsigned int inches_per_foot {12U}; for (const auto& h : height_ins) { feet = h / inches_per_foot; inches = h % inches_per_foot; std::cout << std::setw (2) <<feet << "'" <<std::setw (2) << inches << "\"" << "|" ; std::cout << std::fixed <<std::setprecision(1); for (const auto& w : weight_lbs) { bmi = h / ins_per_m; bmi = (w / lbs_per_kg) / (bmi*bmi); std::cout << std:: setw (2) << "" <<bmi << " |" ; } std::cout << std::endl; } for (size_t i {1} ; i < wt_count ; ++i) std::cout << "---------" ; std::cout << "\nBMI from 18.5 to 24.9 is normal" << std::endl; } |
在循环中,可以用适当的值初始化容器。第一个循环展示了 at() 函数的使用,这里也可以放心地使用 weight_lbs[i]。接下来的两个循环分别输出了表的列头,以及一条用来分隔表头和表的横线。数据表是以循环嵌套的方式生成的。外循环遍历身高并输出英尺和英寸的最左一列的身高。内循环遍历体重,输出当前身高每行的 BMI 值。