简体   繁体   中英

Get pointer to raw data in set like &(vector[0])

To get the pointer to the data in a vector we can use

vector<double> Vec;    
double* Array_Pointer = &(Vec[0]);
Function(Array_Pointer);

Is that possible to get the pointer to the data in a set ? Can I use that as array pointer like above?


If not possible, what is the best way to make a vector out of set? I mean without loop over all elements.

No, this is not necessarily possible. The C++ ISO standard explicitly guarantees contiguous storage of elements in a std::vector , so you can safely take the address of the first element and then use that pointer as if you were pointing at a raw array. Other containers in the standard library do not have this guarantee.

The reason for this is to efficiently support most operations on a std::set , the implementation needs to use complex data structures like balanced binary search trees to store and organize the data. These structures are inherently nonlinear and require nodes to be allocated and linked together. Efficiently getting this to work with the elements in a flat array would be difficult, if not impossible, in the time constraints laid out by the standard (amortized O(log n) for most operations.)

EDIT : In response to your question - there is no way to build a std::vector from a std::set without some code somewhere iterating over the set and copying the elements over. You can do this without explicitly using any loops yourself by using the std::vector range constructor:

std::vector<T> vec(mySet.begin(), mySet.end());

Hope this helps!

No. It's not possible to implement set in such a way that you can do this.

If you implement set in such a way that elements are stored in a single array, then when you add more elements, that array will inevitably need to be reallocated at some point. At that time, any references to existing elements will be invalidated.

One of features of set is that it guarantees that references to elements will never be invalidated if you add (or remove) other elements. As stated in [associative.reqmts]:

The insert and emplace members shall not affect the validity of iterators and references to the container, and the erase members shall invalidate only iterators and references to the erased elements.

So it's impossible to implement set in such a way that all of the elements of the set are stored in a single array.

Note that this has nothing to do with the efficiency requirements such as O(log n) insert/delete/lookup (if you squint really hard and allow for amortized O(log n) insertion time, at least), or maintaining sorted order, or anything like that. If it was just these, they could easily be handled with a data structure on top of the underlying elements, and the elements themselves could be stored in an array. It also doesn't even have anything to do with guarantees about iterator invalidation, since iterators are abstract.

No, the only thing holding you back is the reference invalidation requirement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM