stringstream

How to force std::stringstream operator >> to read an entire string?

半世苍凉 提交于 2019-11-30 04:03:36
问题 How to force std::stringstream operator >> to read an entire string instead of stopping at the first whitespace? I've got a template class that stores a value read from a text file: template <typename T> class ValueContainer { protected: T m_value; public: /* ... */ virtual void fromString(std::string & str) { std::stringstream ss; ss << str; ss >> m_value; } /* ... */ }; I've tried setting/unsetting stream flags but it didn't help. Clarification The class is a container template with

Stringstream extract integer

泪湿孤枕 提交于 2019-11-30 03:52:54
问题 Why do I fail to extract an integer value into the Num variable? #include <sstream> #include <vector> #include <iostream> using namespace std; int main() { string Digits("1 2 3"); stringstream ss(Digits); string Temp; vector<string>Tokens; while(ss >> Temp) Tokens.push_back(Temp); ss.str(Tokens[0]); int Num = 0; ss >> Num; cout << Num; //output: 0 } 回答1: When the stream extracts the last of the 3 digist "1 2 3" the eof state will be set. This is not cleared by the str() member,you need to do

Going from string to stringstream to vector<int>

我是研究僧i 提交于 2019-11-30 02:59:14
I've this sample program of a step that I want to implement on my application. I want to push_back the int elements on the string separately, into a vector. How can I? #include <iostream> #include <sstream> #include <vector> using namespace std; int main(){ string line = "1 2 3 4 5"; //includes spaces stringstream lineStream(line); vector<int> numbers; // how do I push_back the numbers (separately) here? // in this example I know the size of my string but in my application I won't } int num; while (lineStream >> num) numbers.push_back(num); Johannes Schaub - litb This is a classic example of

How to deal with last comma, when making comma separated string? [duplicate]

ε祈祈猫儿з 提交于 2019-11-29 23:45:38
Possible Duplicates: Don't print space after last number Printing lists with commas C++ #include <vector> #include <iostream> #include <sstream> #include <boost/foreach.hpp> using namespace std; int main() { vector<int> VecInts; VecInts.push_back(1); VecInts.push_back(2); VecInts.push_back(3); VecInts.push_back(4); VecInts.push_back(5); stringstream ss; BOOST_FOREACH(int i, VecInts) { ss << i << ","; } cout << ss.str(); return 0; } This prints out: 1,2,3,4,5, However I want: 1,2,3,4,5 How can I achieve that in an elegant way? I see there is some confusion about what I mean with "elegant": E.g.

C++: vector to stringstream

非 Y 不嫁゛ 提交于 2019-11-29 22:29:23
I want to know if it is possible to transform a std::vector to a std::stringstream using generic programming and how can one accomplish such a thing? Adapting Brian Neal's comment, the following will only work if the << operator is defined for the object in the std::vector (in this example, std::string ). #include <iostream> #include <sstream> #include <vector> #include <string> #include <iterator> // Dummy std::vector of strings std::vector<std::string> sentence; sentence.push_back("aa"); sentence.push_back("ab"); // Required std::stringstream object std::stringstream ss; // Populate std:

How do I convert from stringstream to string in C++?

牧云@^-^@ 提交于 2019-11-29 20:47:00
How do I convert from std::stringstream to std::string in C++? Do I need to call a method on the string stream? Tyler McHenry ​​​​​​​ yourStringStream.str() Emil H Use the .str()-method : Manages the contents of the underlying string object. 1) Returns a copy of the underlying string as if by calling rdbuf()->str() . 2) Replaces the contents of the underlying string as if by calling rdbuf()->str(new_str) ... Notes The copy of the underlying string returned by str is a temporary object that will be destructed at the end of the expression, so directly calling c_str() on the result of str() (for

Memory Error with std:ostringstream and -std=c++11? [duplicate]

不想你离开。 提交于 2019-11-29 16:48:46
This question already has an answer here: stringstream, string, and char* conversion confusion 5 answers EDIT : Thanks to everyone who pointed out the problem, and that it was discussed on Stack Overflow. I cast the last close vote myself. A related question: neither CPP Reference on ostringstream or ostringstream::str state its a temporary. How did so many people know? Or is there different documentation I should have consulted? I'm having a lot of trouble with memory errors under Debian 7.3 (x64) with GCC 4.7.2, -std=c++11 and std::ostringstream . Its leading to bizaare results like https:/

stringstream用法

最后都变了- 提交于 2019-11-29 15:49:30
1.头文件:#include<sstream> 2.stringstream是C++提供的串流(stream)物件,其中: clear()重置流的标志状态;str()清空流的内存缓冲,重复使用内存消耗不再增加! 在使用stringstream时遇到的问题: #include <cstdlib> #include <iostream> #include <sstream> using namespace std; int main(int argc, char * argv[]) { stringstream stream; int a,b; stream<<"80"; stream>>a; stream<<"90"; stream>>b; cout<<a<<endl; cout<<b<<endl; system("PAUSE "); return EXIT_SUCCESS; } 运行结果: 预期b为90,但是出现-858993460, 这是由于stringstream重复使用时,没有清空导致的。 修改之后: #include <cstdlib> #include <iostream> #include <sstream> using namespace std; int main(int argc, char * argv[]) { stringstream stream; int

stringstream->rdbuf()->pubsetbuf is not setting the buffer

ぃ、小莉子 提交于 2019-11-29 13:57:25
I am trying to modify a stringbuffer of a stringstream object without having to copy a string, using the method pubsetbuf, but it is not working. I am following the documentation in http://www.cplusplus.com/reference/iostream/streambuf/pubsetbuf/ . Here is my example code: #include <iostream> #include <sstream> int main(int argc, char* argv[]) { std::stringstream stream("You say goodbye"); char replace[] = {"And I say hello"}; std::cout << stream.str() << std::endl; // Checking original contents stream.rdbuf()->pubsetbuf(replace, 16); // Should set contents here std::cout << stream.str() <<

C++ convert simple values to string

陌路散爱 提交于 2019-11-29 11:04:01
问题 Right now I use the following piece of code to dummily convert basic types ( int , long , char[] , this kind of stuff) to std::string for further processing: template<class T> constexpr std::string stringify(const T& t) { std::stringstream ss; ss << t; return ss.str(); } however I don't like the fact that it depends on std::stringstream . I tried using std::to_string (from C++11's repertoire) however it chokes on char[] variables. Is there a simple way offering an elegant solution for this