Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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_Formatting_Char - Fatal编程技术网

C++ 与';字符*';类型-C++;

C++ 与';字符*';类型-C++;,c++,arrays,formatting,char,C++,Arrays,Formatting,Char,我最近创建了一个用于表处理的类,它基本上由用于输出格式化的函数组成,这些函数返回一个char*变量。我创建了一个演示程序来检查它的功能并解决任何类型的问题。现在我陷入了一个问题。以下是程序输出: ┌───────┬───────┬───────┐ <-- this series of chars is the top part of the table │TYPE │ADDRESS│VALUE │ ├───────┼───────┼───────┤ │N/A │N/A │

我最近创建了一个用于表处理的类,它基本上由用于输出格式化的函数组成,这些函数返回一个
char*
变量。我创建了一个演示程序来检查它的功能并解决任何类型的问题。现在我陷入了一个问题。以下是程序输出:

┌───────┬───────┬───────┐ <-- this series of chars is the top part of the table
│TYPE   │ADDRESS│VALUE  │
├───────┼───────┼───────┤
│N/A    │N/A    │N/A    │
├───────┼───────┼───────┤
│N/A    │N/A    │N/A    │
├───────┼───────┼───────┤
│N/A    │N/A    │N/A    │
└───────┴───────┴───────┘

┌──────────────────────────────────────────────────
──────────────────────────────────────────────────────────── <-- top part gets veeeeeeery long!
│TYPE   │ADDRESS│VALUE  │
├───────┼───────┼───────┤
│N/A    │N/A    │N/A    │
├───────┼───────┼───────┤
│N/A    │N/A    │N/A    │
├───────┼───────┼───────┤
│N/A    │N/A    │A      │<-- in this cell is contained a char* variable value. i think this is the
└───────┴───────┴───────┘ problem

┌───────┬───────┬───────┐ 

混合C和C++,从而打破了基本的编程规则:一致性。

 /* TABLE HEADER */

#ifdef __cplusplus  // useless and dangerous definition !!!!
#define NUL '\0'    // NULL is the correct keyword in C++98, defined in stdlib.h
#else               // std::nullptr_t in c++11 onwards, defined in <cstddefs> 
#define NUL "\0"
#endif

// NEVER, EVER define macros before including library headers !!!!!

#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>

class basic_table
{
private:
    std::vector<std::string> cont_m; //Table content
                                     // Your table is only 1-D ???
    int cols_m, rows_m;              // useless repetition of table row count.
                                     // let std::vector handle that
                                     // this avoids bugs.

    // correct data definition. 
    std::vector<std::vector<std::string>> cont_m;  // 2D table. dimension:[row, column]

   
    int getRecord() //Get the longest string  // NAME is misleading.
    std::vector<size_t> getMaxCellContentsWidths() // renamed to something more appropriate
                                           // and useable result type           
    {
        int record;
        for (int P = 0, c = 0; P < cont_m.size(); P++) // reserve uppercase names for constants !!!! 
        {
            while (cont_m[P][c] != NUL) c++;
            record = (record > c ? record : c);
            c = 0;
        }
        return record;

        // correct code:

        // returns the maximum length of cell column contents, 
        // does not include padding !!!
        std::vector<size_t> result;
        if (cont_m.empty())
            return result;

        // first table row defines table column count  
        const size_t COL_COUNT = cont_m[0].size();
        result.resize(COL_COUNT);

        for (size_t row = 0; row < cont_m.size(); ++row)
        {
            for (size_t col = 0; col < std::min(COL_COUNT, cont_m[row].size()); ++col)
            {
                // this test is needed if you want to print N/A for empty strings
                size_t w = cont_m[row][col].length();
                if (w == 0) w = 3;
                result[col] = std::max(result[col], w);
            }
        }
        return result;
    }
    
    // your table is 1-D ???
    int getLength(int pos) //Localize array position and return string length

    size_t getItemLength(size_t row, size_t col) 
    {
        // your code, below is 1980's C style programming for embedded applications
        // with nbo access to the C library.  NEVER do this if you can avoid
        // it. 
        int c = 0;
        while (cont_m[pos][c] != NUL) c++;
        return c;

        // correct code:
        if (row >= cont_m.size() || col >= cont_m[row].size())
            return 0;
        return cont_m[row][col].length();
    }
    
    // this bady named funtion is not needed to print a table.
    // and it's leaking memory.
    char* complete(int pos) //Set all string to the same length
    {
        int size = getRecord() - getLength(pos) + 1;
        char* spaces = new char[size];  
        for (int P = 0; P < size; P++)
        {
            spaces[P] = ' ';
        }
        spaces[size - 1] = '\0';
        return spaces;
        // why don't you return a std::string ???
    }

