c++ - vector iterating over itself -
in project have vector wit relational data (a struct holds 2 similar objects represent relationship between them) , need check relationships combinations between data in vector.
what doing iterating on vector , inside first loop iterating again relationships between data.
this simplified model of doing
for(a=0; a<vec.size(); a++) { for(b=0; b<vec.size(); b++) { if(vec[a].something==vec[b].something) {...} } }
my collection has 2800 elements means iterating 2800*2800 times...
what kind of data structure more suitable kind of operation? using for_each faster traversing vector this?
thanks in advance!
vec has 2 structs made of 2 integers , nothing ordered.
no, for_each still same thing.
using hash map make problem better. start empty hash , iterate through list. each element, see if it's in hash. if it's not, add it. if is, have duplicate , run code.
in c++, can use std::map. in c, there no built in map datastructure, you'd have make own.
the high-level pseudo code this
foreach (element in array) if map.has_key(element) do_stuff(element) else map.add_key(element)
Comments
Post a Comment