在 C++ 中填充数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14446850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:20:53  来源:igfitidea点击:

filling up an array in c++

c++visual-c++c++11

提问by junni lomo

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.

我是 C++ 新手。我试图编写以下代码来用新值填充数组的每个字节而不覆盖其他值。下面的每个字节 (r) 都应该在数组的新地址处相加。

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

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++ 中是否有任何函数可以做到这一点。在上述情况下,值 'r' 是任意的,它可以有任何新值。因此,结果字符数组应包含值 rrrrr... 10 次。非常感谢。

回答by Rapptz

Using C++11

使用 C++11

#include <algorithm>
#include <iostream>

int main() {
    char array[80];
    std::fill(std::begin(array),std::begin(array)+10,'r');
}

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

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

回答by David Heffernan

You can use the []operator and assign a charvalue.

您可以使用[]运算符并赋值char

char y[80];
for(int b=0; b<10; ++b)
    y[b] = 'r';

And yes, std::fillis a more idiomatic and modern C++ way to do this, but you should know about the []operator too!

是的,这std::fill是一种更惯用和现代的 C++ 方式来做到这一点,但您也应该了解[]运算符!

回答by junni lomo

// ConsoleApp.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

int fun(bool x,int y[],int length);
int funx(char y[]);
int functionx(bool IsMainProd, int MainProdId, int Addons[],int len);
int _tmain(int argc, _TCHAR* argv[])
{
    int AddonCancel[10];

    for( int i = 0 ; i<4 ;++i)
    {
        std::fill(std::begin(AddonCancel)+i,std::begin(AddonCancel)+i+1,i*5);
    }
    bool IsMainProduct (false);
    int MainProduct =4 ; 
    functionx(IsMainProduct,MainProduct,AddonCancel,4);

}

int functionx(bool IsMainProd, int MainProdId, int Addons[],int len)
{
    if(IsMainProd)
        std::cout<< "Is Main Product";
    else
    {
        for(int x = 0 ; x<len;++x)
        {
          std::cout<< Addons[x];
        }
    }

    return 0 ; 
}

回答by Arun

Option 1: Initialize the array while defining. Convenient for initializing only a small number of values. Advantage is that the array can be declared const(not shown here).

选项1:定义时初始化数组。便于仅初始化少量值。优点是可以声明数组const(此处未显示)。

char const fc = 'r';   // fill char
char y[ 80 ] = { fc, fc, fc, fc,
                 fc, fc, fc, fc,
                 fc, fc };

Option 2: Classic C

选项 2:经典 C

memset( y, y+10, 'r' );

Option 3: Classic (pre-C++11) C++

选项 3:经典(C++11 之前)C++

std::fill( y, y+10, 'r' );