【问题标题】:can I build a vector of vectors of structs with vectors of structs? (yes, really)我可以用结构向量构建结构向量的向量吗? (对真的)
【发布时间】:2010-12-04 06:43:35
【问题描述】:

我正在尝试构建一个相对复杂的数据结构(对我而言)。我的目标是从文本文档中读取单词并将单词和一些特定属性索引到哈希表中。该表由结构向量的向量构成:(vector vecName;)。我很幸运。每个唯一的词都被散列到向量中的一个索引位置。向量的第二个维度(结构向量)存储有关正在读取的文件以及在文件中找到单词的位置的信息。对于我读取的每个文件,如果我多次找到某个单词,则在结构中增加一个计数,并且带有整数的结构向量存储该单词存储在文件中的所有位置的信息。

我有两个项目需要帮助:

  1. 我很好奇是否有人提出了比我的建议更好的数据结构实现的建议。包含一些独立数据成员而不是这个庞然大物的类可能更有用吗?
  2. 看来我的语法错误导致编译错误,或者我只是尝试构建向量类不支持的结构。

这里是编译错误。所有三个错误都指结构内的结构向量:

“类 std::vector >”没有名为“theLoc”的成员
“类 std::vector >”没有名为“theStart”的成员
'class std::vector >' 没有名为 'theEnd' 的成员

如果我按照 EboMike 的建议调整代码,原来的错误就会消失,但我会得到:

我得到一个不同的错误,我不能发布因为编辑认为我发布了超链接。总结为:*'testProps.wordProps::theWordLoc:theLoc中对成员'push_back'的请求,属于非类类型'int'*

这是我的代码和我如何看待数据结构的图表(来自我的博客)的链接:

http://iamkevinfrye.com/blog/wp-content/uploads/2010/10/MicroSearch-Hash-Table-Data-Structure-Diagram.png

#include <vector>
#include <iterator>
#include <algorithm>
#include <iostream>

using namespace std;

struct wordLoc
{
    int theLoc;                     // the location of the word in theFile
    int theStart;                   // the beginning of the sentence
    int theEnd;                     // the end of the sentence
};

struct wordProps                    // stores word info to be placed in array
{
    string  theFile;                // stores the file where theWord is found
    int theCount;                   // increments with each occurence of theWord
    vector <wordLoc> theWordLoc;    // stores the wordLoc info for each occurence of theWord
};

int main()
{    
    int Tsize = 20000;

    wordProps testProps;
    testProps.theFile = "test1";
    testProps.theCount = 1;
    testProps.theWordLoc.theLoc.push_back(200);
    testProps.theWordLoc.theStart.push_back(1);
    testProps.theWordLoc.theEnd.push_back(15);

    vector < vector <wordProps> > theWordProps;
    theWordProps.resize(Tsize);

    theWordProps[0].push_back(testProps);

    cout << "index[0] = " << theWordProps[0].front().theFile << endl;
    cout << "index[0] = " << theWordProps[0].front().theCount << endl;
    cout << "index[0] = " << theWordProps[0].front().theWordLoc[0].theLoc << endl;
    cout << "index[0] = " << theWordProps[0].front().theWordLoc[0].theStart << endl;
    cout << "index[0] = " << theWordProps[0].front().theWordLoc[0].theEnd << endl;
    cout << "size of theWordProps[0] = " << theWordProps[0].size();

    cout << endl;
}

【问题讨论】:

  • “看来我的语法错误导致编译错误”...请发布错误消息的全文。

标签: c++ xcode data-structures vector struct


【解决方案1】:

首先是编译错误:您可能指的是这一行:

testProps.theWordLoc.theLoc.push_back(200);
testProps.theWordLoc.theStart.push_back(1);
testProps.theWordLoc.theEnd.push_back(15);

theWordLoc 是一个向量,因此您需要将其视为向量,例如:

testProps.theWordLoc[0].theLoc = 200;

或者,如果那里还没有:

wordLoc wordLocData;
worldLocData.theLoc = 200;
worldLocData.theStart = 1;
worldLocData.theEnd = 15;
testProps.theWorldLoc.push_back(worldLocData);

至于您的实际问题:这是一个可行的解决方案吗?是的。但是,您希望获得多少数据?它有多持久?如果答案是“吨,很长”,我会选择数据库。有一张用于 worldLoc 的表,一张用于 wordProps,一张用于更高级别的向量,事情变得更快更清晰。

