empty fields and nonexistent fields in C++ -


i'm reading "programming principles , practice using c++ 2nd edition" , there theory question in chapter 23 fields goes this

  1. what difference between empty field , nonexistent field?

the best thing find in book information field is. in words it's way format integer values control how appear on output. example setw() manipulator can used change way how digits being displayed on output.

i couldn't find in google me lot if drop fast, short comment.

or maybe should better ask

what empty field , what's nonexistent field?

edit : chapter i'm reading called "text manipulation" , bjarne introducing regular expressions here read table (4 columns , lot of rows) see if info in table matching pattern

this answer 1 other forum 1 nice guy answered me :)

#include <iostream> #include <string> #include <regex>  void display_field( const std::string& text, const std::string& name ) {     // field interested in marked subexpression ([[:alnum:]]*)     // ie. 0 or more alphanumeric characters after 'name='     const std::regex regex( name + '=' + "([[:alnum:]]*)" ) ;     std::smatch match_results ;     std::regex_search( text, match_results, regex ) ;      std::cout << "'" << name << "' == " ;     if( match_results.empty() ) std::cout << "  --- (nonexistent field)\n\n" ;     else if( match_results[1].length() == 0 ) std::cout << "''  --- (empty field)\n\n" ;     else std::cout << "'" << match_results[1] << "'  --- (non-empty field)\n\n" ; }  int main() {     const std::string text = "name=etrusks email= posts=168 phone= " ;     for( std::string fn : { "name", "email", "posts", "age", "phone", "address" } )         display_field( text, fn ) ; } 

this excelent answer, happy :)

output :

'name' == 'etrusks' --- (non-empty field)
'email' == '' --- (empty field)
'posts' == '168' --- (non-empty field)
'age' == --- (nonexistent field)
'phone' == '' --- (empty field)
'address' == --- (nonexistent field)


Comments