Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <array>
- #include <string>
- using namespace std;
- int main()
- {
- // stl array string.
- array<char,7> stl_array_string {"He\0llo"};
- int null_count {0};
- for ( const auto& a_char : stl_array_string )
- {
- if ( a_char == '\0' )
- ++null_count;
- }
- cout << "null chars iterated over in strange stl array string: " << null_count << endl;
- // cout << "Streams as \"" << stl_array_string << "\"." << endl;
- // Doesn't compile, but given that that's not what it's for, I'm not surprised.
- // c-style array string
- char c_style_array_string[] {"He\0llo"};
- null_count = 0;
- for ( const auto& a_char : c_style_array_string )
- {
- if ( a_char == '\0' )
- ++null_count;
- }
- cout << "null chars iterated over in c-style, array based string: " << null_count << endl;
- cout << "Streams as \"" << c_style_array_string << "\"." << endl;
- // standard string
- string standard_string {"He\0llo"};
- null_count = 0;
- for ( const auto& a_char : standard_string )
- {
- if ( a_char == '\0' )
- ++null_count;
- }
- cout << "null chars iterated over in standard string: " << null_count << endl;
- cout << "Streams as \"" << standard_string << "\"." << endl;
- return 0;
- // Output is:
- //null chars iterated over in strange stl array string: 2
- //null chars iterated over in c-style, array based string: 2
- //Streams as "He".
- //null chars iterated over in standard string: 0
- //Streams as "He".
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement