Solved

Convert vector<T> to T*

Posted on 2003-03-30
Medium Priority
171 Views
I need to pass the contents of an STL vector to a C function expecting an array. The way I am doing it is roughly as follows:

void func(int *a, int size) { ... }

vector<int> v;
...
func(v.begin().base(), v.size());

My question is, is this the standard way of doing this, ie do/should all implementations of iterator have a base() method returning a pointer? If not is there a more standard way? Despite my thinking that this would be a very common requirement I haven't seen anything on the web or in my books in terms of solutions, and for some reason vector doesn't seem to have the equivalent of string's c_str() method which basically does the same thing.
0
Question by:JLevick
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 6

Accepted Solution

GaryFx earned 150 total points
ID: 8236289
Jostuttis (The C++ Standard Library: A Tutorial and Reference) recommends using &v[0], which is certainly simpler and clearer.

I don't have the formal definition of base handy, but I think you're misunderstanding it.  Justuttis only describes it as being used to convert a reverse iterator into a forward iterator.  In other words, base() returns an iterator, not a pointer.

Finally, note that this won't work for vector<bool>.

Gary
0

LVL 30

Expert Comment

ID: 8236393
I agree with GaryFx, in that you should use &v[0] method.

If you want to use the begin() function method, then you should use it without adding .base() to it.
Example:
func(v.begin(), v.size());
0

LVL 1

Expert Comment

ID: 8238164
Here are 3 ways to get a pointer to the first item of an std::vector:
T* p = &*v.begin();
T* p = &v[0];
T* p = &v.front();
In the first option, many will find the &* cryptic.
The second option is perfectly ok, but I dislike the kind of redundant use of the zero index.
The third option is my preferred one.

Unfortunately, it is ILLEGAL to use ANY of these three expressions if the container is empty. It might work as you expect, but formally it leads to undefined behavior (i.e. "it could be erasing your hard disk").
So you must check v.empty() before attempting to get a pointer to the vector's first element.
0

LVL 1

Expert Comment

ID: 8238196
Woops, regarding the original question:
In the C++ standard, only reverse iterators have a "base()" member function, which returns the corresponding forward-iterator (caveat: dereferencing this iterator does not return the same item).
There is no such thing as a base() member in std::vector::iterator in standard C++.
0

LVL 30

Expert Comment

ID: 8239240
>>The third option is my preferred one.

FYI:
Any C++ STL book that referrs to this convertion, always shows the "&v[0]" method as the correct and prefered method to use.

>>T* p = &*v.begin();
This is redundant.  All you need is the v.begin().
Example:
T* p = v.begin();

Out of the methods posted, &v.front() would be the least preferred method.

0

Author Comment

ID: 8242805
The version of the STL I'm using (default with VS .NET) base() does return a pointer for both forward and reverse iterators and without using it (ie just v.begin()) the compiler complains that it can't convert between an iterator and a pointer. So it seems &v[0] is the best solution, thanks.
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses
Course of the Month15 days, 7 hours left to enroll