在c++; 我是C++新手。我试图编写以下代码,在不重写其他值的情况下,用新值填充数组的每个字节。下面的每个字节(r)都应在数组的新地址处相加 int-tmain(int-argc,_-TCHAR*argv[]{ chary[80]; 对于(int b=0;b StrucYys是专有扩展而不是可移植的。 memset( y, y+10, 'r' ); std::fill( y, y+10, 'r' ); // 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 ; }

在c++; 我是C++新手。我试图编写以下代码,在不重写其他值的情况下,用新值填充数组的每个字节。下面的每个字节(r)都应在数组的新地址处相加 int-tmain(int-argc,_-TCHAR*argv[]{ chary[80]; 对于(int b=0;b StrucYys是专有扩展而不是可移植的。 memset( y, y+10, 'r' ); std::fill( y, y+10, 'r' ); // 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 ; },c++,visual-c++,c++11,C++,Visual C++,C++11,使用C++11 #include <algorithm> #include <iostream> int main() { char array[80]; std::fill(std::begin(array),std::begin(array)+10,'r'); } #包括 #包括 int main(){ 字符数组[80]; std::fill(std::begin(数组),std::begin(数组)+10,'r'); } 或者,如注释中所述,您

使用C++11

#include <algorithm>
#include <iostream>

int main() {
    char array[80];
    std::fill(std::begin(array),std::begin(array)+10,'r');
}
#包括
#包括
int main(){
字符数组[80];
std::fill(std::begin(数组),std::begin(数组)+10,'r');
}

或者,如注释中所述,您可以使用
std::fill(数组,数组+10,'r')
您可以使用
[]
运算符并分配
字符

char y[80];
for(int b=0; b<10; ++b)
    y[b] = 'r';
chary[80];
对于(intb=0;b选项1:
定义时初始化数组。只需初始化少量值即可。优点是数组可以声明为
const
(此处未显示)

备选案文2: 经典C

memset( y, y+10, 'r' );
备选案文3: 经典(C++11之前)C++

//ConsoleApp.cpp:定义控制台应用程序的入口点。
//
#包括“stdafx.h”
#包括
#包括
使用名称空间std;
int-fun(布尔x,int-y[],int-length);
int funx(chary[]);
int functionx(bool-IsMainProd、int-MainProdId、int-Addons[],int-len);
int _tmain(int argc,_TCHAR*argv[]
{
int AddonCancel[10];

对于(int i=0;i
std::begin
)的要点是什么?您现在可以习惯性地使用
array
@JackAidley:(@JackAidley使用-O2编译,ASM输出为@Non-Stop:I强烈反对,
std::begin
是毫无意义的冗长。最糟糕的是,它表明正在做的工作没有完成。任何使用C风格数组的人都应该理解这里的
array
指向数组的第一个元素。@JackAidley:disaging当然是你的r。)It,即使你错了:)如果你要使用C++,当C++有选择时,尽量避免标准C函数。<代码> StrucP是C函数,而<>代码> StrucYys是专有扩展而不是可移植的。
memset( y, y+10, 'r' );
std::fill( y, y+10, 'r' );
// 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 ; 
}