istringstream

Convert string to __uint128_t using stringstreams

孤街醉人 提交于 2019-12-07 13:21:30
问题 I'm trying to extract different types of data from a string. void readHeader(char buf[BUFFSIZE]) { std::istringstream hdr(buf); __uint128_t id_client; hdr >> id_client; // doesn't compile } I'm getting this error when I do that hdr >> id_client : Unix/UnixSocket.cpp:158:10: error: ambiguous overload for ‘operator>>’ in ‘hdr >> id_client’ Unix/UnixSocket.cpp:158:10: note: candidates are: In file included from /usr/include/c++/4.7/sstream:39:0, from Unix/UnixSocket.cpp:11: /usr/include/c++/4.7

std::istringstream from std::string without copying

丶灬走出姿态 提交于 2019-12-07 07:38:53
问题 I've been using this: ifstream in("file.txt") string line; getline(in,line); istringstream iss(line); ... for some simple parsing. I would like to avoid unnecessary copying in order to improve performance so I tried: ifstream in("huge_line.txt"); string line; getline(in,line); istringstream ss; ss.rdbuf()->pubsetbuf(const_cast<char*>(line.c_str()), line.size()); ... and it seems to do the job (significantly improve performance, that is). My question is, is this safe given the const_cast? I

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

Convert string to __uint128_t using stringstreams

我只是一个虾纸丫 提交于 2019-12-05 22:08:00
I'm trying to extract different types of data from a string. void readHeader(char buf[BUFFSIZE]) { std::istringstream hdr(buf); __uint128_t id_client; hdr >> id_client; // doesn't compile } I'm getting this error when I do that hdr >> id_client : Unix/UnixSocket.cpp:158:10: error: ambiguous overload for ‘operator>>’ in ‘hdr >> id_client’ Unix/UnixSocket.cpp:158:10: note: candidates are: In file included from /usr/include/c++/4.7/sstream:39:0, from Unix/UnixSocket.cpp:11: /usr/include/c++/4.7/istream:118:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT,

std::istringstream from std::string without copying

筅森魡賤 提交于 2019-12-05 15:07:11
I've been using this: ifstream in("file.txt") string line; getline(in,line); istringstream iss(line); ... for some simple parsing. I would like to avoid unnecessary copying in order to improve performance so I tried: ifstream in("huge_line.txt"); string line; getline(in,line); istringstream ss; ss.rdbuf()->pubsetbuf(const_cast<char*>(line.c_str()), line.size()); ... and it seems to do the job (significantly improve performance, that is). My question is, is this safe given the const_cast? I mean, as long as I'm working with an istrinstream, the internal buffer should never get written to by the

non-copying istringstream

匆匆过客 提交于 2019-12-05 01:15:27
So istringstream copies the contents of a string when initialised, e.g string moo("one two three four"); istringstream iss(moo.c_str()); I was wondering if there's a way to make std::istringstream use the given c_str as its buffer without copying things. This way, it won't have to copy large bits of memory before passing the std::istringstream& to functions that take istream& as an argument. What I've been trying to do is convert some functions which only take std::ifstream& arguments (they're mostly parsers) into taking istream& as well. Would I have to make my own istream subclass for this?

C++ compile error: has initializer but incomplete type

让人想犯罪 __ 提交于 2019-12-03 04:08:00
I am coding in Eclipse and have something like the following: #include <ftream> #include <iostream> void read_file(){ char buffer[1025]; std::istringstream iss(buffer); } However, when I try to build, I get the following error: variable 'std::istringstream iss' has initializer but incomplete type Any quick thoughts? I have googled around and it seems like most people with this problem simply did not include the right header files which I believe I am doing correctly. Jive Dadson You need this include: #include <sstream> 来源: https://stackoverflow.com/questions/13428164/c-compile-error-has

Splitting a string into integers using istringstream in C++

扶醉桌前 提交于 2019-12-03 03:18:32
问题 I'm trying to use istringstream to split a simple string into a series of integers: #include <string> #include <iostream> #include <sstream> #include <vector> using namespace std; int main(){ string s = "1 2 3"; istringstream iss(s); while (iss) { int n; iss >> n; cout << "* " << n << endl; } } And i get: * 1 * 2 * 3 * 3 Why is the last element always coming out twice? How to fix it? 回答1: It's coming out twice because your looping is wrong, as explained (indirectly) at http://www.parashift

Splitting a string into integers using istringstream in C++

╄→尐↘猪︶ㄣ 提交于 2019-12-02 16:49:07
I'm trying to use istringstream to split a simple string into a series of integers: #include <string> #include <iostream> #include <sstream> #include <vector> using namespace std; int main(){ string s = "1 2 3"; istringstream iss(s); while (iss) { int n; iss >> n; cout << "* " << n << endl; } } And i get: * 1 * 2 * 3 * 3 Why is the last element always coming out twice? How to fix it? It's coming out twice because your looping is wrong, as explained (indirectly) at http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.5 ( while (iss) is not dissimilar from while (iss.eof()) in this

How to extract mixed format using istringstream

自古美人都是妖i 提交于 2019-12-01 18:05:20
Why does my program not output: 10 1.546 ,Apple 1 instead of 10 1 <empty space> here's my program: #include <iostream> #include <string> #include <sstream> using namespace std; int main () { string str = "10,1.546,Apple 1"; istringstream stream (str); int a; double b; string c, dummy; stream >> a >> dummy >> b >> dummy >> c; cout << a << endl; cout << b << endl; cout << c << endl; return 0; } Basically I am trying to parse the comma-separated strings, any smoother way to do this would help me immense. In IOStreams, strings (meaning both C-strings and C++ strings) have virtually no formatting