std::endl in a string variable?

前端 未结 4 2038
青春惊慌失措
青春惊慌失措 2021-01-18 16:03

Hi i want to save multiply lines in a string. I got a string logstring and i want to save multiplay error logs which i later can print in a txt file or as a console output.

4条回答
  •  时光取名叫无心
    2021-01-18 16:33

    To separate lines in a string you should use the new line escape character '\n'.

    For example

    std::string logstring;
    
    //...
    
    logstring += "Error Message 1 Date";
    logstring += '\n';
    
    logstring += "Error Message 2 Date";
    logstring += '\n';
    
    logstring += "Error Message 3 Date";
    logstring += '\n';
    

    As for std::endl then it is a function declared like

    template 
    basic_ostream& endl(basic_ostream& os);
    

    It is a so-called stream manipulator. Fo example in this statement

    std::cout << std::endl;
    

    there is used operator << that accepts pointer to the function as an argument.

    You can not concatenate a string with the function pointer std::endl and if you did this somehow it did not make sense.

提交回复
热议问题