Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keeping vector of iterators of the data

I have a function :

void get_good_items(const std::vector<T>& data,std::vector<XXX>& good_items);

This function should check all data and find items that satisfies a condition and return where they are in good_items.

what is best instead of std::vector<XXX>?

  1. std::vector<size_t> that contains all good indices.
  2. std::vector<T*> that contain a pointers to the items.
  3. std::vector<std::vector<T>::iterator> that contains iterators to the items.
  4. other ??

EDIT:

What will I do with the good_items? Many things... one of them is to delete them from the vector and save them in other place. maybe something else later

EDIT 2:

One of the most important for me is how will accessing the items in data will be fast depending on the struct of good_items?

EDIT 3:

I have just relized that my thought was wrong. Is not better to keep raw pointers(or smart) as items of the vector so I can keep the real values of the vector (which are pointers) and I do not afraid of heavy copy because they are just pointers?

like image 924
Humam Helfawi Avatar asked Nov 12 '15 16:11

Humam Helfawi


People also ask

What is iterator vector?

An iterator is used to move thru the elements an STL container (vector, list, set, map, ...) in a similar way to array indexes or pointers. The * operator dereferences an iterator (ie, is used to access the element an iterator points to) , and ++ (and -- for most iterators) increments to the next element.

How do you access vectors using iterators?

Vector's iterators are random access iterators which means they look and feel like plain pointers. You can access the nth element by adding n to the iterator returned from the container's begin() method, or you can use operator [] .

Can I store an iterator C++?

It should be no problem to store the iterators, just make sure you don't use them on a copy of the list -- an iterator is bound to one instance of the list, and cannot be used on a copy. As noted by others: Of course, you should also not invalidate the iterator by removing the pointed-to element. Save this answer.


2 Answers

If you remove items from the original vector, every one of the methods you listed will be a problem.

If you add items to the original vector, the second and third will be problematic. The first one won't be a problem if you use push_back to add items.

All of them will be fine if you don't modify the original vector.

Given that, I would recommend using std::vector<size_t>.

like image 100
R Sahu Avatar answered Sep 20 '22 15:09

R Sahu


I would go with std::vector<size_t> or std::vector<T*> because they are easier to type. Otherwise, those three vectors are pretty much equivalent, they all identify positions of elements.

std::vector<size_t> can be made to use a smaller type for indexes if you know the limits.

If you expect that there are going to be many elements in this vector, you may like to consider using boost::dynamic_bitset instead to save memory and increase CPU cache utilization. A bit per element, bit position being the index into the original vector.

like image 23
Maxim Egorushkin Avatar answered Sep 21 '22 15:09

Maxim Egorushkin