stringstream

How can I improve formatting number with commas performance?

霸气de小男生 提交于 2019-12-07 07:58:43
问题 I'm using the following method to format a number with commas: template<class T> static std::string FormatNumberWithCommas(T value, int numberOfDecimalPlaces = 0) { std::stringstream ss; ss.imbue(std::locale("")); ss.precision(numberOfDecimalPlaces); ss << std::fixed << value; return ss.str(); } Profiling has show this method to take a significant amount of time relative to other code. Specifically the profiler has identified the line: ss.imbue(std::locale("")); And within that I believe it

Getting a byte value using stringstream

六眼飞鱼酱① 提交于 2019-12-07 03:32:11
问题 I've got this (incorrect) sample code for getting a value out of stringstream and storing it in a byte-sized variable (it needs to be in a single byte var, not an int): #include <iostream> #include <sstream> using namespace std; int main(int argc, char** argv) { stringstream ss( "1" ); unsigned char c; ss >> c; cout << (int) c << endl; } The output when I run this is 49, which is not what I would like to see. Obviously, this is being treated as a char and not simple numeric value. What is the

字符串与其他基本类型的转换——从C到C++11

橙三吉。 提交于 2019-12-06 22:35:19
转自 IBM 编译器中国开发团队的《C++11中的string - atoi/itoa》 在C++11中,由于右值引用的引入,常为人所诟病std::string的性能问题得到了很大的改善。另外一方面,我们也可以看到新语言为std::string类增加了很多新的api。比较引人注意的就是std::string的成员函数stoi系列,以及std::to_string全局函数。这两种API虽然很不起眼,却为C++11的格式化输出(formatted I/O)增加了一种实用的手段。我们可以依序会议一下C,C++98,C++11中我们是如何处理atoi/itoa的问题的: 在C时代,通常我们遇到atoi(字符串到数值转换)的问题的时候我们会使用<stdlib.h>中的atoi函数: int num = atoi(cstr); 这里的cstr通常为char 或者const char 类型的字符串。函数返回的结果则是该字符串所表示的一个十进制的integer。函数的整个效果则等同于<stdlib.h>中的另外一个函数strtol: int num = strtol(cstr, NULL, 10); 相比于atoi,strtol多了最后一个参数"radix"表明函数采用的是几进制(这个进制数可以从2到34,这个数值范围的原因显而易见)。除去strtol会在出错时设置全局的errno外

The role of std::ws (whitespace) when reading data

丶灬走出姿态 提交于 2019-12-06 04:57:41
Data saved in my file is (white spaces added at both beginning and end on purpose for this test): 1 2 3 Loading the data using the code below with or without "std::ws" does not cause any difference. So I am confused by the role of "std::ws" as I have seen code using it. Can someone explain a little bit? Thanks! void main () { ifstream inf; inf.open ("test.txt"); double x=0, y=0, z=0; string line; getline(inf, line); istringstream iss(line); //Using "std::ws" here does NOT cause any difference if (!(iss >> std::ws >> x >> y >> z >> std::ws)) { cout << "Format error in the line" << endl; } else

std::stringstream as parameter to a function

拥有回忆 提交于 2019-12-05 19:24:08
I have a std::vector<std::string> temp_results and I wish to use std::for_each to go through this vector and concatenate a string, so I concocted the following construction: std::stringstream ss; std::string res = std::for_each(temp_results.begin(), temp_results.end(), boost::bind(addup, _1, ss)); std::string addup(std::string str, std::stringstream ss) { ss << str; ss << ";"; return ss.str; } I get the following error, which is beyond my understanding: error C2475: 'std::basic_stringstream<_Elem,_Traits,_Alloc>::str' : forming a pointer-to-member requires explicit use of the address-of

cannot overwrite stringstream variable with a new value

折月煮酒 提交于 2019-12-05 17:44:42
string whatTime(int seconds) { string h,m,s,ans; stringstream ss; ss << (seconds/3600); seconds -= (3600*(seconds/3600)); ss >> h; ss.str(""); ss << (seconds/60); seconds -= (60*(seconds/60)); ss >> m; ss.str(""); ss << seconds; ss >> s; return (h + ":" + m + ":" + s ); } Output for above program is coming in this format "some_value::" I have also tried ss.str(std::string()) and ss.str().clear() but even that doesn't work. Could somebody please suggest any ways how to tackle this problem? Lightness Races with Monica You've correctly emptied the string buffer with ss.str("") , but you also need

atof and stringstream produce different results

陌路散爱 提交于 2019-12-05 11:06:21
I have been looking into a problem whereby I am converting a float to a human readable format, and back. Namely a string. I have ran into issues using stringstream and found that atof produces "better" results. Notice, I do not print out the data in this case, I used the debugger to retrieve the values: const char *val = "73.31"; std::stringstream ss; ss << val << '\0'; float floatVal = 0.0f; ss >> floatVal; //VALUE IS 73.3100052 floatVal = atof(val); //VALUE IS 73.3099976 There is probably a reasonable explanation to this. If anybody can enlighten me I'd be greatful :). Answer is based on the

Getting a byte value using stringstream

允我心安 提交于 2019-12-05 08:12:09
I've got this (incorrect) sample code for getting a value out of stringstream and storing it in a byte-sized variable (it needs to be in a single byte var, not an int): #include <iostream> #include <sstream> using namespace std; int main(int argc, char** argv) { stringstream ss( "1" ); unsigned char c; ss >> c; cout << (int) c << endl; } The output when I run this is 49, which is not what I would like to see. Obviously, this is being treated as a char and not simple numeric value. What is the most c++ish way to get c to hold a 1 rather than a 49 when casted to an int? Thanks! The most C++-ish

What are the default Format Flags (and widths) for double output in a std::stringstream?

跟風遠走 提交于 2019-12-05 05:55:06
问题 What is the default format, when I'm writting a double to a stringstream ? double v = 3.0; std::stringstream ss; ss << v; Where can I find a list of the default format setup for a stringstream ? Is the default format the same for all derived classes of std::istream (within the stdlib)? 回答1: The defaults are setup by std::basic_ios::init and are the same for all streams derived from ios_base . The defaults are: rdbuf() sb tie() 0 rdstate() goodbit if sb is not a null pointer, otherwise badbit.

C++ reset locale to “C” globally?

岁酱吖の 提交于 2019-12-05 01:48:53
In a project I am currently working on I link to a proprietary dynamic library. As soon as I run the library's initialize function, the behavior of logging and printing of numbers changes. Commas have been inserted at every third decimal. Ie. cout << 123456789 << endl used to print out 123456789 and now it prints 123,456,789 . This is horribly annoying, because this behavior is not what I want. This issue is not only apparent in the binary I am compiling, but also shows up in all the couts and stringstreams in the libraries that I link to it. I have tried using this line of code after calling