我是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 ++填充数组
发布评论