I'm new to C++ so forgive my newbishness.
I'm trying to loop through a deque of structs. in each of these structs, there's a struct member that is itself a vector of a different struct. I'm trying to find unique values of these nested structs, and if i find a nested struct item is not unique, i want to delete the parent struct from the deque. I refer to each parent struct as a "split".
Here are my structures:
// 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;
So like I said, i want to loop through the deque all_splits_que, which has a bunch of split_struct. I then. what to access each split_struct's "actions" member. I want to see if those sequence of actions are unique among all of the "actions" members in all split_struct items, and if not, delete the split_struct from its deque.
Here is my initial code that attempts to get to that point:
// 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);
}
This is as far as i can get, as it doesnt seem to like me creating a second iterator. I use gcc in Geany, this is the first group of errors logged. I don't know what they mean. What am I doing wrong? I tried replacing the type of iterator it2 with "auto" and get the same errors. thanks.
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
You have to define operator== on act_struct because it is needed by std::find to compare two std::vector<act_struct>
Example :
#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);
}
}
Compilation :
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 $
Probably the right operator== on act_struct has to compare all the fields, I put a pseudo definition just to show the code compile
As I said in a remark I encourage you to replace
std::vector<act_struct> this_split_acts = it->actions;
by
const std::vector<act_struct> & this_split_acts = it->actions;
and probably
std::vector<std::vector<act_struct>>::iterator it2
can be
std::vector<std::vector<act_struct>>::const_iterator it2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With