Originally Posted by
The Brain
The string class member function size( ) returns the number of characters in a string (not including the \0 null terminator) The cstring function strlen( ) also returns the same value.
No. Normally a C++ string does not have a null (\0) terminator. But if it did, the '\0' would be included in the number of characters returned by size(). For example:
Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s = "hello";
cout << s << "world" << endl;
cout << s.size() << endl;
s = s + '\0';
cout << s << "world" << endl;
cout << s.size() << endl;
s = s + '\0';
cout << s << "world" << endl;
cout << s.size() << endl;
return 0;
}
outputs:
Code:
helloworld
5
helloworld
6
helloworld
7
Press any key to continue
kermit: did you find
Code:
for(string::size_type idx = 0; idx != str.size(); ++idx)
in a textbook? It does seem inefficient, but i wonder if a compiler would fix it.