简体   繁体   English

如何遍历嵌套向量?

[英]How do I iterate through nested vectors?

I have a rather complex ordered internal Grammar representation of my productions consisting of typed std libarary classes:我的作品有一个相当复杂的有序内部语法表示,由类型化的 std 库类组成:

class Grammar {
    std::tuple<int, NonTerminal, std::vector<std::vector<Symbol>>> productions;
}

Now I would like to create LR0-Items from this representation.现在我想从这个表示创建 LR0-Items。

From inner to outer:从内到外:

  • std::vector<Symbol> represents a rule std::vector<Symbol>表示规则
  • std::vector<std::vector<Symbol>> represents an alternative rule of the production. std::vector<std::vector<Symbol>>表示产生式的替代规则。
  • NonTerminal, std::vector<std::vector<Symbol>>> represents the lhs of the production NonTerminal, std::vector<std::vector<Symbol>>>代表产生式的lhs
  • std::tuple<int, NonTerminal, std::vector<std::vector<Symbol>>> represents the order of the productions. std::tuple<int, NonTerminal, std::vector<std::vector<Symbol>>>表示产生式的顺序。

Question: How do I iterate through the productions?问题:如何迭代产品? I am new to c++.我是 C++ 的新手。 As far as I know the way is through an ::iterator .据我所知,方法是通过::iterator But then, how do I address one nested datatype to build an iterator from it?但是,我如何解决一种嵌套数据类型以从中构建迭代器?

Thanks谢谢

EDIT:编辑:

Instead of std::tuple I think it's yet better to have the definition consist of two times a std::pair :而不是std::tuple我认为让定义包含两次std::pair更好:

std::pair<int, std::pair<NonTerminal, std::vector<std::vector<Symbol>>> productions;

I try to adopt the answer of user idclev 463035818我尝试采用用户idclev 463035818的答案

for( const &auto production : productions.back) {
  for( const &auto alternatives : production.back) {
    for( const &auto rule : alternatives) {
     // ...
    }
  }
}

Correct ?正确的 ?

To iterate a vector whose elements are of type T you can use a range based for loop:要迭代元素类型为T的向量,您可以使用基于范围的 for 循环:

std::vector<T> vect;
for (const auto& element : vect) {
    // element is a const reference to elements of vect
    // put code here
}

Now, if T is again a vector, you just replace //put code here with your code to iterate that vector element .现在,如果T再次是一个向量,您只需将//put code here替换为您的代码以迭代该向量element


Nesting containers is not as complicated as you might expect.嵌套容器并不像您想象的那么复杂。 It does not require special treatment.它不需要特殊处理。 If you know how to iterate one vector and get a reference to its elements, then you already do know how to iterate those elements in case they are vectors.如果您知道如何迭代一个向量并获得对其元素的引用,那么您就已经知道如何迭代这些元素,以防它们是向量。


So lets say you have a std::vector<std::vector<Symbol>>> (I'll skip the tuple, because I suppose you don't want to "iterate" it, but just pick the 3rd element from it).因此,假设您有一个std::vector<std::vector<Symbol>>> (我将跳过元组,因为我想您不想“迭代”它,而只需从中选择第三个元素)。

std::vector<std::vector<Symbol>>> vect_vect;

for (const auto& vect : vect_vect) {
    // here vect is a const reference to std::vector<Symbol>
    for (const auto& symbol : vect) {
       // here symbol is a const reference to a Symbol
       // put your code here
       // eg assuming Symbol has a member get_bar
       auto foo = symbol.get_bar();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM