views:

103

answers:

2

Simple question, how does one create a function which takes an unsigned char std::vector and spits out an unsigned char[] with a length. Thanks!

Ah, well it seems my problem was my knowledge of std::vector. I always believed that std::vector did not hold its values in linear fashion. That solves a lot of my problems. Thanks!

+5  A: 

Just use: &v.front().

If you need a copy use std::copy.

Brian R. Bondy
Billy ONeal
Brian R. Bondy
@Brian: The only reason I think it's more popular is that it's the syntax used in the standard.
Billy ONeal
+1  A: 

Unfortunately, it's not a one liner, but it's not too bad:

 std::vector<unsigned char> v(10);
 unsigned char* a = new unsigned char[v.size()];
 std::copy(v.begin(), v.end(), a);
 delete [] a;
Stephen
What's the purpose of basically taking the safety from it? Never use `new` where you have to explicitly delete it. In this case, you'd use a `vector` and...now you've just moved it from one vector to another.
GMan
@GMan - Dunno, the OP asked, maybe he needs to pass ownership to a legacy API (turns out, no... he just needed `v.front()`)
Stephen