我是C ++的初学者,请原谅我的新手问题。
我试图遍历一个结构体deque
。在每个这样的结构体中,都有一个本身是另一种结构体的向量结构体成员。我试图找到这些嵌套结构体的唯一值,并且如果我发现嵌套结构体项目不唯一,则要从deque
删除父结构体。我将每个父结构体称为“拆分”。
这是我的结构:
// struct for each action inside each "split".
struct act_struct {
short int csv_card_index;
short int act_type;
short int act_mod1;
short int act_mod2;
};
// struct for each individual split, the parent struct
struct split_struct {
std::vector<short int> cards_in_hand;
std::vector<short int> cards_drawn;
std::vector<short int> cards_in_deck;
std::vector<short int> cards_bf;
std::vector<short int> cards_mana_disabled;
std::vector<act_struct> actions; <--- this is where the act_struct gets nested
bool played_a_land;
};
// deque holding different split_structs in sequence
typedef std::deque<split_struct> all_splits_que;
就像我之前说过的,我希望遍历deque all_splits_que,它包含了一堆split_struct。然后我想访问每个split_struct的"actions"成员。我想查看所有split_struct项中"actions"成员的序列是否是唯一的,如果不是,则将该split_struct从其deque中删除。
以下是我的初步代码尝试:
// this is a vector where I'll track action vectors i've already encountered, so I can tell
// if the current action vector is unique. if the current action vector is unique, it will get push
// onto this
std::vector<std::vector<act_struct>> used_action_sequences;
// all_splits is an all_splits_que with several split_struct in sequence
// first i create an iterator to loop through all_splits
auto it = all_splits.begin();
while ( it != all_splits.end() ) {
// access the actions struct for this split_struct
std::vector<act_struct> this_split_acts = it->actions;
// create a new iterator that checks if this_split_acts is already found in
// used_action_sequences (ERROR THROWN ON THIS LINE)
std::vector<std::vector<act_struct>>::iterator it2 = std::find(used_action_sequences.begin(), used_action_sequences.end(), this_split_acts);
}
就我来说,这是我所能做到的了,因为它似乎不喜欢我创建第二个迭代器。我在Geany中使用gcc,这是记录的第一组错误。我不知道它们是什么意思。我做错了什么?我尝试将迭代器it2的类型替换为"auto",但是得到了相同的错误。谢谢。
In file included from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/9.2.0/include/c++/bits/char_traits.h:39,
from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/9.2.0/include/c++/ios:40,
from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/9.2.0/include/c++/ostream:38,
from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/9.2.0/include/c++/iostream:39,
from main.cpp:3:
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/9.2.0/include/c++/bits/stl_algobase.h: In instantiation of 'static bool std::__equal<_BoolType>::equal(_II1, _II1, _II2) [with _II1 = const act_struct*; _II2 = const act_struct*; bool _BoolType = false]':
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/9.2.0/include/c++/bits/stl_algobase.h:851:43: required from 'bool std::__equal_aux(_II1, _II1, _II2) [with _II1 = const act_struct*; _II2 = const act_struct*]'
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/9.2.0/include/c++/bits/stl_algobase.h:1069:30: required from 'bool std::equal(_II1, _II1, _II2) [with _II1 = __gnu_cxx::__normal_iterator<const act_struct*, std::vector<act_struct> >; _II2 = __gnu_cxx::__normal_iterator<const act_struct*, std::vector<act_struct> >]'
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/9.2.0/include/c++/bits/stl_vector.h:1890:21: required from 'bool std::operator==(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&) [with _Tp = act_struct; _Alloc = std::allocator<act_struct>]'
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/9.2.0/include/c++/bits/predefined_ops.h:241:17: required from 'bool __gnu_cxx::__ops::_Iter_equals_val<_Value>::operator()(_Iterator) [with _Iterator = __gnu_cxx::__normal_iterator<std::vector<act_struct>*, std::vector<std::vector<act_struct> > >; _Value = const std::vector<act_struct>]'
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/9.2.0/include/c++/bits/stl_algo.h:120:14: required from '_RandomAccessIterator std::__find_if(_RandomAccessIterator, _RandomAccessIterator, _Predicate, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<std::vector<act_struct>*, std::vector<std::vector<act_struct> > >; _Predicate = __gnu_cxx::__ops::_Iter_equals_val<const std::vector<act_struct> >]'
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/9.2.0/include/c++/bits/stl_algo.h:161:23: required from '_Iterator std::__find_if(_Iterator, _Iterator, _Predicate) [with _Iterator = __gnu_cxx::__normal_iterator<std::vector<act_struct>*, std::vector<std::vector<act_struct> > >; _Predicate = __gnu_cxx::__ops::_Iter_equals_val<const std::vector<act_struct> >]'
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/9.2.0/include/c++/bits/stl_algo.h:3899:28: required from '_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = __gnu_cxx::__normal_iterator<std::vector<act_struct>*, std::vector<std::vector<act_struct> > >; _Tp = std::vector<act_struct>]'
main.cpp:296:145: required from here
all_splits
的定义。为什么要使用std::vector<act_struct> this_split_acts = it->actions;
而不是const std::vector<act_struct> & this_split_acts = it->actions;
?我的意思是至少使用引用而不是复制向量,并且在您不使用修改它时最好也有const
。 - brunostd::vector<act_struct>
对象。 - kesarling He-Him