iomanip

Read and write using std::hexfloat

有些话、适合烂在心里 提交于 2019-12-01 18:21:41
问题 This piece of code printed 0 on my machine, but I expected 0.3 . What's wrong? I'm using g++ 6.3.1 on latest Arch Linux. Compilation flags seem unrelevent. #include <iostream> #include <sstream> int main() { std::stringstream s; s << std::hexfloat << 0.3 << std::endl; double d = -1.0; while(s >> std::hexfloat >> d) std::cout << d << std::endl; } 回答1: Use double d = std::strtod(s.str().c_str(), NULL); as a workaround. It seems like a bug. 来源: https://stackoverflow.com/questions/42604596/read

Effective use of C++ iomanip library

感情迁移 提交于 2019-11-28 10:17:07
I created a Vector class in C++ and it works great for my problems. I am now cleaning it up, and I ran into the following piece of code: std::ostream& operator<<(std::ostream &output, const Vector &v){ output<<"[" <<std::setiosflags(std::ios::right | std::ios::scientific) <<std::setw(23) <<std::setprecision(16) <<v._x<<", " <<std::setiosflags(std::ios::right | std::ios::scientific) <<std::setw(23) <<std::setprecision(16) <<v._y<<", " <<std::setiosflags(std::ios::right | std::ios::scientific) <<std::setw(23) <<std::setprecision(16) <<v._z<<"]"; return output; } The code allows to print a vector

How can I print 0x0a instead of 0xa using cout?

做~自己de王妃 提交于 2019-11-27 12:33:18
How can I print 0x0a, instead of 0xa using cout? #include <iostream> using std::cout; using std::endl; using std::hex; int main() { cout << hex << showbase << 10 << endl; } This works for me in GCC: #include <iostream> #include <iomanip> using namespace std; int main() { cout << "0x" << setfill('0') << setw(2) << hex << 10 << endl; } If you are getting sick and tired of iostream's formatting quirkiness, give Boost.Format a try. It allows good-old-fashioned, printf-style format specifiers, yet it is type-safe. #include <iostream> #include <boost/format.hpp> int main() { std::cout << boost:

How can I print 0x0a instead of 0xa using cout?

倖福魔咒の 提交于 2019-11-26 16:03:38
问题 How can I print 0x0a, instead of 0xa using cout? #include <iostream> using std::cout; using std::endl; using std::hex; int main() { cout << hex << showbase << 10 << endl; } 回答1: This works for me in GCC: #include <iostream> #include <iomanip> using namespace std; int main() { cout << "0x" << setfill('0') << setw(2) << hex << 10 << endl; } If you are getting sick and tired of iostream's formatting quirkiness, give Boost.Format a try. It allows good-old-fashioned, printf-style format specifiers