stringstream

How to stop doubles converting to scientific notation when using a stringstream

ⅰ亾dé卋堺 提交于 2019-11-28 10:06:07
I'm making a function to return the number of decimal and whole number digits and am converting the inserted typename number to a string using sstream s. However the number when being converted to a string comes out in scientific notations which is not useful for counting the number of digits are in the normal number. How can I stop this from happening in my function below? enum { DECIMALS = 10, WHOLE_NUMBS = 20, ALL = 30 }; template < typename T > int Numbs_Digits(T numb, int scope) { stringstream ss(stringstream::in | stringstream::out); stringstream ss2(stringstream::in | stringstream::out)

C++ Extract number from the middle of a string

天涯浪子 提交于 2019-11-28 09:40:54
I have a vector containing strings that follow the format of text_number-number Eg: Example_45-3 I only want the first number ( 45 in the example) and nothing else which I am able to do with my current code: std::vector<std::string> imgNumStrVec; for(size_t i = 0; i < StrVec.size(); i++){ std::vector<std::string> seglist; std::stringstream ss(StrVec[i]); std::string seg, seg2; while(std::getline(ss, seg, '_')) seglist.push_back(seg); std::stringstream ss2(seglist[1]); std::getline(ss2, seg2, '-'); imgNumStrVec.push_back(seg2); } Are there more streamlined and simpler ways of doing this? and if

stringstream->rdbuf()->pubsetbuf is not setting the buffer

邮差的信 提交于 2019-11-28 07:31:28
问题 I am trying to modify a stringbuffer of a stringstream object without having to copy a string, using the method pubsetbuf, but it is not working. I am following the documentation in http://www.cplusplus.com/reference/iostream/streambuf/pubsetbuf/. Here is my example code: #include <iostream> #include <sstream> int main(int argc, char* argv[]) { std::stringstream stream("You say goodbye"); char replace[] = {"And I say hello"}; std::cout << stream.str() << std::endl; // Checking original

redirect std::cout to a custom writer

为君一笑 提交于 2019-11-28 06:38:44
I want to use this snippet from Mr-Edd's iostreams article to print std::clog somewhere. #include <iostream> #include <iomanip> #include <string> #include <sstream> int main() { std::ostringstream oss; // Make clog use the buffer from oss std::streambuf *former_buff = std::clog.rdbuf(oss.rdbuf()); std::clog << "This will appear in oss!" << std::flush; std::cout << oss.str() << '\\n'; // Give clog back its previous buffer std::clog.rdbuf(former_buff); return 0; } so, in a mainloop, I will do something like while (! oss.eof()) { //add to window text somewhere } Here's the ostringstream docs but

C++ Compare and replace last character of stringstream

﹥>﹥吖頭↗ 提交于 2019-11-28 05:48:06
问题 I would like to check the following: If the last character appended to the stringstream is a comma. If it is remove it. std::stringstream str; str << "[" //loop which adds several strings separated by commas str.seekp(-1, str.cur); // this is to remove the last comma before closing bracket str<< "]"; The problem is if nothing is added in the loop, the opening bracket is removed from the string. So I need a way to check whether the last character is a comma. I did that like this: if (str.str()

C++: insert char to a string

做~自己de王妃 提交于 2019-11-27 21:59:59
so I am trying to insert the character, which i got from a string, to another string. Here I my actions: 1. I want to use simple: someString.insert(somePosition, myChar); 2. I got an error, because insert requires(in my case) char* or string 3. I am converting char to char* via stringstream: stringstream conversion; char* myCharInsert; conversion << myChar //That is actually someAnotherString.at(someOtherPosition) if that matters; conversion >> myCharInsert; someString.insert(somePosition, myCharInsert); 4. Everything seems to be compiling successfully, but program crashes the gets to

Getting a buffer into a stringstream in hex representation:

旧街凉风 提交于 2019-11-27 21:53:41
If I had a buffer like: uint8_t buffer[32]; and it was filled up completely with values, how could I get it into a stringstream, in hexadecimal representation, with 0-padding on small values? I tried: std::stringstream ss; for (int i = 0; i < 32; ++i) { ss << std::hex << buffer[i]; } but when I take the string out of the stringstream, I have an issue: bytes with values < 16 only take one character to represent, and I'd like them to be 0 padded. For example if bytes 1 and 2 in the array were {32} {4} my stringstream would have: 204 instead of 2004 Can I apply formatting to the stringstream to

使用 stringstream 进行类型转换

為{幸葍}努か 提交于 2019-11-27 21:40:36
如何用使用stringstream进行类型转换: 1. 下面例子为整型和sting类型的相互转换示例 整型转换为字符串类型 string NumberToString(int num) { stringstream ss; ss<<num; //像流中传值 string result; ss>>result; //将流中的值写入到result return result; } 字符创类型转换为整型 int StringToNumber(string strNum) { stringstream ss; ss<<strNum; int result; ss>>result; return result; } 编译期就确定了num,result,strNum的类型,调用stringstream的时候拥有足够的信息判断来进行自动转换,并将转换结果缓存到stringstream对象的内部缓冲中 stringstream对象会根据需要自动分配内存,不用担心溢出问题. 例如: float n = 22.22; string strNum = "22.22"; cout<<StringToNumber(strNum)<<endl; cout<<NumberToString(n)<<endl; 输出:22 22 2. 任意类型的转换 template <class output_type

Incomplete type is not allowed: stringstream

时间秒杀一切 提交于 2019-11-27 20:06:35
问题 Why does this line give the error Error: incomplete type is not allowed ? stringstream ss; 回答1: #include <sstream> and use the fully qualified name i.e. std::stringstream ss; 回答2: Please, add: #include <sstream> 回答3: An incomplete type error is when the compiler knows the identifier is a type, for instance because you have a forward-declaration of it (e.g. class stringstream; ), but it hasn't seen a full definition for it ( class stringstream { ... }; ). This could happen for a type that is

resetting a stringstream

≯℡__Kan透↙ 提交于 2019-11-27 19:12:48
How do I "reset" the state of a stringstream to what it was when I created it? int firstValue = 1; int secondValue = 2; std::wstringstream ss; ss << "Hello: " << firstValue; std::wstring firstText(ss.str()); //print the value of firstText here //How do I "reset" the stringstream here? //I would like it behave as if I had created // stringstream ss2 and used it below. ss << "Bye: " << secondValue; std::wstring secondText(ss.str()); //print the value of secondText here This is the way I usually do it: ss.str(""); ss.clear(); // Clear state flags. I would do std::wstringstream temp; ss.swap(temp)