There is an array of type string , in which both 1 and 200 values ​​can be written.

 string line[255]; 

How to get the length of the array (not set 255, and recorded in the array)?

Add. Question: Is it possible to create a dynamic string array? If suddenly the values ​​will be over 255 - there will be problems.

  • four
    C-array for C ++ strings ... are you kidding? - user227465
  • 3
    Do not be nonsense. Use std::vector<std::string> . - αλεχολυτ

2 answers 2

 #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; } 
  • Thanks for the example. Yet in my case, vector needed. - Vitali
  • @ Vitali, always happy to help :) - isnullxbh

You can not determine the length of the array with real elements. Either you must support the count of the number of actual elements in the array yourself, or you must add to the array some value of the object of type std::string that you select as the boundary value of the array. And it is not at all necessary that such a boundary value in the context of the task can use an empty string.

In your case, instead of an array, it is better to use the standard class std::vector<std::string> , which will support for you the number of actual elements in the vector, which you can get by calling the function - a member of the size class.

For example,

 #include <vector> #include <string> //... std::vector<std::string> line; line.reserve( 255 ); 
  • How do you interpret the expression "array length with real elements"? Since you say “you can’t determine the length of the array with real elements”, it means that you understood the essence of the problem just as I did - find the number of elements that were reinitialized after the array was created. Otherwise, the phrase “you cannot determine the length of the array with real elements” loses its meaning - we understand that in this case, it is 255. Then who prevents us from adding the initialized() method to the class, for example? And use the value returned by it, as a sufficient condition for searching for elements? - isnullxbh
  • Or is it considered bad practice? - isnullxbh
  • @isnullxbh It's very simple. For example, a character array can contain strings, and then you can determine the actual number of elements in the array using the standard function strlen .. As for the mentioned initialize method or the term "reinitialize", I completely misunderstood their meaning. - Vlad from Moscow
  • Just a minute, now add an example in the post. - isnullxbh
  • Added an example. - isnullxbh