vendredi 27 novembre 2015

How do I determine the number of items I have stored in an array?

I have a string array of size 5, and I have n elements in it. How could I determine n? I have tried sizeof(array)/sizeof(array[0]), but that returns the size of the array, which is 5. My code is:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string array[5];
    array[0] = "pie";
    array[1] = ":P";
    array[2] = "YELLOW";
    cout << sizeof(array)/sizeof(array[0]);
}

Aucun commentaire:

Enregistrer un commentaire