stringstream

Stringstream extract integer

假如想象 提交于 2019-11-30 19:39:34
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 } 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 it yourself. Change your code to: ss.clear(); ss.str(Tokens[0]); Why are you reading into a temp string

How to initialize a std::stringstream?

我们两清 提交于 2019-11-30 17:11:55
I need to concatenate a string with integers. To do that I'm using stringstream in the following way: int numPeople = 10; stringstream ss; ss << "Number of people is " << numPeople; And that worked. But I was trying to do it in the way below: int numPeople = 10; stringstream ss << "Number of people is " << numPeople; And I was getting the following error: " expected initializer before '<<' token " Why was I getting this error? Why can't I assign the stringstream value at the same time I declare it? stringstream ss << "Number of people is " << numPeople; Why can't I assign the stringstream

C++ Extract int from string using stringstream

落花浮王杯 提交于 2019-11-30 17:03:42
I am trying to write a short line that gets a string using getline and checks it for an int using stringstream. I am having trouble with how to check if the part of the string being checked is an int. I've looked up how to do this, but most seem to throw exceptions - I need it to keep going until it hits an int. Later I will adjust to account for a string that doesn't contain any ints, but for now any ideas on how to get past this part? (For now, I'm just inputting a test string rather than use getline each time.) int main() { std::stringstream ss; std::string input = "a b c 4 e"; ss.str("");

C++ Extract int from string using stringstream

痴心易碎 提交于 2019-11-30 16:29:18
问题 I am trying to write a short line that gets a string using getline and checks it for an int using stringstream. I am having trouble with how to check if the part of the string being checked is an int. I've looked up how to do this, but most seem to throw exceptions - I need it to keep going until it hits an int. Later I will adjust to account for a string that doesn't contain any ints, but for now any ideas on how to get past this part? (For now, I'm just inputting a test string rather than

C++ Using stringstream after << as parameter

时光毁灭记忆、已成空白 提交于 2019-11-30 14:04:21
Is it possible to write a method that takes a stringstream and have it look something like this, void method(string str) void printStringStream( StringStream& ss) { method(ss.str()); } And can be called like this stringstream var; printStringStream( var << "Text" << intVar << "More text"<<floatvar); I looked up the << operator and it looks like it returns a ostream& object but I'm probably reading this wrong or just not implementing it right. Really all I want is a clean way to concatenate stuff together as a string and pass it to a function. The cleanest thing I could find was a stringstream

Center text in fixed-width field with stream manipulators in C++

只愿长相守 提交于 2019-11-30 13:14:30
问题 I am refactoring some legacy code which is using printf with longs strings (without any actual formatting) to print out plain text table headers which looks notionally like this: | Table | Column | Header | which are currently being produced like this: printf("| Table | Column | Header |"); I would like to produce the above with code to the effect of 1 : outputStream << "|" << std::setw(10) << std::center << "Table" << "|" << std::setw(10) << std::center << "Column" << "|" << std::setw(9) <<

remove char from stringstream and append some data

佐手、 提交于 2019-11-30 08:25:36
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 << '}'; You can extract the string (with the str() member), remove the last char with std::string::erase and then reset the new string as buffer to the std::ostringstream . However, a better solution would be to not

C++ convert simple values to string

China☆狼群 提交于 2019-11-30 08:06:23
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 problem? As far as I know the only way of doing this is by specialising the template by the parameter

Should I preallocate std::stringstream?

南笙酒味 提交于 2019-11-30 07:51:29
问题 I use std::stringstream extensively to construct strings and error messages in my application. The stringstreams are usually very short life automatic variables. Will such usage cause heap reallocation for every variable? Should I switch from temporary to class-member stringstream variable? In latter case, how can I reserve stringstream buffer? (Should I initialize it with a large enough string or is there a more elegant method?) 回答1: Have you profiled your execution, and found them to be a

Center text in fixed-width field with stream manipulators in C++

若如初见. 提交于 2019-11-30 06:52:34
I am refactoring some legacy code which is using printf with longs strings (without any actual formatting) to print out plain text table headers which looks notionally like this: | Table | Column | Header | which are currently being produced like this: printf("| Table | Column | Header |"); I would like to produce the above with code to the effect of 1 : outputStream << "|" << std::setw(10) << std::center << "Table" << "|" << std::setw(10) << std::center << "Column" << "|" << std::setw(9) << std::center << "Header" << "|" << std::endl; which does not compile because <iomanip> has the stream