首页 > 解决方案 > strdup 用于将 const char* 转换为 char*

问题描述

我为 Huffman 树设计了用较短的 bin 代码转换二进制代码。在 main 中,如果您调用 Binary tree.init(q),那么该树将带有键:频率和值:bin 代码。问题是将 const char* 转换为 char*。我查看了一些代码,在这里我使用 strdup 对其进行了转换。有时工作正常,但有时不起作用。所以我检查了函数的参数。调用 strdup 或其他人有问题吗?

#pragma once
#include <stdio.h>
#include <queue>
#include <iostream>
#include "pch.h"
#include <string.h>
#include <string>
#define _CRT_SECURE_NO_WARNINGS

//this is a header file

using namespace std;


class Node
{
public:

     
    //key : frequency, value : code
    int f;
    char* code;
    Node* left;
    Node* right;



    int getFrequency()
    {
        return f;
    }

    char* getCode()
    {
        return code;
    }

    void init(int frequency, char* codestring)
    {
        f = frequency;
        code = codestring;

    }

    Node* getLeft() {
        return left;
    }

    Node* getRight()
    {
        return right;
    }

    void setLeft(Node* L)
    {
        left = L;
    }

    void setRight(Node* R)
    {
        right = R;
    }

    void setFrequency(int frequency)
    {
        f = frequency;
    }

    void setCode(char* string)
    {
        code = string;
    }

};


class BinaryTree
{
public:
    typedef priority_queue<int, vector<int>, greater<int>> pq;
    pq q;
    Node* proot;
    int sizeofqueue;
    void init(pq PriorityQueue)
    {
        q = PriorityQueue;
        sizeofqueue = q.size();

        N = 0;

        int comparetimes = q.size() - 1;
        for (int i = 0; i < comparetimes; i++)
        {
            if (i == 0)
            {
                put_first_two_nodes();
            }

            else
            {
                if (proot->getFrequency() <= q.top())
                {
                    put_right_node();
                }

                else if (proot->getFrequency() > q.top())
                {
                    put_left_node();
                }

                q.pop();
            }

        }

    }

    void put_first_two_nodes()
    {
        Node* pleft = new Node();
        (*pleft).setFrequency(q.top());
        (*pleft).setCode("0");
        q.pop();

        Node* pright = new Node();
        (*pright).setFrequency(q.top());
        (*pright).setCode("1");
        put(pleft, pright);
        q.pop();
    }

    void put_right_node()
    {
        Node* pright = new Node();
        pright->setFrequency(q.top());
        pright->setCode("1");
        put(proot, pright);

        appendcode(0);
    }
    void appendcode(int prefix)
    {
        string pre;
        if (prefix == 1) pre = "1";

        else pre = "0";
        Node* targetNode = proot->getRight();
        char* rcode = targetNode->getRight()->getCode();
        char* lcode = targetNode->getLeft()->getCode();

        string lefts = pre;
        string rights = pre;

        lefts.append(lcode);
        rights.append(rcode);

        char* leftstring = strdup(lefts.c_str());
        char* rightstring = strdup(rights.c_str());

        targetNode->getLeft()->setCode(leftstring);
        targetNode->getRight()->setCode(rightstring);

        free(leftstring);
        free(rightstring);
    }
    void put_left_node()
    {
        Node* pleft = new Node();
        pleft->setFrequency(q.top());
        pleft->setCode("0");
        put(pleft, proot);

        appendcode(1);
        
    }

    char* get(int k)
    {
        return getItem(*proot, k);
    }
    char* getItem(Node root, int k)
    {
        //if there's no node
        if (&root == nullptr) return "";

        //if f or root > k, search left sibling
        if (root.getFrequency() > k) return  getItem(*(root.getLeft()), k);

        //else, search right sibling
        else if (root.getFrequency() < k) return getItem(*(root.getRight()), k);

        //get it
        else return root.getCode();
    }

    

    void put(Node* left, Node* right)
    {
        put_item(left,right);
    }

    void put_item(Node* left, Node* right)
    {
        //make new node that has sibling with left and right
        Node* newnode = new Node();
        newnode->setLeft(left);
        newnode->setRight(right);

        //exchange the new node and root without losing data
        Node* temp;

        temp = proot;
        proot = newnode;
        newnode = temp;

        //proot's frequency : left f + right f
        (*proot).setFrequency((*left).getFrequency() + (*right).getFrequency());
        
    }

    void printpost()
    {
        postorder(proot);
    }
    void postorder(Node* root)
    {
        if (root != nullptr)
        {
            if (root->getLeft() != nullptr) postorder(root->getLeft());

            if (root->getRight() != nullptr) postorder(root->getRight());

            printf("%d : %s ",root->getFrequency(), root->getCode());
        }

        

    }

private:

    int N;
    Node root;
};


标签: c++binary-treehuffman-codestrdup

解决方案


你不应该在const char*中使用and (除非有时处理遗留或外部接口)。char*

切换您的代码以使用例如。std::stringstd::string_view)代替(string_view 需要更多的理解才能正确处理,并且可以说是 const - 所以我会坚持使用蝙蝠)。std::string在需要时通过引用或 const 引用传递。std::string对于大多数程序来说,开销可以忽略不计。


推荐阅读