    /* ... */
};
/*表格标题*/
#ifdef\uu cplusplus//无用和危险的定义!!!!
#define num'\0'//NULL是C++98中正确的关键字,在stdlib.h中定义
#else//std::nullptr\t在c++11及以后的版本中,在中定义
#定义NUL“\0”
#恩迪夫
//永远不要在包含库标题之前定义宏!!!!!
#包括
#包括
#包括
#包括
类基本表
{
私人:
std::vector cont_m;//表内容
//你的桌子只有1-D???
int cols\u m,rows\u m;//表行计数的无用重复。
//让std::vector来处理这个问题
//这样可以避免bug。
//正确的数据定义。
std::vector cont_m;//二维表。维度:[行,列]
int getRecord()//获取最长字符串//名称有误导性。
std::vector getMaxCellContentsWidths()//重命名为更合适的名称
//和可用的结果类型
{
int记录;
for(int P=0,c=0;Pc?记录:c);
c=0;
}
返回记录;
//正确代码:
//返回单元格列内容的最大长度,
//不包括填充!!!
std::向量结果;
if(cont_m.empty())
返回结果;
//第一个表行定义表列计数
const size\u t COL\u COUNT=cont\u m[0]。size();
结果。调整大小(列数);
对于(行大小=0;行<连续大小();++行)
{
对于(size_t col=0;col=cont|m.size()|列>=cont|m[row].size())
返回0;
返回cont_m[行][col].length();
}
//打印表时不需要此名为funtion的命令。
//它正在泄漏内存。
char*complete(int-pos)//将所有字符串设置为相同的长度
{
int size=getRecord()-getLength(pos)+1;
字符*空格=新字符[大小];
对于(int P=0;P
不幸的是,代码的其余部分无法修复,因为它假定存储为1-D表。。。也就是说,只有一列的表。你的错误来自于糟糕的原始设计。不要绝望,它会发生的


<>提示改写:不要使用原始字符指针,不要调用新的,使用上面定义的函数的单元格内容宽度,使用C++构造,以及STD库结构和类。< /P>你在一个代码< char < /C>……上做了<代码> StLLIN <代码>……C是怎样的?C没有类。您标记为C语言。C语言没有
std::vector
。顶部的条件编译毫无价值,因为您的代码不会编译为C语言。请根据需要编辑语言标记。查找
std::string::length()
std::string::size()
。您可以通过使用现有功能来删除函数。我强烈建议拆分文件。将
定义和方法声明放在头文件中。将函数定义(实现)放入源文件中。按原样,包括头文件在内的任何源文件都必须编译所有这些函数。通过将内容放入源文件,所有这些函数都将被编译一次。
 /* TABLE HEADER */

#ifdef __cplusplus  // useless and dangerous definition !!!!
#define NUL '\0'    // NULL is the correct keyword in C++98, defined in stdlib.h
#else               // std::nullptr_t in c++11 onwards, defined in <cstddefs> 
#define NUL "\0"
#endif

// NEVER, EVER define macros before including library headers !!!!!

#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>

class basic_table
{
private:
    std::vector<std::string> cont_m; //Table content
                                     // Your table is only 1-D ???
    int cols_m, rows_m;              // useless repetition of table row count.
                                     // let std::vector handle that
                                     // this avoids bugs.

    // correct data definition. 
    std::vector<std::vector<std::string>> cont_m;  // 2D table. dimension:[row, column]

   
    int getRecord() //Get the longest string  // NAME is misleading.
    std::vector<size_t> getMaxCellContentsWidths() // renamed to something more appropriate
                                           // and useable result type           
    {
        int record;
        for (int P = 0, c = 0; P < cont_m.size(); P++) // reserve uppercase names for constants !!!! 
        {
            while (cont_m[P][c] != NUL) c++;
            record = (record > c ? record : c);
            c = 0;
        }
        return record;

        // correct code:

        // returns the maximum length of cell column contents, 
        // does not include padding !!!
        std::vector<size_t> result;
        if (cont_m.empty())
            return result;

        // first table row defines table column count  
        const size_t COL_COUNT = cont_m[0].size();
        result.resize(COL_COUNT);

        for (size_t row = 0; row < cont_m.size(); ++row)
        {
            for (size_t col = 0; col < std::min(COL_COUNT, cont_m[row].size()); ++col)
            {
                // this test is needed if you want to print N/A for empty strings
                size_t w = cont_m[row][col].length();
                if (w == 0) w = 3;
                result[col] = std::max(result[col], w);
            }
        }
        return result;
    }
    
    // your table is 1-D ???
    int getLength(int pos) //Localize array position and return string length

    size_t getItemLength(size_t row, size_t col) 
    {
        // your code, below is 1980's C style programming for embedded applications
        // with nbo access to the C library.  NEVER do this if you can avoid
        // it. 
        int c = 0;
        while (cont_m[pos][c] != NUL) c++;
        return c;

        // correct code:
        if (row >= cont_m.size() || col >= cont_m[row].size())
            return 0;
        return cont_m[row][col].length();
    }
    
    // this bady named funtion is not needed to print a table.
    // and it's leaking memory.
    char* complete(int pos) //Set all string to the same length
    {
        int size = getRecord() - getLength(pos) + 1;
        char* spaces = new char[size];  
        for (int P = 0; P < size; P++)
        {
            spaces[P] = ' ';
        }
        spaces[size - 1] = '\0';
        return spaces;
        // why don't you return a std::string ???
    }

    /* ... */
};