另外,我不喜欢顶级向量。我不明白你打算在那里做的结构(我只是看了一眼图表),但听起来你正在寻找一个哈希图。

【讨论】:

  • 我编辑了带有错误的原始帖子。它们位于 EboMike 建议的行中。theWordLoc 是一个向量,我正在尝试使用不需要索引的 .push_back 方法。但是,我尝试添加索引并收到一组不同的错误。
  • 感谢您对使用数据库的评论。很诱人。对我来说不幸的是,这是数据结构研究项目的一部分,实际上我正在使用数据库,我只是从头开始构建它......但这一点仍然很好。我完全同意。
  • 你真的应该考虑一下。听起来这确实是您应该使用的。顺便说一句,将编辑有关编译错误的答案 - theLoc 不是向量,因此您不能在其上说“push_back”。
【解决方案2】:

除了将其设置为 hasmap 之外,我不知道数据结构的设计选择,但您的代码几乎是正确的!

查看我的 cmets:

int main()
{    
    int Tsize = 20000;

    wordProps testProps;
    testProps.theFile = "test1";
    testProps.theCount = 1;

    // create your wordLoc object
    wordLoc wl;
    wl.theLoc = 200;
    wl.theStart = 1;
    wl.theEnd = 15;

    // put it into the vector
    testProps.theWordLoc.push_back(wl);

    vector < vector <wordProps> > theWordProps;
    theWordProps.resize(Tsize);

    theWordProps[0].push_back(testProps);

    cout << "index[0] = " << theWordProps[0].front().theFile << endl;
    cout << "index[0] = " << theWordProps[0].front().theCount << endl;
    cout << "index[0] = " << theWordProps[0].front().theWordLoc[0].theLoc << endl;
    cout << "index[0] = " << theWordProps[0].front().theWordLoc[0].theStart << endl;
    cout << "index[0] = " << theWordProps[0].front().theWordLoc[0].theEnd << endl;
    cout << "size of theWordProps[0] = " << theWordProps[0].size();

    cout << endl;
}

【讨论】:

  • 谢谢维克!这是完全合理的,我在将 testProps 传递给 WordProps 时使用了相同类型的逻辑,但是那里有点模糊,我没有看到将 wl 传递给 wordProps 的相同模式。感谢您帮助这个小小的胜利! :) 现在我可以休息了!
【解决方案3】:

在testProps.theWordLoc.theLoc 中,您指的是向量theWordLoc 的theLoc 成员。这简直不能接受。你应该使用testProps.theWordLoc[0].theLoc之类的东西。

【讨论】:

  • 那么您是否建议在这种情况下,vector 类的 .push_back 方法是不可接受的,我需要提供一个索引来访问 theWordLoc 的成员?
  • @frieguy:如果你想push_back,请使用:testProps.theWordLoc.push_back({instance of wordLoc})。 theWordLoc 是一个向量。
  • @frieguy 你的逻辑是错误的。你的代码是testProps.theWordLoc.theLoc.push_back。这意味着“进入testProps,然后找到that 的theWordLoc 成员,然后找到that 的theLoc 成员,然后找到that 的push_back 成员b>”。此代码尝试在theWordLoc 中查找theLoc,但theWordLoc 是std::vector - 它没有theLoc。拥有theLoc 的东西是你想要push_back 的东西。你想把push_back 放到theWordLoc 上,所以你的代码应该看起来像theWordLoc.push_back( 这里的东西)。
【解决方案4】:

也许对于数据结构,multimap 可能是您的朋友,在这里替换了向量的顶部 lvl 向量。

http://www.cplusplus.com/reference/stl/multimap/

【讨论】:

  • 我可以使用散列函数将值存储在多重映射中吗?我正在寻找创建一个与 Big-O of 1 一样的表。
  • 从我发布的链接中您可以看到,当使用 multimap 模板时,如果我没有完全遗漏某些内容,您可以提供一个比较器作为参数,它可以包含您的哈希函数。插入应该类似于 O(log(n)) 并使用哈希 O(1) 查找。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-08
  • 1970-01-01
  • 1970-01-01
  • 2015-04-22
  • 1970-01-01
相关资源
最近更新 更多