【问题标题】:How to fill a vector of lists?如何填充列表向量?
【发布时间】:2013-10-22 09:12:30
【问题描述】:

我正在尝试将 ascii 代码和名称放入列表向量中:理想情况下它会是这样的:

97:“真棒”、“全部”

98:“最佳”、“轰隆”、“炸弹”

99:“猫”

我有

class index_table
{
public:
   index_table() { table.resize(128);}
   void insert(string &, int);
private:
   class entry {           //Subclass
      string word;
      vector <int> line;
    }
   vector< list <entry > > table;

那么我怎样才能正确地将这些单词和ASCII码放入列表的向量中呢?

在 main 中我尝试了一些语法,但它不起作用:

void index_table :: insert ( string & word, int num) //This is the code for "cat" and "99"
{
    entry obj;
    //This is the part I'm not sure about. How do I enter each word and num into the vector < list < entry >> table

}

希望我说得足够清楚。总而言之,我对 vector > 表的工作方式感到困惑。或者更确切地说,我如何才能正确存储我的号码和单词?

【问题讨论】:

  • 为什么不简单地std::map&lt;int, std::vector&lt;std::string&gt; &gt;
  • @P0W:我认为key -> entry的原因,其中entry包含单词+行号是因为他正在研究某种搜索索引。
  • @LihO idk,但 vector&lt;int&gt; for line 对于给定的示例没有意义

标签: c++ list vector


【解决方案1】:

您正在寻找包含以下内容的数据结构:
ID ~> entry 对象列表
ID ~> 另一个列表...

然而,以下类型的table 是错误的决定:

vector< list <entry > > table;

如果这些数字确实是唯一的,使用std::map 会更明智:

std::map<int, std::list<entry> > table;

或者在 C++11 中甚至 std::unordered_map

【讨论】:

  • 它们是独一无二的。那么,我该如何调用vector > table呢?
猜你喜欢
  • 2018-11-07
  • 1970-01-01
  • 2021-02-13
  • 2021-06-06
  • 1970-01-01
  • 2013-09-08
  • 1970-01-01
  • 2019-11-09
  • 1970-01-01
相关资源
最近更新 更多