Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 以C+;格式返回生成的数组+;_C++_Arrays_Function_Compiler Errors_Return - Fatal编程技术网

C++ 以C+;格式返回生成的数组+;

C++ 以C+;格式返回生成的数组+;,c++,arrays,function,compiler-errors,return,C++,Arrays,Function,Compiler Errors,Return,很多人可能认为这是一个已回答的问题,但我的情况不同,因为我在函数中声明了数组。问题是:我想制作一个字符串拆分器,它将字符串分割成块,这些块将成为数组的一部分。基本源代码是: string separate(string str, int chunk, string mode = "res2end"){ int strlen = str.length(); int full = strlen / chunk; int arr_size = full+1; string result[arr

很多人可能认为这是一个已回答的问题,但我的情况不同,因为我在函数中声明了数组。问题是:我想制作一个字符串拆分器,它将字符串分割成块,这些块将成为数组的一部分。基本源代码是:

string separate(string str, int chunk, string mode = "res2end"){

int strlen = str.length();

int full = strlen / chunk;

int arr_size = full+1;

string result[arr_size]; //this is the 25th row

int modf = strlen % chunk;

    for(unsigned i=0; i<full; i++){
        int msr = i*chunk;
        int sp = msr - chunk;
        string subres = str.substr(msr, chunk);
        result[i] = subres;
    }

    if(modf != 0){
        int restm = strlen - (full * chunk);
        result[full+1] = str.substr(restm, modf);
    }

return result;
}
因此,如果有人与我分享解决方案,我将非常高兴

>可变长度数组(VLAS)不是C++标准的一部分(??) 您应该使用一个动态分配的数组,这是您的最佳选择。这还允许您返回数组,因为您也不能从函数返回原始数组(或将数组传递给函数)。也请注意更改返回类型。

std::vectorseparate(string str,size\t chunk,string mode=“res2end”){
std::vector< string > separate(string str, size_t chunk, string mode = "res2end"){
    size_t strlen = str.length();
    size_t full = strlen / chunk;
    std::vector< string > result; //this is the 25th row
    int modf = strlen % chunk;

    for(size_t i=0; i<full; i++){
        int msr = i*chunk;
        int sp = msr - chunk;
        result.push_back( str.substr(msr, chunk));
    }

    if(modf){
        int restm = strlen - (full * chunk);
        result.push_back( str.substr(restm, modf) );
    }

    return result;  // This could be a costly copy if prior to move introduced in C++11
}
长度=长度(); 完整大小=strlen/块; std::vectorresult;//这是第25行 int modf=strlen%chunk;
对于(size\t i=0;i您正在创建字符串数组,而不是单个字符串。您的函数返回单个字符串,但“result”变量是一个数组。

string result[arr\u size]不是合法C++。编译器可能支持它,但是<代码> ARRIZHEATEXION/CODE >需要编译时常数。使用<代码> STD::vector < /代码>作为<代码>结果< /代码>和返回类型。
std::vector< string > separate(string str, size_t chunk, string mode = "res2end"){
    size_t strlen = str.length();
    size_t full = strlen / chunk;
    std::vector< string > result; //this is the 25th row
    int modf = strlen % chunk;

    for(size_t i=0; i<full; i++){
        int msr = i*chunk;
        int sp = msr - chunk;
        result.push_back( str.substr(msr, chunk));
    }

    if(modf){
        int restm = strlen - (full * chunk);
        result.push_back( str.substr(restm, modf) );
    }

    return result;  // This could be a costly copy if prior to move introduced in C++11
}