stringstream

String Stream in C++ to parse string of words & numbers

爱⌒轻易说出口 提交于 2021-02-18 12:39:06
问题 I have string like this: '123plus43times7' where numbers are followed by words from a dictionary. I understand that I can extract int/numbers by using the >> operator: StringStream >> number I can get the number. However, the Stream still has the number in it. How do I remove the number when the length of number is unknown or should I find out length of number and then use str.substr() to create a new String Stream ? Any other better method for doing it using C++ STL String and SStream would

reusing a stringstream without re-allocation

浪尽此生 提交于 2021-02-10 07:49:28
问题 I am trying to figure out how to reuse a stringstream object without the need to re-allocate the underlying string every time I put something in the stream. I have found this answer which led me to do this: int main() { stringstream ss; int x; ss << "423"; ss >> x; // x is now 423 ss.clear(); ss.seekg(0); ss.seekp(0); ss << "1"; ss >> x; // x is now 123. Instead I want x to be 1. std::cout << x << std::endl; } Unfortunately, this doesn't work since the contents of the string from the first

Why do successive istream_iter objects increment the iterated stream?

不打扰是莪最后的温柔 提交于 2021-01-27 10:40:53
问题 Why does the following code output c ? // main.cpp #include <iostream> #include <sstream> #include <iterator> int main( int argc, char* argv[] ) { std::string p( "abcdefghijklmnopqrstuvwxyz" ); std::stringstream ss(p); std::istream_iterator<char> i( ss ); std::istream_iterator<char> j( ss ); std::istream_iterator<char> k( ss ); std::cout << *k << std::endl; return 0; } . $ g++ --version g++ (GCC) 9.2.1 20190827 (Red Hat 9.2.1-1) Copyright (C) 2019 Free Software Foundation, Inc. This is free

Skip whitespaces with getline

喜你入骨 提交于 2020-12-28 07:50:28
问题 I'm making a program to make question forms. The questions are saved to a file, and I want to read them and store them in memory (I use a vector for this). My questions have the form: 1 TEXT What is your name? 2 CHOICE Are you ready for these questions? Yes No My problem is, when I'm reading these questions from the file, I read a line, using getline, then I turn it into a stringstream, read the number and type of question, and then use getline again, on the stringstream this time, to read

Converting unsigned char * to hexstring

家住魔仙堡 提交于 2020-07-21 07:35:46
问题 Below code takes a hex string(every byte is represented as its corresponidng hex value) converts it to unsigned char * buffer and then converts back to hex string. This code is testing the conversion from unsigned char* buffer to hex string which I need to send over the network to a receiver process. I chose hex string as unsigned char can be in range of 0 to 255 and there is no printable character after 127. The below code just tells the portion that bugs me. Its in the comment. #include