Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.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++;:如何使用new查找函数返回值的存储? 我正在阅读Bjarne Stroustrup的第三版C++程序,并试图完成所有的练习。我不确定如何处理第6.6节中的练习13,所以我想我应该转向堆栈溢出来获得一些见解。以下是问题的描述:_C++ - Fatal编程技术网

C++;:如何使用new查找函数返回值的存储? 我正在阅读Bjarne Stroustrup的第三版C++程序,并试图完成所有的练习。我不确定如何处理第6.6节中的练习13,所以我想我应该转向堆栈溢出来获得一些见解。以下是问题的描述:

C++;:如何使用new查找函数返回值的存储? 我正在阅读Bjarne Stroustrup的第三版C++程序,并试图完成所有的练习。我不确定如何处理第6.6节中的练习13,所以我想我应该转向堆栈溢出来获得一些见解。以下是问题的描述:,c++,C++,编写一个函数cat(),该函数接受两个C样式的字符串参数和 返回作为参数串联的单个字符串。 使用new查找结果的存储 这是我到目前为止的代码,带有问号,我不知道该怎么做: ? cat(char first[], char second[]) { char current = ''; int i = 0; while (current != '\0') { current = first[i]; // somehow append

编写一个函数cat(),该函数接受两个C样式的字符串参数和 返回作为参数串联的单个字符串。 使用new查找结果的存储

这是我到目前为止的代码,带有问号,我不知道该怎么做:

? cat(char first[], char second[])
{
    char current = '';
    int i = 0;

    while (current != '\0')
    {
        current = first[i];
        // somehow append current to whatever will eventually be returned
        i++;
    }

    current = '';
    i = 0;

    while (current != '\0')
    {
        current = second[i];
        // somehow append current to whatever will eventually be returned
        i++;
    }

    return ?
}

int main(int argc, char* argv[])
{
    char first[]  = "Hello, ";
    char second[] = "World!";

    ? = cat(first, second);

    return 0;
}
以下是我的问题:

  • 我如何使用new来查找商店?我是否需要做一些类似于
    std::string*result=newstd::string的事情还是应该使用new以某种方式创建另一个C样式字符串
  • 与上一个问题相关,我应该从cat()返回什么?如果我必须使用new,我假设它必须是一个指针。但是指向什么的指针呢
  • 虽然这个问题没有提到使用delete来释放内存,但我知道我应该这样做,因为我将使用new来分配内存。我应该在返回之前删除main末尾的内容吗
  • 在“真实”程序中,可以使用std::string。听起来这个示例希望您使用C字符串

    所以可能是这样的:

    char * cat(char first[], char second[])
    {
        char *result = new char[strlen(first) + strlen(second) + 1];
    
    int n = 0;
    int k = 0;
    
    //also can use strlen
    while( first[n] != '\0' )
         n ++ ;
    while( second[k] != '\0' )
         k ++ ;
    
    //now, the allocation
    
    char* joint = new char[n+k+1]; //+1 for a '\0'
    
    //and for example memcpy for joining
    
    memcpy(joint, first, n );
    memcpy(joint+n, second, k+1); //also copying the null
    
    
    return joint;
    

    问:你如何“附加”

    答:只要把所有的东西都写在“第一”到“结果”上就行了

    完成后,继续在“秒”中写下所有内容以获得结果(从您结束的地方开始)。完成后,请确保在末尾附加“\0”

    我如何使用new来查找商店?我是否需要做一些类似于
    std::string*result=newstd::string的事情还是应该使用new以某种方式创建另一个C样式字符串

    后者;该方法接受C样式的字符串,文本中没有任何内容表明它应该返回任何其他内容。因此,函数的原型应该是
    char*cat(char-const*,char-const*)
    。当然,这不是通常编写函数的方式;在现代C++中,手动内存管理是完全禁忌的,因为它容易出错。 虽然这个问题没有提到使用delete来释放内存,但我知道我应该这样做,因为我将使用new来分配内存。我应该在返回之前删除main末尾的内容吗

    在这个练习中,是的。在现实世界中,不:就像我上面说的,这是完全禁忌的。实际上,您将返回一个
    std::string
    ,而不使用
    new
    分配内存如果您发现自己手动分配内存(并且假设这是有充分理由的),您应该将该内存放在智能指针中而不是原始指针中–
    std::unique_ptr
    std::shared_ptr

  • 您应该返回一个C样式的字符串,因此不能使用
    std::string
    (或者至少,这不是“问题的本质”)。是的,您应该使用
    new
    创建C样式字符串
  • 您应该返回生成的C样式字符串。。。因此,指向新创建字符串的第一个字符的指针
  • 正确,您应该删除结尾处的结果。我希望它可能会被忽略,因为在这个特殊的情况下,它可能没那么重要-但是为了完整性/正确性,您应该这样做
    练习的意思是使用
    new
    来分配内存。“寻找商店”的措辞很奇怪,但事实上它就是这么做的。你告诉它你需要多少存储空间,它就会找到一个你可以使用的可用内存块,并返回它的地址

    看起来练习并不希望您使用std::string。听起来您需要返回一个
    char*
    。因此,功能原型应为:

    char*cat(const char first[],const char second[])

    请注意
    常量
    说明符。这很重要,这样您就可以将字符串文本作为参数传递

    因此,在不直接给出代码的情况下,您需要做的是确定生成的
    char*
    字符串应该有多大,使用
    new
    分配所需的数量,将两个源字符串复制到新分配的空间中,然后返回它


    注意,C++中通常不手动进行这种内存管理(使用<代码> STD::String ),但了解它仍然很重要,这就是为什么这个练习的原因。

    < P>这里有一些我从一个项目中挖掘出来的旧代码:

    char* mergeChar(char* text1, char* text2){
        //Find the length of the first text
        int alen = 0;
        while(text1[alen] != '\0')
            alen++;
    
        //Find the length of the second text
        int blen = 0;
        while(text2[blen] != '\0')
            blen++;
    
        //Copy the first text
        char* newchar = new char[alen + blen + 1];
        for(int a = 0; a < alen; a++){
                newchar[a] = text1[a];
        }
    
        //Copy the second text
        for(int b = 0; b < blen; b++)
            newchar[alen + b] = text2[b];
    
        //Null terminate!
        newchar[alen + blen] = '\0';
        return newchar;
    }
    
    char*mergeChar(char*text1,char*text2){
    //查找第一个文本的长度
    int-alen=0;
    while(text1[alen]!='\0')
    alen++;
    //查找第二个文本的长度
    int-blen=0;
    while(text2[blen]!='\0')
    blen++;
    //复制第一个文本
    char*newchar=newchar[alen+blen+1];
    对于(int a=0;a

    通常,在“真实”程序中,您需要使用
    std::string
    。请确保稍后删除[]newchar
  • 似乎需要使用
    new
    为字符串分配内存,然后返回指针。因此,
    cat
    返回类型将是
    `char*

    您可以这样做:

    char * cat(char first[], char second[])
    {
        char *result = new char[strlen(first) + strlen(second) + 1];
    
    int n = 0;
    int k = 0;
    
    //also can use strlen
    while( first[n] != '\0' )
         n ++ ;
    while( second[k] != '\0' )
         k ++ ;
    
    //now, the allocation
    
    char* joint = new char[n+k+1]; //+1 for a '\0'
    
    //and for example memcpy for joining
    
    memcpy(joint, first, n );
    memcpy(joint+n, second, k+1); //also copying the null
    
    
    return joint;
    

    它告诉你用C的方式来做这件事:

    #include <cstring>
    
    char *cat (const char *s1, const char *s2)
    {
        // Learn to explore your library a bit, and
        // you'll see that there is no need for a loop
        // to determine the lengths. Anything C string
        // related is in <cstring>.
        //
        size_t len_s1 = std::strlen(s1);
        size_t len_s2 = std::strlen(s2);
        char *dst;
    
        // You have the lengths.
        // Now use `new` to allocate storage for dst.
    
    
        /*
         * There's a faster way to copy C strings
         * than looping, especially when you
         * know the lengths...
         *
         * Use a reference to determine what functions
         * in <cstring> COPY values.
         * Add code before the return statement to
         * do this, and you will have your answer.
         *
         * Note: remember that C strings are zero
         * terminated!
         */
    
        return dst;
    }
    
    #包括
    字符*cat(常量字符*s1,常量字符*s2)
    {
    //学习探索一下你的图书馆,然后
    //您将看到不需要循环
    //确定长度。任何C字符串
    //相关的是在。
    //
    尺寸长度s1=std::strlen(s1);
    尺寸长度s2=std::strlen(s2);
    char*dst;
    //你有足够的时间