1

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();
mheavers
  • 29,530
  • 58
  • 194
  • 315
  • **don't** use C-style arrays in C++. either use `std::vector<>` (see Heiko's answer) when the length (size) of the "array" is a run-time variable, or use `std::array<>` (C++11), when it is a compile-time variable, allowing more optimisation (but less flexibility). – Walter Sep 19 '12 at 17:49

5 Answers5

4

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.

Heiko Nardmann
  • 171
  • 1
  • 11
  • +1 this is the only C++ answer I've seen here! all the others, while explaining what's wrong, still use C-style arrays. – Walter Sep 19 '12 at 17:47
  • Great answer but only valid for compilers that handle `c++0x` or `c++11`. Which one is the right way to refer to the new standard? – Dan Sep 19 '12 at 18:01
  • @Walter: There's nothing "wrong" about learning how the language you are using works. Throwing the STL at a beginner without explaining what is actually happening is a terrible idea. – Ed S. Sep 19 '12 at 18:05
  • Thanks Heiko - this seems to be a very simple way to do what I'm looking to do. The only problem is that XCode throws an error with this syntax. Should it maybe be vector s = {"s1","s2"}; ? – mheavers Sep 19 '12 at 21:56
  • I assume XCode uses g++? Which version? Maybe you have to add the option '-std=c++0x'? – Heiko Nardmann Sep 27 '12 at 17:37
2

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();
daminetreg
  • 9,724
  • 1
  • 23
  • 15
  • 2
    Currently all major compilers aren't fully c++11 compatible, so I assume that most people are still coding in C++03. – daminetreg Sep 19 '12 at 18:07
1

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);
jrok
  • 54,456
  • 9
  • 109
  • 141
  • 1
    Read [this question](http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c) for more info on arrays. – jrok Sep 19 '12 at 17:35
  • Thanks - this actually answers my question, but it sounds like there might be a better way for me to do it, especially considering I'll need to refer to these strings by their index later. – mheavers Sep 19 '12 at 17:36
  • You can use indices without a problem on pointers, too, if that's your concern. – jrok Sep 19 '12 at 17:40
0

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.

Philipp
  • 67,764
  • 9
  • 118
  • 153
  • 2
    `sizeof some_array / sizeof some_array[0]` works just fine as long as you actually have an array. – Ed S. Sep 19 '12 at 17:28
  • Also, I don't think it is helpful to simply say "use the STL" to a beginner. Would you want to work with someone who doesn't understand arrays and other basic language constructs? – Ed S. Sep 19 '12 at 17:30
0

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.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • I'm confused about the arr[10] ... why 10? – mheavers Sep 19 '12 at 17:33
  • `10` in this context is just a hint (to the reader of code, I guess), it has no effect. The declaration is actually equivalent to void `foo(int* arr);` – jrok Sep 19 '12 at 17:43
  • @mheavers: Yes, as jrok said, it was just to provide an example of a function which takes an array as input, but actually receives a pointer. – Ed S. Sep 19 '12 at 18:05