stringstream

C++ Extract number from the middle of a string

纵饮孤独 提交于 2019-12-17 19:07:18
问题 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,

std::stringstream operator>> fails to convert string to float

自古美人都是妖i 提交于 2019-12-12 16:47:21
问题 I can't understand why the second >> fails. Am I doing something wrong or missing some code? std::ifstream file; std::stringstream ss; std::string str; float f1, f2; file.open("file.txt"); getline(file, str); ss.str(str); ss >> f1; getline(file, str);//when packed inside if(), evalueates to true ss.str(str); ss >> f2; //when packed inside if(), evalueates to false - but why it fails? std::cout<<"str = "<<str<<"\n"; std::cout<<"ss.str() = "<<ss.str()<<"\n"; std::cout<<"f1 = "<<f1<<"\nf2 = "<

How to display multiple leading zeros for floating point values in C++? [duplicate]

99封情书 提交于 2019-12-12 10:47:29
问题 This question already has answers here : How can I pad an int with leading zeros when using cout << operator? (6 answers) Closed 4 years ago . In a C++ program, I want to display a column of floating point values so that the sign, digits, and decimal point all line up. Multiple leading zeros should pad the whole number part of each value, when necessary. For example: A column of floating point values: +000.0012 -000.0123 +000.1235 -001.2346 +012.3457 -123.4568 I had an elaborately commented

Rounding off floats with ostringstream

我与影子孤独终老i 提交于 2019-12-12 10:37:09
问题 I have an issue regarding conversion from float to c++ string using ostringstream. Here is my line: void doSomething(float t) { ostringstream stream; stream << t; cout << stream.str(); } when t has value -0.89999 it is round off to -0.9, but when it's value is 0.0999 or lesser than this say 1.754e-7, it just prints without round off. what can be the solution for this. 回答1: You need to set the precision for ostringstream using precision e.g stream.precision(3); stream<<fixed; // for fixed

num_get facet and stringstream conversion to boolean - fails with initialised boolean?

早过忘川 提交于 2019-12-12 09:09:50
问题 I have inherited a template to convert a string to a numerical value, and want to apply it to convert to boolean . I am not very experienced with the stringstream and locale classes. I do seem to be getting some odd behaviour, and I am wondering if someone could please explain it to me? template<typename T> T convertFromString( const string& str ) const { std::stringstream SStream( str ); T num = 0; SStream >> num; return num; } This works fine until I try the boolean conversion string str1(

Can I tell if a std::string represents a number using stringstream?

时间秒杀一切 提交于 2019-12-12 08:34:43
问题 Apparently this is suposed to work in showing if a string is numerical, for example "12.5" == yes, "abc" == no. However I get a no reguardless of the input. std::stringstream ss("2"); double d; ss >> d; if(ss.good()) {std::cout<<"number"<<std::endl;} else {std::cout<<"other"<<std::endl;} 回答1: You should use an istringstream so that it knows it's trying to parse input. Also, just check the result of the extraction directly rather than using good later. #include <sstream> #include <iostream>

stringstream::str copy lifetime

我是研究僧i 提交于 2019-12-12 07:29:54
问题 Something a lot of C++ programmers miss (read: me) when first using stringstream s is the fact that the copy returned by stringstream::str() is a temporary, which lasts until the end of the expression it's used in. However, I don't understand: How this is done. Looking in the sstream header for libstdc++, I only see a copy being made and returned. How is the lifetime restricted? Why this is the desired behavior, especially since it is such a common gotcha. If a copy is being made anyway, why

What is a “query parameter” in C++?

限于喜欢 提交于 2019-12-12 03:56:59
问题 We were using stringstream to prepare select queries in C++. But we were strongly advised to use QUERY PARAMETERS to submit db2 sql queries to avoid using of stringstream. Can anyone share what exactly meant by query parameter in C++? Also, share some practical sample code snippets. Appreciate the help in advance. Edit: It is stringstream and not strstream. Thanks, Mathew Liju 回答1: I suspect this refers to parameterized queries in general, rather than constructing the query in a string, they

convert hex buffer to unsigned int

ぐ巨炮叔叔 提交于 2019-12-12 03:16:38
问题 I've been trying to convert a hexadecimal number saved in a buffer to an unsigned int. However the "0x00" in front of every hexadecimal number that I'm reading from has been giving me problem, in essence the problem (in a downscaled version) looks like this: char b[] = "0x0014A12"; std::stringstream ss; unsigned int i; ss << std::hex << b; ss >> i; cout << i << endl; Any tips? Note: The program outputs a high decimal nubmer which equals CCCCCC in hex. 回答1: This works fine for me: #include

Last item of istringstream repeats?

空扰寡人 提交于 2019-12-11 19:43:11
问题 I'm having trouble with splitting a string with sstream. It appears when the loop of cout happens, the last item of data, an integer in my case, is repeated? Is my loop set up wrong?? This is my code: #include <iostream> #include <string> #include <list> #include <fstream> #include <sstream> using namespace std; int main() { string inputText("Jane Smith 18"); istringstream iss(inputText); while (iss) { string first; string last; int age; iss >> first >> last >> age; cout << first << " " <<