首页 > 解决方案 > c ++通过结构向量的嵌套迭代器问题

问题描述

我是 C++ 新手,所以请原谅我的新手。

我正在尝试遍历结构的双端队列。在这些结构中的每一个中,都有一个结构成员,它本身就是不同结构的向量。我试图找到这些嵌套结构的唯一值,如果我发现嵌套结构项不是唯一的,我想从双端队列中删除父结构。我将每个父结构称为“拆分”。

这是我的结构:

// 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;

所以就像我说的,我想循环遍历有一堆 split_struct 的 deque all_splits_que。我那时。访问每个 split_struct 的“操作”成员的内容。我想看看这些动作序列在所有 split_struct 项中的所有“动作”成员中是否唯一,如果不是,则从其双端队列中删除 split_struct。

这是我试图达到这一点的初始代码:

// 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,这是记录的第一组错误。我不知道他们是什么意思。我究竟做错了什么?我尝试用“auto”替换迭代器 it2 的类型并得到相同的错误。谢谢。

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

标签: c++

解决方案


您必须operator==act_struct上定义,因为需要std::find比较两个std::vector<act_struct>

例子 :

#include <vector>
#include <deque>
#include <algorithm>

// 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;
    friend bool operator==(const act_struct & v1, const act_struct & v2) {
       return v1.csv_card_index == v2.csv_card_index; // <<< ADDED >>>
    }
};

// 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;

int main()
{
  
  // 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
  all_splits_que all_splits; // <<< ADDED >>>
  
  // 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; // better to have const ref
    
    // create a new iterator that checks if this_split_acts is already found in 
    // used_action_sequences 
    std::vector<std::vector<act_struct>>::iterator it2 = std::find(used_action_sequences.begin(), used_action_sequences.end(), this_split_acts);

  }
}

编译:

pi@raspberrypi:/tmp $ g++ -Wall c.cc
c.cc: In function ‘int main()’:
c.cc:49:52: warning: variable ‘it2’ set but not used [-Wunused-but-set-variable]
     std::vector<std::vector<act_struct>>::iterator it2 = std::find(used_action_sequences.begin(), used_action_sequences.end(), this_split_acts);
                                                    ^~~
pi@raspberrypi:/tmp $ 

可能act_structoperator==上的权利必须比较所有字段,我放了一个伪定义只是为了显示代码编译

正如我在评论中所说,我鼓励你更换

std::vector<act_struct> this_split_acts = it->actions;

经过

const std::vector<act_struct> & this_split_acts = it->actions;

并且可能

std::vector<std::vector<act_struct>>::iterator it2

 std::vector<std::vector<act_struct>>::const_iterator it2

推荐阅读