in Education by
I've seen examples that allow you to create a manipulator that inserts delimiters but none of those manipulators are sticky. That is, the manipulator returns a special class that inserts the delimiter, rather than modifying the output stream permanently so that it can do it on its own. I want to be able to do this: std::cout << sep(", "); std::cout << "hello" << "world"; // "hello, world" At the moment this prints "h, e, l, l, o, w, o, r, l, d" when I need it to be "hello, world". The reason I'm getting the wrong output is because I put the printing in the overflow() method and overflow() is being called for each character. I'm not sure where is the appropriate place to put it. Sorry about it being verbose. If I knew a simpler way to write it I would. Just start from the bottom and work your way up: #include <iostream> #include // index for delimiter int separator() { static int idx = std::ios_base::xalloc(); return idx; } // index for storage of dynamically-allocated buffer int rdbuffer() { static int idx = std::ios_base::xalloc(); return idx; } struct custom_separator : std::streambuf { public: custom_separator(std::ostream& _stream) : stream(_stream), buffer(_stream.rdbuf()) {} int_type overflow(int_type c) { // has a token been read already if (token_read) { char* delim = static_cast(stream.pword(separator())); // print delim buffer->sputn(delim, strlen(delim)); } token_read = true; return buffer->sputc(c); } private: std::ostream& stream; std::streambuf* buffer; bool token_read = false; }; // deletes the buffer and the delimiter void cleanup(std::ios_base::event evt, std::ios_base& str, int idx) { if (evt == std::ios_base::erase_event) { delete static_cast(str.pword(idx)); delete static_cast(str.pword(rdbuffer())); } } std::ostream& set_separator(std::ostream& os, const char* str) { if (!os.bad()) { // If a separator string doesn't exist, assign os a buffer that prints one if (!os.pword(separator())) { auto buf = new custom_separator(os); os.rdbuf(buf); // this is to keep track of buf so we can delete it later os.pword(rdbuffer()) = static_cast(buf); os.register_callback(cleanup, separator()); } // delete the previous separator delete static_cast(os.pword(separator())); // store the new one os.pword(separator()) = (void*)(str); } return os; } struct sep { explicit sep(const char* _sep) : separator(new char[strlen(_sep) + 1]) { strcpy(separator, _sep); } sep(const sep&) = delete; sep(const sep&&) = delete; char* separator; }; std::ostream& operator<<(std::ostream& os, const sep& manip) { set_separator(os, manip.separator); return os; } int main() { std::cout << sep(", "); std::cout << "hello"; std::cout << "world"; // "h, e, l, l, o, w, o, r, l, d" } The main issue with overflow() is that I don't know when to detect when the end of a token like "hello" has been read to know when to insert. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Try the following. Additionally, new line break processing (new line symbol) has been added so that the separator is not added during the transfer (after new line). #include class MyStream { public: struct Sep { Sep (const std::string& sep_value = ""): m_sep(sep_value) { } operator std::string() { return m_sep; } private: std::string m_sep; }; MyStream& operator << (const Sep& sep) { m_sep = sep; m_add_sep = false; return *this; } MyStream& operator << (const std::string& str) { if(str.find(MyStream::endl) != std::string::npos) m_add_sep = false; operator<< <std::string>(str); m_add_sep = false; return *this; } template MyStream& operator << (const T& value) { if(m_add_sep) std::cout << static_cast<std::string>(m_sep); std::cout << value; m_add_sep = true; return *this; } static const std::string endl; private: Sep m_sep; bool m_add_sep = false; }; const std::string MyStream::endl = std::string("\n"); int main() { MyStream stream; stream << "hello" << "world" << MyStream::endl; // prints "helloworld" stream << MyStream::Sep(", "); stream << "hello" << "world" << MyStream::endl; // prints "hello, world" stream << 1 << 2; stream << 3 << MyStream::endl; // both lines prints "1, 2, 3" stream << MyStream::Sep(); stream << 1 << 2 << 3 << MyStream::endl; // prints "123" return 0; }

Related questions

0 votes
    I am trying to understand the usage of private const in the class. My understanding is that private const is used to make ... (void) { int const MyExample::x = 3; std::cout...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    Im using libVLC to playback video in my application, to open local files im using: libvlc_media_new_path(path_to_file ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    Ok. I'm working with the FW1FontWrapper code for use with DirectX : https://archive.codeplex.com/?p=fw1 This has removed ... hex back onto a UNIT32 UINT32 color = 0xFF + (ucolorb...
asked May 1, 2022 in Education by JackTerrance
0 votes
    Ok. I'm working with the FW1FontWrapper code for use with DirectX : https://archive.codeplex.com/?p=fw1 This has removed ... hex back onto a UNIT32 UINT32 color = 0xFF + (ucolorb...
asked May 1, 2022 in Education by JackTerrance
0 votes
    Ok. I'm working with the FW1FontWrapper code for use with DirectX : https://archive.codeplex.com/?p=fw1 This has removed ... hex back onto a UNIT32 UINT32 color = 0xFF + (ucolorb...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    The _______ allows an application to act as a stream processor, consuming an input stream from one or more topics and ... API (2)Consumer API (3)Connector API (4)Producer API...
asked Jun 16, 2021 in Technology by JackTerrance
0 votes
    // merge.cpp // merges two containers into a third #include #include //for merge() using namespace std; int src1[] = { 2, 3, 4, ... , src1+5, src2, src2+3, dest); for(int j=0; j...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    Sorry for what may sound simple, but I am trying to draw just a simple box in Visual Studio 2017 using the unicode ... code below #include using namespace std; int main() { cout...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I have a struct X with two 64-bit integer members, and a constructor: struct X { X(uint64_t a, ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    I have a struct X with two 64-bit integer members, and a constructor: struct X { X(uint64_t a, ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    I have a struct X with two 64-bit integer members, and a constructor: struct X { X(uint64_t a, ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    I have a struct X with two 64-bit integer members, and a constructor: struct X { X(uint64_t a, ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    I have a struct X with two 64-bit integer members, and a constructor: struct X { X(uint64_t a, ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 10, 2022 in Education by JackTerrance
0 votes
    I want to execute a certain batch file and redirect its console output to a text control in visual c ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
...