I'm new to c++. I just want to declare an array and find its size. How do I do this? I keep getting a character count or something.
string vidArray[] = {"fingers.mov","motion_test.mov"};
int numVids = vidArray->size();
I'm new to c++. I just want to declare an array and find its size. How do I do this? I keep getting a character count or something.
string vidArray[] = {"fingers.mov","motion_test.mov"};
int numVids = vidArray->size();
Just use
vector<string> s{"s1", "s2", "s3"};
for the initialization of your "array".
then you can use
s.size()
to get the size of the vector.
First of all the vidArray->size() call you made is equivalent to vidArray[0].call() because an array is nothing else than a pointer pointing the first element, so by calling vidArray->size() you are simply doing : (*vidArray).size().
Such old arrays doesn't have a size(), there are however unadvisable hacks based on macros to get the count of an array :
#define COUNTOF(arr) (sizeof(arr) / sizeof(arr[0]))
COUNTOF(vidArray)
This will return you the array size, but this isn't a great thing (Explanations), that's why I would advise you to use an std::vector if you need dynamic resizing :
std::vector<std::string> vid;
vid.push_back("fingers.mov");
vid.push_back("motion_test.mov");
size_t vidSize = vid.size();
That you can also initialize this way with boost::assign (Documentation)
std::vector<std::string> vid;
vid += std::string("fingers.mov"),std::string("motion_test.mov");
Or a boost::array (Documentation) if you want an array which cannot vary in size (i.e. As stated by Walter in the comments, if you are already using c++11 you can use std::array which is boost::array but in the standard library)
boost::array<std::string,2> vid = {std::string("fingers.mov"),std::string("motion_test.mov")};
size_t vidSize = vid.size();
Arrays decay to pointers to their first element when passed around. This happens in your case, too - you get string*
and you actually call string's member function size()
. It returns number of characters it holds.
To do what you want, divide the total size of the array by size of a single element:
int numVids = sizeof(vidArray) / sizeof(string);
In C++, arrays aren't objects. They don't have any methods like size(). What you are calling is the size() method of the first string in the array, because the memory address of an array is also the memory address of its first entry.
When you want a smart container class which behaves like an array, you could use std::vector. It comes with a lot of convenient functions, including a size() function.
As @Philipp suggested, you should prefer containers like stdd::vector
over an array in general. However, that doesn't mean you should skip over the basics, so it is best to avoid libraries like the STL until you learn the language.
To get the size of an array, i.e., the number of elements, use the sizeof
operator, which returns the size of the array in bytes:
int arr[10];
size_t element_count = sizeof arr / sizeof arr[0];
Note that, if you pass an array to a function, it decays to a pointer, so this won't work.
void foo(int arr[10]) {
// element_count == sizeof int*, oops
size_t element_count = sizeof arr;
}
In this case you would need to pass the size of the array to the function as well
void foo(int *arr, size_t size) {
}
Once you get a firm grasp on this stuff, prefer a safer and more convenient container like a vector
. It grows dynamically on demand, guarantees that elements will be stored in contiguous memory (like an array), provides safe access methods (if needed) like ::at()
, and you don't have to worry about manual memory management.