Click here to Skip to main content
15,947,841 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: std:strstream does not work Pin
Stefan_Lang23-Dec-20 23:59
Stefan_Lang23-Dec-20 23:59 
AnswerRe: std:strstream does not work Pin
Stefan_Lang23-Dec-20 23:56
Stefan_Lang23-Dec-20 23:56 
GeneralRe: std:strstream does not work Pin
Haakon S.24-Dec-20 0:37
Haakon S.24-Dec-20 0:37 
GeneralRe: std:strstream does not work Pin
Richard MacCutchan24-Dec-20 4:18
mveRichard MacCutchan24-Dec-20 4:18 
GeneralRe: std:strstream does not work Pin
Stefan_Lang24-Dec-20 5:10
Stefan_Lang24-Dec-20 5:10 
GeneralRe: std:strstream does not work Pin
Haakon S.24-Dec-20 11:59
Haakon S.24-Dec-20 11:59 
AnswerRe: std:strstream does not work. Solution (sort of) Pin
Haakon S.26-Dec-20 1:11
Haakon S.26-Dec-20 1:11 
GeneralRe: std:strstream does not work. Solution (sort of) Pin
Randor 26-Dec-20 3:28
professional Randor 26-Dec-20 3:28 
I think your original idea in your first post... std::stringstream was probably the best way.

You just needed to add a istream operator >> to handle the CComponent. Something like this:
C++
class CComponent
{
private:
	std::string something;
public:
	CComponent() noexcept = default;
	friend std::istream& operator >> (std::istream& in, CComponent& component);
};

istream& operator >> (istream& in, CComponent& c)
{
	in >> component.something;
	return in;
}
Which could then be used like this:
C++
const std::string test_haakon = "Lorem ipsum dolor sit amet";
CComponent test_component;
std::stringstream test_string_stream(test_haakon.c_str());
test_string_stream >> test_component;
Merry Christmas to you and your family.

Best Wishes,
-David Delaune

UPDATE:
I really hate answering these modern C++ questions because it takes a lot more effort. I shouldn't have said that it was 'probably the best way' because if you use the code above it will make a copy of the data three times.

If you are able to use C++17 in your project then you will be able to avoid one of those copies by deriving a class from std::basic_streambuf as such:
C++
class CComponent
{
private:
	std::string something;
public:
	CComponent() noexcept = default;
	friend std::istream& operator >> (std::istream& in, CComponent& component);
};

template<typename T = CHAR, typename CT = std::char_traits<T> >
class make_stream_buffer_no_copy : public std::basic_streambuf<T, CT>
{
public:
	make_stream_buffer_no_copy(CHAR* p, size_t len)
	{
		basic_streambuf<T>::setg(p, p, p + len);
		basic_streambuf<T>::setp(p, p + len);
	}

	std::basic_stringstream<T> get_stream()
	{
		return std::basic_stringstream<T>(basic_streambuf<T>::pbase(), ios_base::in | ios_base::out);
	}
};

istream& operator >> (istream& in, CComponent& c)
{
	in >> c.something;
	return in;
}
Which you would use like this:
C++
CHAR p2[] = "Lorem ipsum dolor sit amet";
CComponent test_component2;
make_stream_buffer_no_copy no_copy(p2,strlen(p2));
no_copy.get_stream() >> test_component2;

You can avoid ALL copying if you are willing to extend your CComponent class.

modified 26-Dec-20 11:03am.

QuestionConfigure Script Issue Pin
jblixt21-Dec-20 12:37
professionaljblixt21-Dec-20 12:37 
AnswerRe: Configure Script Issue Pin
Randor 21-Dec-20 13:01
professional Randor 21-Dec-20 13:01 
AnswerRe: Configure Script Issue Pin
k505421-Dec-20 17:42
mvek505421-Dec-20 17:42 
QuestionWriting a Factory Design Pattern Program with separate Header Files and Class Implementations Pin
Litu Sahoo17-Dec-20 12:26
Litu Sahoo17-Dec-20 12:26 
AnswerRe: Writing a Factory Design Pattern Program with separate Header Files and Class Implementations Pin
Gerry Schmitz17-Dec-20 19:27
mveGerry Schmitz17-Dec-20 19:27 
AnswerRe: Writing a Factory Design Pattern Program with separate Header Files and Class Implementations Pin
Richard MacCutchan17-Dec-20 23:36
mveRichard MacCutchan17-Dec-20 23:36 
QuestionBeginning C++ for Windows Application Pin
chipp_zanuff13-Dec-20 6:06
chipp_zanuff13-Dec-20 6:06 
AnswerRe: Beginning C++ for Windows Application Pin
Gerry Schmitz13-Dec-20 6:19
mveGerry Schmitz13-Dec-20 6:19 
QuestionRe: Beginning C++ for Windows Application Pin
Richard MacCutchan13-Dec-20 6:32
mveRichard MacCutchan13-Dec-20 6:32 
Questioncmath.h compile error Pin
Haakon S.9-Dec-20 21:20
Haakon S.9-Dec-20 21:20 
AnswerRe: cmath.h compile error Pin
CPallini9-Dec-20 21:39
mveCPallini9-Dec-20 21:39 
GeneralRe: cmath.h compile error Pin
Haakon S.10-Dec-20 0:51
Haakon S.10-Dec-20 0:51 
GeneralRe: cmath.h compile error Pin
CPallini10-Dec-20 1:16
mveCPallini10-Dec-20 1:16 
AnswerRe: cmath.h compile error Pin
Greg Utas10-Dec-20 1:04
professionalGreg Utas10-Dec-20 1:04 
GeneralRe: cmath.h compile error Pin
Haakon S.10-Dec-20 1:58
Haakon S.10-Dec-20 1:58 
GeneralRe: cmath.h compile error Pin
Greg Utas10-Dec-20 2:19
professionalGreg Utas10-Dec-20 2:19 
SuggestionRe: cmath.h compile error Pin
David Crow10-Dec-20 2:13
David Crow10-Dec-20 2:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.