【问题标题】:Printing an array of STL list打印 STL 列表数组
【发布时间】:2014-03-22 03:12:57
【问题描述】:

我在打印列表数组时遇到了一些问题(例如list<string> hashTable[100]

这就是我目前所拥有的,我的问题在底部:

hash2.h

1 #ifndef __HASH2_H
2 #define __HASH2_H
3
4 #include<string>
5 #include<list>
6 #include<fstream>
7
8 using namespace std;
9
10 class Hash
11 {
12  public:
13    void processFile(string fileName);
14    void print();
15
16  private:
17   list<string> hashTable[100];
18
19  private:
20   int hf(string ins);
21
22
23 };
24
25 #endif

hash_function2.cpp

 1 #include<iostream>
 2 #include<string>
 3 #include<fstream>
 4 #include "hash2.h"
 5
 6 using namespace std;
 7
 8 void Hash::processFile(string fileName)
 9 {
 10  string word;
 11  ifstream myIfile(fileName.c_str());
 12  while(getline(myIfile, word))
 13  {
 14   int index = hf(word);
 15   hashTable[index].push_back(word);
 16  }
 17  myIfile.close();
 18 }
 19
 20 void Hash::print()
 21 {
 22  for(int i = 0; i < 100; i++)
 23  {
 24   if(hashTable[i] != NULL)
 25   {
 26    list<int>::iterator i;
 27    for(i = hashTable[i].begin(); i != hashTable[i].end(); ++i)
 28    {
 29     cout << *i << endl;
 30    }
 31   }
 32  }
 33 }
 34
 35 int Hash::hf(string ins)
 36 {
 37  return ( (int) ins[0] ) % 100;
 38 }

main2.cpp

  1 #include "hash2.h"
  2 #include<iostream>
  3 #include<iomanip>
  4 #include<fstream>
  5
  6 using namespace std;
  7
  8 int main()
  9 {
  10  Hash hashTable;
  11  hashTable.processFile("test1.txt");
  12  hashTable.print();
  13 }

所以,我现在拥有的是 processFile 函数,它获取文本文件,读取每个单词,执行散列函数(糟糕,我知道),然后将该单词放入散列函数返回的数组索引中。我认为这很好用。

我遇到的问题本质上是使用 STL 列表。所以,在 hash_function2.cpp 中,我想打印我的哈希表。我不确定如何检查数组索引是否为空(没有我存储的任何单词),我也不确定如何在给定数组索引处打印列表中的所有字符串。基本上我想做的只是打印我的哈希表;让我的 print() 函数工作。

如果有人能指出我正确的方向,那就太棒了!将不胜感激。

【问题讨论】:

标签: c++ arrays list stl hashtable


【解决方案1】:

您不应该检查 NULL,因为您有一个 std::list 对象数组,而不是指针。

您可以检查索引i 处的列表是否包含以下元素:

if (!hashTable[i].empty())
{
    // there are elements in the list
}

或者

if (hashTable[i].size())
{
    // there are elements in the list
}

同样在您的print() 函数中,您正在使用std::list&lt;int&gt; 类型的迭代器,但它应该是std::list&lt;string&gt;,它与hashTable 的声明相匹配。

【讨论】:

    【解决方案2】:

    您的问题不在于 std 列表,而在于数组。

    您可以尝试使用std::array

    #include <array>
    
    std::array< std::list<string>, 100> hashTable;
    

    这样你可以检查它是否为空

    if (!hashTable.empty())
    {
      // do stuff
    }
    

    你可以在里面查看每个列表

    if(!hashTable[i].empty())
    {
      // do even more stuff
    }
    

    【讨论】:

      【解决方案3】:

      太棒了,非常感谢大家!我了解如何检查每个数组索引并遍历列表!这是我所做的:

      1 #include "hash2.h"
      2
      3 #include<iostream>
      4 #include<string>
      5 #include<fstream>
      6
      7 using namespace std;
      8
      9 void Hash::processFile(string fileName)
      10 {
      11  string word;
      12  ifstream myIfile(fileName.c_str());
      13  while(getline(myIfile, word))
      14  {
      15   int index = hf(word);
      16   hashTable[index].push_back(word);
      17  }
      18  myIfile.close();
      19 }
      20
      21 void Hash::print()
      22 {
      23  for(int i = 0; i < 100; i++)
      24  {
      25   if(hashTable[i].empty() != true)
      26   {
      27    list<string>::iterator r;
      28    for(r = hashTable[i].begin(); r != hashTable[i].end(); ++r)
      29    {
      30     cout << *r << endl;
      31    }
      32   }
      33  }
      34 }
      35
      36 int Hash::hf(string ins)
      37 {
      38  return ( (int) ins[0] ) % 100;
      39 }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-05
        • 2022-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多