¿Hay una pestaña equivalente a std:: endl dentro de la biblioteca estándar?


Usando C++, ¿hay una constante de biblioteca estándar equivalente para '\t' como la hay para una nueva línea?

Idealmente:

std::stringstream ss;
ss << std::tab << "text";

Si no, ¿por qué es este el caso?

(Soy consciente de que solo puedo insertar un '\t' pero me gustaría saciar mi curiosidad).

 27
Author: matthewrdev, 2014-02-27

4 answers

No. std::endl no es una constante de nueva línea. Es un manipulador que, además de insertar una nueva línea, también descarga el flujo.

Si solo desea agregar una nueva línea, se supone que solo debe insertar un '\n'. Y si solo desea agregar una pestaña, simplemente inserte un '\t'. No hay std::tab ni nada porque insertar una pestaña más limpiar el flujo no es exactamente una operación común.

 53
Author: Brian,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-02-27 01:30:29

Si desea agregar la característica usted mismo, se vería así:

#include <iostream>

namespace std {
  template <typename _CharT, typename _Traits>
  inline basic_ostream<_CharT, _Traits> &
  tab(basic_ostream<_CharT, _Traits> &__os) {
    return __os.put(__os.widen('\t'));
  }
}

int main() {

  std::cout << "hello" << std::endl;
  std::cout << std::tab << "world" << std::endl;
}

No recomiendo hacer esto, pero quería agregar una solución para completar.

 10
Author: Trevor Hickey,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-24 04:45:10

En Realidad, no es necesario.

Porque endl primero hace el mismo trabajo de insertar una nueva línea que \n, y luego también limpia el búfer.

Insertar \t en un flujo no requiere vaciarlo después .

 3
Author: Pranit Kothari,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-24 04:18:53

No.

Solo hay manipuladores de salida std::ends (insertar carácter nulo) y std::flush (vaciar el flujo) además de std::endl en el archivo include de ostream.

Puede encontrar otros en ios y iomanip archivos de inclusión. La lista completa está aquí

 3
Author: Sly_TheKing,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-06-16 17:03:23