iomanip

cout << setw doesn't align correctly with åäö

牧云@^-^@ 提交于 2019-12-08 15:41:29
问题 The following code reproduces my problem: #include <iostream> #include <iomanip> #include <string> void p(std::string s, int w) { std::cout << std::left << std::setw(w) << s; } int main(int argc, char const *argv[]) { p("COL_A", 7); p("COL_B", 7); p("COL_C", 5); std::cout << std::endl; p("ABC", 7); p("ÅÄÖ", 7); p("ABC", 5); std::cout << std::endl; return 0; } This produces the following output: COL_A COL_B COL_C ABC ÅÄÖ ABC If i change "ÅÄÖ" in the code to e.g. "ABC", then it works: COL_A COL

How to understand C++ std::setw 's inconsistent behaviour?

旧城冷巷雨未停 提交于 2019-12-07 20:10:56
问题 Given the following code: /*Formatting Output **Goal: practice using cout to format output to console **Print the variables in three columns: **Ints, Floats, Doubles */ #include <iostream> #include <iomanip> using namespace std; int main() { int a = 45; float b = 45.323; double c = 45.5468; int aa = a + 9; float bb = b + 9; double cc = c + 9; int aaa = aa + 9; float bbb = bb + 9; double ccc = cc + 9; // 1st attempt :> cout << "\n\n\n" << "// 1st attempt :>" << "\n"; cout <<

How to understand C++ std::setw 's inconsistent behaviour?

不问归期 提交于 2019-12-06 11:56:34
Given the following code: /*Formatting Output **Goal: practice using cout to format output to console **Print the variables in three columns: **Ints, Floats, Doubles */ #include <iostream> #include <iomanip> using namespace std; int main() { int a = 45; float b = 45.323; double c = 45.5468; int aa = a + 9; float bb = b + 9; double cc = c + 9; int aaa = aa + 9; float bbb = bb + 9; double ccc = cc + 9; // 1st attempt :> cout << "\n\n\n" << "// 1st attempt :>" << "\n"; cout << "12345678901234567890123456789012345678901234567890" << "\n"; cout << "Ints" << setw(15) << "Floats" << setw(15) <<

using stream operator<< with std::endl in c++

一个人想着一个人 提交于 2019-12-06 00:44:06
I am trying out the following C++ class for using the stream operator << to log contents from this answer : class Log { public: Log() : m_filename( "dafault.log" ) {} // if you wanna give other names eventually... Log( const std::string & p_filename ) : m_filename( p_filename ) {} virtual ~Log() { // implement your writeToFile() with std::ofstream writeToFile( m_filename, m_stream, true ); } template< typename T > Log & operator<<( const T & p_value ) { m_stream << p_value; return *this; } private: std::string m_filename; std::ostringstream m_stream; }; This works for many cases. However, it

Correctly pad negative integers with zeros with std::cout

亡梦爱人 提交于 2019-12-05 14:03:10
问题 I found this question already asked, but the answer everybody gives is std::cout << std::setw(5) << std::setfill('0') << value << std::endl; which is fine for positive numbers, but with -5, it prints: 000-5 Is there a way to make it print -0005 or to force cout to always print at least 5 digits (which would result in -00005) as we can do with printf? 回答1: std::cout << std::setw(5) << std::setfill('0') << std::internal << -5 << '\n'; // ^^^^^^^^ Output: -0005 std::internal Edit: For those

Does put_money hold its argument by value or reference?

若如初见. 提交于 2019-12-05 05:15:45
Does the following invoke undefined behavior? #include <iostream> #include <iomanip> #include <algorithm> #include <experimental/iterator> int main() { long double values[] = {1, 2, 3}; std::transform( std::begin(values), std::end(values), std::experimental::make_ostream_joiner(std::cout, ", "), [](long double v) { return std::put_money(v + 1); } ); return 0; } My worry is that return std::put_money(v + 1) returns a reference to the temporary v + 1 . The standard ( [ext.manip]/6 ) only defines this specific expression: out << put_­money(mon, intl); It is unspecified how mon is stored in the

How are iomanip Functions Implemented?

浪子不回头ぞ 提交于 2019-12-04 00:49:57
问题 Some of the standard iomanip functions take take a parameter. I'd like to know how this is accomplished, for instance, can I do something similar with a function? That's really the solution that I needed for this answer, but I couldn't figure out how to do this. When I looked up the definition for setw function for example in http://en.cppreference.com it lists the return type as "unspecified", and it also only lists one argument, rather than also taking a stream& parameter. How does this

Correctly pad negative integers with zeros with std::cout

怎甘沉沦 提交于 2019-12-04 00:26:56
I found this question already asked, but the answer everybody gives is std::cout << std::setw(5) << std::setfill('0') << value << std::endl; which is fine for positive numbers, but with -5, it prints: 000-5 Is there a way to make it print -0005 or to force cout to always print at least 5 digits (which would result in -00005) as we can do with printf? std::cout << std::setw(5) << std::setfill('0') << std::internal << -5 << '\n'; // ^^^^^^^^ Output: -0005 std::internal Edit: For those those that care about such things, N3337 ( ~c++11 ), 22.4.2.2.2 : The location of any padding is determined

The C++ equivalent of C's format string [closed]

安稳与你 提交于 2019-12-02 23:56:34
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 3 years ago . I have a C program that reads from keyboard, like this: scanf("%*[ \t\n]\"%[^A-Za-z]%[^\"]\"", ps1, ps2); For a better understanding of what this instruction does, let's split the format string as follows: %*[ \t\n]\" => read all spaces, tabs and newlines ( [ \t\n] ) but not store them in any

The C++ equivalent of C's format string [closed]

本秂侑毒 提交于 2019-12-02 15:17:06
I have a C program that reads from keyboard, like this: scanf("%*[ \t\n]\"%[^A-Za-z]%[^\"]\"", ps1, ps2); For a better understanding of what this instruction does, let's split the format string as follows: %*[ \t\n]\" => read all spaces, tabs and newlines ( [ \t\n] ) but not store them in any variable (hence the ' * '), and will keep reading until encounter a double quote ( \" ), however the double quote is not input. Once scanf() has found the double quote, reads all caracters that are not letters into ps1. This is accomplished with... %[^A-Za-z] => input anything not an uppercase letter 'A'