Letīs say that we have string stringArray[5] = {"Lucy","Mary","Tina"}
Is there a way to get the number of names in the array, not the size of the array nor the number of elements there can be in the array max? If there is not such a built-in way,can anyone please give me instructions how could I determine that number? Any help is appreciated.
3 Replies - 124 Views - Last Post: 18 February 2013 - 06:29 PM
#1
Determine the number of elements in a string array
Posted 18 February 2013 - 01:37 PM
Replies To: Determine the number of elements in a string array
#2
Re: Determine the number of elements in a string array
Posted 18 February 2013 - 01:48 PM
With a standard array there is no built in way to get the number of elements in it. However, if you are willing to NULL fill the array (or otherwise fill the array with a known non-value) you can loop through it and determine how many of the values are not the known non-value.
For instance:
Hope that helps.
For instance:
string stringArray[5] = {"Lucy", "Jill", "Amy", "", ""}; // where "" is the known non-value
int numberOfElements = 0;
for(int i = 0; i < 5; i++){
if(stringArray[i] == ""){
numberOfElements++;
}
}
cout << "Elements: " << numberOfElements << endl;
Hope that helps.
#3
Re: Determine the number of elements in a string array
Posted 18 February 2013 - 01:51 PM
Since you're using C++ strings, why not use C++ vectors? Then you could just do:
cout << "Elements: " << stringArray.size() << endl;
#4
Re: Determine the number of elements in a string array
Posted 18 February 2013 - 06:29 PM
The default constructor creates an empty string so null values should not be required.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote






|