istringstream

istringstream to int8_t produce unexpected result

孤街浪徒 提交于 2019-12-13 09:28:23
问题 After read this FAQ, i choose to use istringstream to convert my input string to numerical value. My code is look like this: <template T> T Operand<T>::getValue(const std::string &s) { T _value; std::istringstream v_ss(s); v_ss >> _value; return _value; } When T is int, short, long or float, no problem i get correct value. But when T is int8_t, this code doesn't work. By exemple, if my input string is "10", getValue return me a int8_t with value equals 49. With 49 == '1' in ASCII table, i

Reading a double from an istream in Hex

徘徊边缘 提交于 2019-12-12 19:14:52
问题 Given double foo I can assign it from a hex format string using sscanf like this: sscanf("0XD", "%lg", &foo) But I cannot seem to get an istringstream to behave the same way. All of these just write 0 to foo : istringstream("0XD") >> foo istringstream("0XD") >> hex >> foo istringstream("D") >> hex >> foo This is particularly concerning when I read here that the double istream extraction operator should: The check is made whether the char obtained from the previous steps is allowed in the

Lvalue istringstream Required for istream_iterator?

痞子三分冷 提交于 2019-12-12 03:28:33
问题 Given a string foo in Visual Studio I can break the words into a vector by doing: vector fooVec{ istream_iterator<string>(istringstream(foo)), istream_iterator<string>() }; But this won't compile in gcc 5.1. I get the error: invalid initialization of non-const reference of type std::istream_iterator<std::basic_string<char> >::istream_type& {aka std::basic_istream<char>& } from an rvalue of type std::basic_istream<char> Now I know that gcc had a bug that was fixed by our own Jonathan Wakely.

Stream types in C++, how to read from IstringStream?

可紊 提交于 2019-12-12 00:26:57
问题 i have a txt file that has millions of lines, each line has 3 floats, i read it using the following code : ifstream file(path) float x,y,z; while(!file.eof()) file >> x >> y >> z; and i works perfectly. Now i want to try doing the same thing using Boost mapped files, so i do the following string filename = "C:\\myfile.txt"; file_mapping mapping(filename.c_str(), read_only); mapped_region mapped_rgn(mapping, read_only); char* const mmaped_data = static_cast<char*>(mapped_rgn.get_address());

strtok vs istringsteam on splitting the strings?

非 Y 不嫁゛ 提交于 2019-12-11 20:27:43
问题 I am trying to split my actual key on dot and then extract all the fields after splitting it on dot. My key would look like something this - t26.example.1136580077.colox Below is the code I have which I was in the impression, it should work fine but after that I figured it out that it is for windows only and I am running my code on ubuntu and because of that reason I always get - error: âstrtok_sâ was not declared in this scope Below is my code if(key) { vector<string> res; char* p; char*

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 << " " <<

Extract the coefficients of a polynomial with istringstream c++

自作多情 提交于 2019-12-11 03:08:47
问题 currently i'm working on a project (namely, creating a class of a polynomial) and i've already implemented the "add-subtract-divide-and-so-on" methods. But i'm stuck on a method to pass from a string like that 3x^1-2x^4 to a vector of coefficients like 0 3 0 0 4. So here's the code: string s; cin >> s; istringstream iss(s); double coeff; char x, sym; int degree; vector<double> coefficients; int i = 0; while (iss >> coeff >> x >> sym >> degree) { //if (sign == '-') coeff *= -1; if (degree == i

How to block on reading a c++ stringstream to wait for data

你。 提交于 2019-12-10 17:35:53
问题 So, I've been trying to figure out, how to wait for data from a C++ stringstream (for instance), without being constantly checking if data is there, which is quite CPU consuming. I'm perfectly able to read, for instance, from a serial device, and lock the process while no data arrives, but unfortunately I haven't been able to figure how to do that with C++ streams. I'm sure I'm missing something, since cin does exactly that, i.e., waits for the return key to step out from istream reading, but

“Off by one error” while using istringstream in C++

怎甘沉沦 提交于 2019-12-10 16:03:15
问题 I get an off by one error while executing the following code #include <iostream> #include <sstream> #include <string> using namespace std; int main (int argc, char* argv[]){ string tokens,input; input = "how are you"; istringstream iss (input , istringstream::in); while(iss){ iss >> tokens; cout << tokens << endl; } return 0; } It prints out the last token "you" twice, However if I make the following changes everything works fine. while(iss >> tokens){ cout << tokens << endl; } Can anyone

istringstream invalid error beginner

为君一笑 提交于 2019-12-08 12:13:19
问题 I have this piece of code : if(flag == 0) { // converting string value to integer istringstream(temp) >> value ; value = (int) value ; // value is a } I am not sure if I am using the istringstream operator right . I want to convert the variable "value" to integer. Compiler error : Invalid use of istringstream. How should I fix it ? After trying to fix with the first given answer . it's showing me the following error : stoi was not declared in this scope Is there a way we can work past it .