There are ten lines, even though you only initialized two of them:
#include <iostream> int main (void) { std::string str[10] = {"one","two"}; std::cout << sizeof(str)/sizeof(*str) << std::endl; std::cout << str[0] << std::endl; std::cout << str[1] << std::endl; std::cout << str[2] << std::endl; std::cout << "===" << std::endl; return 0; }
Output:
10 one two
If you want to count non-empty lines:
#include <iostream> int main (void) { std::string str[10] = {"one","two"}; size_t count = 0; for (size_t i = 0; i < sizeof(str)/sizeof(*str); i++) if (str[i] != "") count++; std::cout << count << std::endl; return 0; }
Outputs 2 as expected.
source share