filling up an array in c++

38,619

Solution 1

Using 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').

Solution 2

You can use the [] operator and assign a char value.

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

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

Solution 3

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).

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

Option 2: Classic C

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

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

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

Solution 4

// 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 ; 
}
Share:
38,619
junni lomo
Author by

junni lomo

Updated on September 28, 2020

Comments

  • junni lomo
    junni lomo over 3 years

    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");
        }
    }
    

    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.

  • Jack Aidley
    Jack Aidley over 11 years
    What's the point of the std::begin? You can simply use array.
  • Rapptz
    Rapptz over 11 years
    @JackAidley Out of habit now :(
  • Jack Aidley
    Jack Aidley over 11 years
    Out of idle interest, how does the performance of std::fill compare to memset?
  • Rapptz
    Rapptz over 11 years
    @JackAidley compiling with -O2 and the ASM output is near identical
  • Lightness Races in Orbit
    Lightness Races in Orbit over 11 years
    @JackAidley: Documented intent. std::begin is the "correct" tool to use here.
  • Lightness Races in Orbit
    Lightness Races in Orbit over 11 years
  • Jack Aidley
    Jack Aidley over 11 years
    @Non-Stop: I strongly disagree, std::begin is pointlessly verbose. Worst, it suggests that work is being done that isn't. Anyone using C-style arrays should understand that array here points to the first element of the array.
  • Lightness Races in Orbit
    Lightness Races in Orbit over 11 years
    @JackAidley: Disagreeing is certainly your right, even though you're wrong. :)
  • balki
    balki over 11 years
    begin()+10 is ugly, Use fill(begin(array),end(array),'r');
  • Rapptz
    Rapptz over 11 years
    @balki the original question was to fill the first 10 elements.