stringstream

C++: what benefits do string streams offer?

社会主义新天地 提交于 2020-01-01 09:57:06
问题 could any one tell me about some practical examples on using string streams in c++, i.e. inputing and outputing to a string stream using stream insertion and stream extraction operators? 回答1: You can use string streams to convert anything that implements operator << to a string: #include <sstream> template<typename T> std::string toString(const T& t) { std::ostringstream stream; stream << t; return stream.str(); } or even template <typename U, typename T> U convert(const T& t) { std:

Stream from std::string without making a copy?

故事扮演 提交于 2020-01-01 02:43:08
问题 I have a network client with a request method that takes a std::streambuf* . This method is implemented by boost::iostreams::copy -ing it to a custom std::streambuf -derived class that knows how to write the data to a network API, which works great. This means I can stream a file into the request without any need to read it all into memory. There are some cases, however, where large blocks of data must be sent which are not in a file, so I included an overload that takes a string. To avoid

Extract multiple words to one string variable

余生长醉 提交于 2019-12-31 03:29:12
问题 std::stringstream convertor("Tom Scott 25"); std::string name; int age; convertor >> name >> age; if(convertor.fail()) { // it fails of course } I'd like to extract two or more words to one string variable. So far I've read, it seems that it is not possible. If so, how else to do it? I'd like name to get all characters before number (the age). I'd feel most comfortable using sscanf, but I obviously can't. What I need is ability to extract all words before age for example. 回答1: Most of the

remove char from stringstream and append some data

烈酒焚心 提交于 2019-12-30 02:44:06
问题 In my code there is a loop that adds sth like that "number," to stringstream. When it ends, I need to extract ',' add '}' and add '{' if the loop is to repeated. I thought i can use ignore() to remove ',' but it didn't work. Do you know how I can do what I describe? example: douCoh << '{'; for(unsigned int i=0;i<dataSize;i++) if(v[i].test) douCoh << i+1 << ','; douCoh.get(); douCoh << '}'; 回答1: You can extract the string (with the str() member), remove the last char with std::string::erase

redirect std::cout to a custom writer

喜欢而已 提交于 2019-12-28 11:57:31
问题 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

redirect std::cout to a custom writer

試著忘記壹切 提交于 2019-12-28 11:57:00
问题 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

OpenGL: setting up indices/vertices

岁酱吖の 提交于 2019-12-25 19:03:51
问题 I have been trying to write a very simple .obj model loader in c++ but in both of my methods the model doesn't get displayed correctly and I would also like to know which of the 2 methods is faster/better and why. this is my first method: //vertices&indices are private members of the class "Model" //same with size and indexCount void Model::loadModel(std::string source) { using namespace std; string name = source.substr(0,source.find('.')); cout<<"===LOADING: "<<name<<"==="<<endl; ifstream

OpenGL: setting up indices/vertices

人盡茶涼 提交于 2019-12-25 19:03:26
问题 I have been trying to write a very simple .obj model loader in c++ but in both of my methods the model doesn't get displayed correctly and I would also like to know which of the 2 methods is faster/better and why. this is my first method: //vertices&indices are private members of the class "Model" //same with size and indexCount void Model::loadModel(std::string source) { using namespace std; string name = source.substr(0,source.find('.')); cout<<"===LOADING: "<<name<<"==="<<endl; ifstream

Streams dont work as expected

泪湿孤枕 提交于 2019-12-25 08:15:38
问题 I have this problem and need really fast solving. It's my code: void stypendium() { string tresc = ""; string temp; stringstream ss; fstream file; vector<float> grades; float grade, sum, av; file.open(this->plik, ios_base::in); if(!file.is_open()) { ofstream fileTmp(this->plik); fileTmp.close(); file.open(this->plik, ios_base::in); } while (file >> temp) { if(strspn( temp.c_str(), "-.0123456789" ) == temp.size()) { ss.str(""); ss << temp; ss >> grade; grades.push_back(grade); } }; sum = 0;

Sending a long String over a Socket C++

心已入冬 提交于 2019-12-25 07:42:41
问题 I'm having a strange issue with some basic client <-> server communication using sockets. The server packages an array of structs containing player information by dumping the data into one long string using an ostringstream object and some some simple formatting with newlines and spaces, then sends that string via socket to the client to be displayed to the player. The issue arises when the "name" field in the one or more of the structs being packaged is longer than 4 characters; if this is