用C ++填充数组 [英] filling up an array in c++

查看:93
本文介绍了用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 ++填充数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