首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

C++:数组的构造函数/初始化程序?

在C++中,数组的构造函数/初始化程序可以通过以下几种方式来实现:

  1. 使用花括号进行初始化:int arr[5] = {1, 2, 3, 4, 5};
  2. 使用std::array进行初始化:#include<array> std::array<int, 5> arr = {1, 2, 3, 4, 5};
  3. 使用std::vector进行初始化:#include<vector> std::vector<int> arr = {1, 2, 3, 4, 5};
  4. 使用std::initializer_list进行初始化:#include<initializer_list> std::initializer_list<int> arr = {1, 2, 3, 4, 5};
  5. 使用std::fill进行初始化:#include<algorithm> #include<vector> std::vector<int> arr(5); std::fill(arr.begin(), arr.end(), 0);
  6. 使用std::fill_n进行初始化:#include<algorithm> #include<vector> std::vector<int> arr(5); std::fill_n(arr.begin(), 5, 0);
  7. 使用std::generate进行初始化:#include<algorithm> #include<vector> #include<random> std::vector<int> arr(5); std::generate(arr.begin(), arr.end(), std::rand);
  8. 使用std::generate_n进行初始化:#include<algorithm> #include<vector> #include<random> std::vector<int> arr(5); std::generate_n(arr.begin(), 5, std::rand);

以上是C++中数组的构造函数/初始化程序的常见方法,可以根据具体需求选择合适的方法进行初始化。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

OJ刷题记录:L1-206-学霸递情书(15分)

题目要求: 李雷和韩梅梅坐前后排。上课想说话怕老师发现,所以改为传小纸条。为了被老师发现他们纸条上说的是啥,他们约定了如下方法传递信息: 将26个英文字母(全为大写),外加空格,一共27个字符分成三组,每组9个。也就是ABCDEFGHI是第一组,JKLMNOPQR是第二组,STUVWXYZ是第三组(此处用代表空格)。 然后根据传递纸条那天的日期,改变字母的位置 先根据月份数m, 以整个分组为单位进行循环左移,移动(m-1)次。然后根据日期数d,对每个分组内的字符进行循环左移,移动(d-1)次。 例如:以3月8号为例,首先移动分组,3月需要循环左移2次,变成:STUVWXYZ*,ABCDEFGHI,JKLMNOPQR 然后对每组内的字符,8日需要循环左移7次,最终编程: Z*STUVWXY,HIABCDEFG,QRJKLMNOP 对于需要传递信息中的每个字符。用组号和组内序号两个数字来表示。 如果在3月8号需要传递信息为“HAPPY”,那么H位于第2组第1个,A位于第2组第3个…依次类推。所以最终纸条上写成: 21 23 39 39 19 现给定日期和需要传递的信息,请输出应该写在纸条上的编码。 输入 每个输入包含两行。 第一行是用空格分隔的两个数字,分别代表月份和日子。输入保证是一个合法的日期 第二行为需要编码的信息字符串,仅由大写字母A~Z和空格组成,字符串长度不超过1024个字符。 输出 对每个输入,打印对应的编码,数字之间用空格分隔,每个输出占一行。 样例输入 Copy 3 8 HAPPY 样例输出 Copy 21 23 39 39 19

02

ACM竞赛常用STL(二)之STL--algorithm

<algorithm>无疑是STL 中最大的一个头文件,它是由一大堆模板函数组成的。 下面列举出<algorithm>中的模板函数: adjacent_find / binary_search / copy / copy_backward / count / count_if / equal / equal_range / fill / fill_n / find / find_end / find_first_of / find_if / for_each / generate / generate_n / includes / inplace_merge / iter_swap / lexicographical_compare / lower_bound / make_heap / max / max_element / merge / min / min_element / mismatch / next_permutation / nth_element / partial_sort / partial_sort_copy / partition / pop_heap / prev_permutation / push_heap / random_shuffle / remove / remove_copy / remove_copy_if / remove_if / replace / replace_copy / replace_copy_if / replace_if / reverse / reverse_copy / rotate / rotate_copy / search / search_n / set_difference / set_intersection / set_symmetric_difference / set_union / sort / sort_heap / stable_partition / stable_sort / swap / swap_ranges / transform / unique / unique_copy / upper_bound 如果详细叙述每一个模板函数的使用,足够写一本书的了。还是来看几个简单 的示例程序吧。 示例程序之一,for_each 遍历容器:

03
领券