【问题标题】:How to store ASCII table in c++ [closed]如何在 C++ 中存储 ASCII 表 [关闭]
【发布时间】:2013-11-27 21:09:44
【问题描述】:

我在存储 ASCII 表时遇到了麻烦,我不想手动编写所有 255 个表,我需要将它们存储在哈希表中,以便根据 Ziv-Lempel 算法压缩字符串文件。那么您有什么建议吗?还有其他方法可以将它们存储在哈希表中吗?

谢谢。

已编辑:

HashTable::HashTable(){

char charToBeStored;
theList.resize(4096);
for(int i= 0; i<256; i++){
    charToBeStored = i;
    string stringToBeStored = charToBeStored; //Problem is here I also need to store
// strings beside char. I need to store them both 
    theList.push_back(charToBeStored); // Used <vectors> -> vector<string> theList;
  }
}

已解决(感谢您的回复,我认为这是一个非常基本的问题,对此感到抱歉。)

HashTable::HashTable(){
    unsigned char charToBeStored;
    theList.resize(4096);

    for(int i= 0; i<256; i++){
        charToBeStored = i;
        string stringToBeStored = "";
        stringToBeStored += charToBeStored;
        theList.push_back(stringToBeStored);
    }
}

【问题讨论】:

  • 我不确定您的确切意思。如果你 static_cast a charint 它会给你字符的 ASCII 值:static_cast&lt;int&gt;('a') 给出 97。

标签: c++ compression hashtable


【解决方案1】:

ASCII 只是 0 到 255,所以编写一个 for 循环,将无符号字符存储到 0 到 255 的哈希表中。

【讨论】:

  • 在你提到我这样做之后,但现在我必须在这个列表中存储字符串(我使用了向量)。
【解决方案2】:

要生成所有 ascii 字符:

char[256] asciichars;
for(int i = 0;i<256;++i)
{
    char[i] = static_cast<char>(i);
}

【讨论】:

  • 谢谢,我已将代码添加到问题中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-20
  • 2017-11-30
  • 1970-01-01
  • 1970-01-01
  • 2010-12-06
  • 1970-01-01
  • 2018-01-04
相关资源
最近更新 更多