#include <iostream> #include <algorithm> using namespace std; int main() { std::string arr[255]; arr[34] = "sd"; arr[99] = "ds"; size_t count = std::count_if(arr, arr + 255, [](std::string const& str) { return str != ""; }); std::cout << "count=" << count; return 0; }
Add. Question: Is it possible to create a dynamic string array?
So would it be better for you to use some kind of container? std::vector , for example?
UPD ():
#include <iostream> #include <algorithm> using namespace std; class Foo { public: Foo() : m_init( false ) , m_dummy( 0 ) {} void setDummy( int inDummy ) { m_dummy = inDummy; m_init = true; } bool initialized() const { return m_init; } protected: bool m_init; int m_dummy; }; int main() { Foo arr[100]; arr[23].setDummy(2); size_t count = std::count_if( arr, arr + 100, []( Foo const& foo ) { return foo.initialized(); }); std::cout << "count=" << count; return 0; }
std::vector<std::string>. - αλεχολυτ