用C ++填充数组

编程入门 行业动态 更新时间:2024-10-23 20:27:54
本文介绍了用C ++填充数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是c ++的新手.我试图编写以下代码,用新值填充数组的每个字节,而不覆盖其他值.下面的每个字节(r)都应加到数组的新地址上.

I am new to c++ . I was trying to write following code to fill up each byte of array with new values without overriding others. Each byte (r) below should be added up at new address of the array.

int _tmain(int argc, _TCHAR* argv[]) { char y[80]; for(int b = 0; b < 10; ++b) { strcpy_s(y, "r"); } }

请让我知道c ++中是否有任何函数可以做到这一点.在上述情况下,值"r"是任意的,并且可以具有任何新值.因此,所得的字符数组应包含值rrrrr ... 10次.对此非常感谢.

Please let me know if there is any function in c++ which can do that. In the above case the value 'r' is arbitrary and this can have any new value. So the resultant array of characters should contain value rrrrr... 10 times. Thanks a lot in advance for this.

推荐答案

使用C ++ 11

#include <algorithm> #include <iostream> int main() { char array[80]; std::fill(std::begin(array),std::begin(array)+10,'r'); }

或者,如注释中所述,您可以使用 std :: fill(array,array + 10,'r').

Or, as mentioned in the comments you can use std::fill(array,array+10,'r').

更多推荐

用C ++填充数组

本文发布于:2023-08-05 21:23:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1308136.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!