Questions about std::stringstream
-
- How to empty stringstream after I put into it some symbols?
- Some funtion needs the start iterator and pass-the-end iterator. How to get these iterators from a stringstream object?
-
- How to empty stringstream after I put into it some symbols?
You could use the heap and simply replace it by a new stringstream. This works perfectly:
#include <iostream> #include <string> #include <sstream> using namespace std; int main() { stringstream* s = new stringstream(); *s << "so what?"; cout << s->str(); delete s; s = new stringstream(); cout << s->str(); return 0; }
- Some funtion needs the start iterator and pass-the-end iterator
You could use the method str() as shown above and use the iterators of the returned string. Or you try to work with the underlying stringbuf-Object. Here you can read about it:
http://www.cplusplus.com/reference/iostream/stringbuf/
-
SAn schrieb:
- How to empty stringstream after I put into it some symbols?
As follows:
std::stringstream Stream; Stream.str("");
SAn schrieb:
- Some funtion needs the start iterator and pass-the-end iterator. How to get these iterators from a stringstream object?
If you just need the Iterators of the
std::string
, usestr()
to get the stream's string and access thestd::string::iterator
.
-
Wirdbald schrieb:
You could use the heap and simply replace it by a new stringstream. This works perfectly:
Oh, and you call that simple?
-
Other question: how to pass std::string object to the WinAPI function which will write data to it?
-
SAn schrieb:
Other question: how to pass std::string object to the WinAPI function which will write data to it?
Hm, I think you can't do this directly since the internal stuff of
std::string
is implementation-defined.So, you have to go the indirect way about a
char
-Pointer. Getting a string's Pointer can be done by the member functionstd::string::c_str()
. Note that you get aconst char*
here, so you have to copy the string.Assigning a
std::string
to achar
-Pointer can either be done byoperator=
,assign()
, or the constructor.
-
I assume that you need to pass a char*. You could do this by copying the whole string via std::string::copy(char* s, size_t n) to a char-array. Or you directly use char*. It depends on what you aim to do with the string.
-
-
Nice. Now I was the first to find the probably more suitable method
Have a nice day. I should work a little bit now...
-
Question:
Will
std::basic_stringstream<unsigned int, std::char_traits<unsigned int>, std::allocator<unsigned int> >
correctly process UTF32? Willoperator>>
will convert it to numbers etc. ??
-
Der stellt ja immer nur Fragen ohne auf die Antworten einzugehen.
Allerunterste Schublade.
-
Will std::basic_stringstream<unsigned int, std::char_traits<unsigned int>, std::allocator<unsigned int> > correctly process UTF32?
Of course. But it will not provide you anything more than simple input-output of 4-byte strings. If you want to do any conversions from or to any encoding you have to implement it by yourself.
Will operator>> will convert it to numbers
Nobody knows, I guess. Because it depends on the implementation of that operator that you will have to supply yourself or get from some library.
Maybe it would be a good idea to post what you are attempting to do. I don't think there are WinAPI-Functions using UCS4-Encoding. Not even utf16. As far as I know windows is uses UCS2 - or did I miss something?
-
I just sitting here for 4 hours and trying to convert
std::vector<unsigned int>
containing UTF-32 to the system-default ANSI codepage.The only solution I found is:
- Convert UTF-32 to UTF-8 using utfcpp.
- Convert UTF-8 to UTF-16 using utfcpp or MultiByteToWideChar WinAPI function
- Convert UTF-16 to the system-default ANSI codepage by WideCharToMultiByte(CP_ACP,...
Is there the easier way to do such a conversion???
-
Of course. You should rather call it UCS4 than UTF32. Both is simply pure unicode, one character per 4-byte value.The Winapi-Function
int WideCharToMultiByte( UINT CodePage, DWORD dwFlags, LPCWSTR lpWideCharStr, int cchWideChar, LPSTR lpMultiByteStr, int cbMultiByte, LPCSTR lpDefaultChar, LPBOOL lpUsedDefaultChar );
takes a pointer to a WCHAR-String as its parameter LPCWSTR. You could copy your 4-byte values to a new WCHAR-array and pass that array to WideCharToMultiByte. If some character doesn't fit into 2 bytes, you won't be able to encode it as ANSI anyway. But you could check for overflows and treat the errors as needed in your context.