c++ - what class type should s be in after s has been resolve by for(auto &s :text)? -


string text("asdffffffffff.f"); (const auto &s : text){   cout << s ;   if(s.empty() || s[s.size()-1] == '.')       cout << endl;   else         cout << "" ;   } return 0; }; 
  1. my book doesn't state text should be, made string text up, type should text be? const string, const char, else?

    1. after resolve, type s becomes?
    2. what s[s.size()-1] ? or function type s attempting call both empty , size? -after compiling error: 'empty , size in 's,' of non-class type 'const char.
    3. when though string , char function in book, couldn't find empty() or size() function. have make function empty() , size()?

in case auto deducted type char, s const char&.

to fix code can write :

    (const auto& s : text){     cout << s ;     if(s == '\0' || s == '.')         cout << endl;     else         cout << "" ; } 

'\0' represents empty char. don't have write function. '\0' acts string terminator, every string , char* contains '\0' @ end appended automatically you.

empty() , size() member functions of std::string class. text std::string can use text.empty() or text.size() .size() returns total size of string.

however s not array here (it's single reference char, not char*) can't use operator [] single char.


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

wso2esb - How to concatenate JSON array values in WSO2 ESB? -