Click here to Skip to main content
14,768,479 members
Home » Discussions » C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: std:strstream does not work Pin
Stefan_Lang24-Dec-20 0:59
mveStefan_Lang24-Dec-20 0:59 
AnswerRe: std:strstream does not work Pin
Stefan_Lang24-Dec-20 0:56
mveStefan_Lang24-Dec-20 0:56 
GeneralRe: std:strstream does not work Pin
Haakon S.24-Dec-20 1:37
MemberHaakon S.24-Dec-20 1:37 
GeneralRe: std:strstream does not work Pin
Richard MacCutchan24-Dec-20 5:18
mveRichard MacCutchan24-Dec-20 5:18 
GeneralRe: std:strstream does not work Pin
Stefan_Lang24-Dec-20 6:10
mveStefan_Lang24-Dec-20 6:10 
GeneralRe: std:strstream does not work Pin
Haakon S.24-Dec-20 12:59
MemberHaakon S.24-Dec-20 12:59 
AnswerRe: std:strstream does not work. Solution (sort of) Pin
Haakon S.26-Dec-20 2:11
MemberHaakon S.26-Dec-20 2:11 
GeneralRe: std:strstream does not work. Solution (sort of) Pin
Randor 26-Dec-20 4:28
professional Randor 26-Dec-20 4: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:
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:
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:
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:
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 13:37
professionaljblixt21-Dec-20 13:37 
AnswerRe: Configure Script Issue Pin
Randor 21-Dec-20 14:01
professional Randor 21-Dec-20 14:01 
AnswerRe: Configure Script Issue Pin
k505421-Dec-20 18:42
mvek505421-Dec-20 18:42 
QuestionWriting a Factory Design Pattern Program with separate Header Files and Class Implementations Pin
Litu Sahoo17-Dec-20 13:26
MemberLitu Sahoo17-Dec-20 13:26 
AnswerRe: Writing a Factory Design Pattern Program with separate Header Files and Class Implementations Pin
Gerry Schmitz17-Dec-20 20:27
mveGerry Schmitz17-Dec-20 20:27 
AnswerRe: Writing a Factory Design Pattern Program with separate Header Files and Class Implementations Pin
Richard MacCutchan18-Dec-20 0:36
mveRichard MacCutchan18-Dec-20 0:36 
QuestionBeginning C++ for Windows Application Pin
chipp_zanuff13-Dec-20 7:06
Memberchipp_zanuff13-Dec-20 7:06 
AnswerRe: Beginning C++ for Windows Application Pin
Gerry Schmitz13-Dec-20 7:19
mveGerry Schmitz13-Dec-20 7:19 
QuestionRe: Beginning C++ for Windows Application Pin
Richard MacCutchan13-Dec-20 7:32
mveRichard MacCutchan13-Dec-20 7:32 
Questioncmath.h compile error Pin
Haakon S.9-Dec-20 22:20
MemberHaakon S.9-Dec-20 22:20 
AnswerRe: cmath.h compile error Pin
CPallini9-Dec-20 22:39
mveCPallini9-Dec-20 22:39 
GeneralRe: cmath.h compile error Pin
Haakon S.10-Dec-20 1:51
MemberHaakon S.10-Dec-20 1:51 
GeneralRe: cmath.h compile error Pin
CPallini10-Dec-20 2:16
mveCPallini10-Dec-20 2:16 
AnswerRe: cmath.h compile error Pin
Greg Utas10-Dec-20 2:04
mvaGreg Utas10-Dec-20 2:04 
GeneralRe: cmath.h compile error Pin
Haakon S.10-Dec-20 2:58
MemberHaakon S.10-Dec-20 2:58 
GeneralRe: cmath.h compile error Pin
Greg Utas10-Dec-20 3:19
mvaGreg Utas10-Dec-20 3:19 
SuggestionRe: cmath.h compile error Pin
David Crow10-Dec-20 3:13
MemberDavid Crow10-Dec-20 3: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.