Access to list items in C ++

I have a list of such lists:

std::list<std::list<double> > list; 

I filled it with some lists with doubles in them (actually quite a lot, so I don't use a vector. All this copying takes a lot of time.)

Let's say I want to access an element that can be added as list[3][3] if the list was not a list, but a vector or two-dimensional one. How can I do it?

I know that accessing items in a list is done using an iterator. I could not figure out how to get out of the double, though.

+6
source share
3 answers
 double item = *std::next(std::begin(*std::next(std::begin(list), 3)), 3); 

Using a vector usually has much better performance; access to list item n is O (n).

If you're worried about the performance of splicing the inside of a container, you can use deque , which has an operator[] , amortized constant insert and delete from either end, and linear temporary insert and delete from the inside.

For C ++ 03 compilers, you can implement begin and next yourself:

 template<typename Container> typename Container::iterator begin(Container &container) { return container.begin(); } template<typename Container> typename Container::const_iterator begin(const Container &container) { return container.begin(); } template<typename T, int n> T *begin(T (&array)[n]) { return &array[0]; } template<typename Iterator> Iterator next(Iterator it, typename std::iterator_traits<Iterator>::difference_type n = 1) { std::advance(it, n); return it; } 
+4
source

To answer your question, you should probably look at std::advance .

+1
source

To strictly answer your question, Joachim Pileborg's Answer is the way to go:

 std::list<std::list<double> >::iterator it = list.begin(); std::advance(it, 3); std::list<double>::iterator it2 = (*it).begin(); std::advance(it2, 3); double d = *it2; 

Now, from your question and further comments, it is not clear whether you always add items to the end of lists or whether you can add them anywhere. If you always add at the end, vector<double> will work better. A vector<T> does not need to be copied every time its size increases; only when its capacity increases, which is completely different.

In addition to this, using reserve() as mentioned earlier will help a lot in redistribution. You do not need to reserve the combined size of all vectors, but only for each individual vector. So:

 std::vector<std::vector<double> > v; v.reserve(512); // If you are inserting 400 vectors, with a little extra just in case 

And you will also reserve for each vector<double> inside v . It's all.

Please note that your list of lists will take up much more space. For each double in the internal list, it will have to select at least two additional pointers, as well as two additional pointers for each list in the global least. This means that the total memory occupied by your container will be approximately three times larger than that of the vector. And all this distribution and management also requires additional lead time.

+1
source

Source: https://habr.com/ru/post/924624/


All Articles