Я смотрю пример setw
на cppreference. Он использует setw
, чтобы установить ширину. В этом примере предполагается, что строка извлекается в 'arr', и она устанавливает ширину 6. Но почему в 'arr' всего 5 символов, почему результат "привет", а не "привет"? Спасибо за ваш ответ.
код источника: std :: setw
#include <sstream>
#include <iostream>
#include <iomanip>
int main()
{
std::cout << "no setw:" << 42 << '\n'
<< "setw(6):" << std::setw(6) << 42 << '\n'
<< "setw(6), several elements: " << 89 << std::setw(6) << 12 << 34 << '\n';
std::istringstream is("hello, world");
char arr[10];
is >> std::setw(6) >> arr;
std::cout << "Input from \"" << is.str() << "\" with setw(6) gave \""
<< arr << "\"\n";
}
Выход:
no setw:42
setw(6): 42
setw(6), several elements: 89 1234
Input from "hello, world" with setw(6) gave "hello"