我有一些布尔表达式要评估和处理。也许使用 Boost 会更好,但我仍在学习 STL,并没有那样做。我现在正在学习迭代器验证,或者视情况而定。有没有办法安全地将新元素插入到下面的这个嵌套向量中?如果您不想看到一个成年人哭泣,请不要建议我重写所有内容:) 说真的,我也欢迎在解决我更直接的问题后如何以更优雅的方式重写它的建议,我怀疑这是一个无效的迭代器......
...我并不十分关心性能。基于此并阅读其他帖子,也许 a std::List
in place ofstd::vector
会更好,但我是否需要在嵌套的每个级别?
----nested.h
#include <vector>
struct Term {
uint32_t termNumber;
std::string content;
uint32_t importance;
uint32_t numAppearances;
uint32_t ContextFlags;
};
struct SubClause {
std::string typeName;
std::vector<Term> terms;
std::string clauseExpression;
};
struct Clause {
std::vector<SubClause> subClauses;
};
-----nested.cpp
#include <iostream>
#include "nested_container.h"
int main (int argc, char * const argv[]) {
std::vector< Clause > expression;
std::vector< Clause >::iterator clauseIter = expression.begin();
std::vector< Clause >::iterator clauseEnd = expression.end();
for( ; clauseIter != clauseEnd ; clauseIter++ )
{
std::vector< SubClause >::iterator subIter = clauseIter->subClauses.begin();
std::vector< SubClause >::iterator subEnd = clauseIter->subClauses.end();
for( ; subIter != subEnd ; subIter++ )
{
std::vector< Term >::iterator termIter = subIter->terms.begin();
std::vector< Term >::iterator termEnd = subIter->terms.end();
for( ; termIter != termEnd ; termIter++ )
{
/* Evaluate SubClause Terms based on some criteria
*/
/* if criteria true */
if( true/* criteria true? */ )
{
Term newTerm = { };
/* fillOutTerm(newTerm) */
/* Invalidates the subIter pointer, pretty sure. Anything else???? */
subIter->terms.push_back( newTerm ); //BAD?
}
}
}
}
return 0;
}