0

I would like to know if there is an easy solution for casting an array to a variably sized vector.

double CApp::GetTargetCost(const vector<unsigned char> &uHalfphoneFeatureVector_Bytes,const vector<unsigned char> &uTargetFeatureVector_Bytes)

I would like to pass

struct udtByteFeatures
{
    unsigned char Values[52];
};

to this function, but C++ does not like the fact that it has a fixed size. It expects a variably sized vector.

The error I am getting is

error C2664: 'CApp::GetTargetCost': Conversion of parameter 1 from 'unsigned char [52]' to 'const std::vector<_Ty> &' not possible

I am not sure yet whether I will use a fixed size later on or not, but currently I simply would like to stay flexible.

Thank you!

Adam Haun
  • 359
  • 7
  • 13
tmighty
  • 10,734
  • 21
  • 104
  • 218
  • 5
    What you are calling a 'fixed sized vector' has a name - `array` – Patashu Jun 19 '13 at 06:07
  • Btw, the code on this page shows how you can make a vector out of an array: http://www.cplusplus.com/reference/vector/vector/vector/ – Patashu Jun 19 '13 at 06:10
  • 2
    [if anything, then maybe cppreference.com.](http://en.cppreference.com/w/cpp/container/vector) –  Jun 19 '13 at 06:13
  • 1
    @H2CO3 > "No cplusplus.com please!" But why? O_O – Ivan Aksamentov - Drop Jun 19 '13 at 06:21
  • 2
    @H2CO3: You might want to explain yourself. – SigTerm Jun 19 '13 at 06:26
  • @SigTerm unneeded, there's a thingy called Google, isn't there. –  Jun 19 '13 at 06:33
  • @Drop [this](http://stackoverflow.com/questions/11972076/why-is-the-cplusplus-website-bad) –  Jun 19 '13 at 06:33
  • @@h2C03: Not convincing. If you google anything C++ related 1st result most likely will land on cplusplus reference anyway. Another problem is that there aren't many alternatives aside from buying bjarne stroustroup's book (which you can't ctrl+f if it's paper) or somehow "acquiring" copy of C++ standard. 10..12 years ago I would've recommended msdn, but now MS has pretty much ruined it. cplusplus is a decent resource for beginner/intermediate level (few documentation errors do not make it useless). People that want something more "precise" learn how to read C++ standard and compiler docs. – SigTerm Jun 19 '13 at 07:05
  • @SigTerm *"Another problem is that there aren't many alternatives"* - Yes, there are, like [cppreference.com](http://en.cppreference.com/w/). – Christian Rau Jun 20 '13 at 11:02
  • @ChristianRau: That's one. "One" is not "many". – SigTerm Jun 20 '13 at 11:06
  • @SigTerm I didn't say anything contrary (may think, but definitely didn't say), just that this particular "problem" is not a problem at all, so it's at least "many" with "one less" and thus a bit "less many" than before. – Christian Rau Jun 20 '13 at 11:08

2 Answers2

1

A conversion from an array to a std::vector is only possible if you know the size of the array. Note that sizeof works only if the size of the array is known at compile time, at least if you don't use C99, so you cannot put this in a function. Namely, this won't work:

template <typename T>
  inline std::vector<T> ArrayToVec(const T* in) {
    return std::vector<T> (in, in + sizeof(in) / sizeof(T) );
  }

as sizeof(in) in this case will return the size of the pointer.

1) Reccomended way: convert your array to a std::vector when you call the function:

std::vector<unsigned char> (Value, Value + <number of elements in Value>)

I recommend that you put in some proper constant, maybe part of udtByteFeatures. By the way, defining a cast from udtByteFeatures to std::vector would be possible.

Source: What is the simplest way to convert array to vector?

2) Difficult and dangerous way: convert your array using a macro:

#define CharArrayToVec(in) (std::vector<char> (in, in + sizeof(in) / sizeof(*in) ) )

(too bad it cannot be templated on the type :) )

Edit:

(too bad it cannot be templated on the type :) )

Actually you could pass the type as an argument. If there were a typeof operator, you wouldn't even need that.

Community
  • 1
  • 1
Antonio
  • 19,451
  • 13
  • 99
  • 197
0

Values is not a vector. It's an array. It's not even remotely related to an object which is an instance of a class (which std::vector is). If you had searched Google for "initialize vector from array", you would have found out that the fact that arrays decay into pointers is very useful:

int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
std::vector<int> v(a, a + 10);
for (std::vector<int>::iterator it = v.begin(); it != v.end(); it++)
    std::cout << *it << std::endl;
Community
  • 1
  • 1