यहां कुछ समाधान दिए गए हैं जो काम करेंगे यदि आप शब्दों के बीच किसी भी अंतर और/या अन्य सफेद स्थान को फेंकने के इच्छुक हैं।
पहला दृष्टिकोण, जो सबसे सरल है, पाठ को istringstream
में पढ़ना होगा और स्ट्रीम से शब्दों को निकालना होगा। प्रत्येक शब्द को प्रिंट करने से पहले, यह देखने के लिए जांचें कि क्या शब्द वर्तमान रेखा पर फिट होगा या नहीं, अगर यह नहीं होगा तो एक नई लाइन प्रिंट करें। यह विशेष कार्यान्वयन अधिकतम पंक्ति लंबाई से अधिक लंबे समय तक शब्दों को संभाल नहीं पाएगा, लेकिन लंबे शब्दों को विभाजित करने के लिए इसे संशोधित करना मुश्किल नहीं होगा।
#include <iostream>
#include <sstream>
#include <string>
int main() {
const unsigned max_line_length(40);
const std::string line_prefix(" ");
const std::string text(
"Friends, Romans, countrymen, lend me your ears; I come to bury Caesar,"
" not to praise him. The evil that men do lives after them; The good "
"is oft interred with their bones; So let it be with Caesar.");
std::istringstream text_iss(text);
std::string word;
unsigned characters_written = 0;
std::cout << line_prefix;
while (text_iss >> word) {
if (word.size() + characters_written > max_line_length) {
std::cout << "\n" << line_prefix;
characters_written = 0;
}
std::cout << word << " ";
characters_written += word.size() + 1;
}
std::cout << std::endl;
}
एक दूसरा, अधिक "उन्नत" विकल्प, एक कस्टम ostream_iterator
कि लाइनों स्वरूपों के रूप में आप उम्मीद कर उन्हें प्रारूप तैयार किया जाता करने के लिए लिखने के लिए होगा। मैंने इसे "मजेदार स्वरूपण" के लिए ff_ostream_iterator
नाम दिया है, लेकिन यदि आप इसका उपयोग करना चाहते हैं तो आप इसे और अधिक उचित नाम दे सकते हैं। यह कार्यान्वयन सही शब्दों को सही ढंग से विभाजित करता है।
इटरेटर कार्यान्वयन थोड़ा जटिल है, उपयोग काफी सीधा है:
int main() {
const std::string text(
"Friends, Romans, countrymen, lend me your ears; I come to bury Caesar,"
" not to praise him. The evil that men do lives after them; The good "
"is oft interred with their bones; So let it be with Caesar. ReallyLong"
"WordThatWontFitOnOneLineBecauseItIsSoFreakinLongSeriouslyHowLongIsThis"
"Word");
std::cout << " ========================================" << std::endl;
std::copy(text.begin(), text.end(),
ff_ostream_iterator(std::cerr, " ", 40));
}
इटरेटर के वास्तविक क्रियान्वयन इस प्रकार है:
#include <cctype>
#include <iostream>
#include <iterator>
#include <memory>
#include <sstream>
#include <string>
class ff_ostream_iterator
: public std::iterator<std::output_iterator_tag, char, void, void, void>
{
public:
ff_ostream_iterator() { }
ff_ostream_iterator(std::ostream& os,
std::string line_prefix,
unsigned max_line_length)
: os_(&os),
line_prefix_(line_prefix),
max_line_length_(max_line_length),
current_line_length_(),
active_instance_(new ff_ostream_iterator*(this))
{
*os_ << line_prefix;
}
~ff_ostream_iterator() {
if (*active_instance_ == this)
insert_word();
}
ff_ostream_iterator& operator=(char c) {
*active_instance_ = this;
if (std::isspace(c)) {
if (word_buffer_.size() > 0) {
insert_word();
}
}
else {
word_buffer_.push_back(c);
}
return *this;
}
ff_ostream_iterator& operator*() { return *this; }
ff_ostream_iterator& operator++() { return *this; }
ff_ostream_iterator operator++(int) { return *this; }
private:
void insert_word() {
if (word_buffer_.size() == 0)
return;
if (word_buffer_.size() + current_line_length_ <= max_line_length_) {
write_word(word_buffer_);
}
else {
*os_ << '\n' << line_prefix_;
if (word_buffer_.size() <= max_line_length_) {
current_line_length_ = 0;
write_word(word_buffer_);
}
else {
for (unsigned i(0);i<word_buffer_.size();i+=max_line_length_)
{
current_line_length_ = 0;
write_word(word_buffer_.substr(i, max_line_length_));
if (current_line_length_ == max_line_length_) {
*os_ << '\n' << line_prefix_;
}
}
}
}
word_buffer_ = "";
}
void write_word(const std::string& word) {
*os_ << word;
current_line_length_ += word.size();
if (current_line_length_ != max_line_length_) {
*os_ << ' ';
++current_line_length_;
}
}
std::ostream* os_;
std::string word_buffer_;
std::string line_prefix_;
unsigned max_line_length_;
unsigned current_line_length_;
std::shared_ptr<ff_ostream_iterator*> active_instance_;
};
[आप कॉपी और पेस्ट करेंगे, तो कोड स्निपेट और main
इसके ऊपर से, यह संकलित और चलाया जाना चाहिए यदि आपका कंपाइलर C++ 0x std::shared_ptr
का समर्थन करता है; आप की जगह ले सकता है कि boost::shared_ptr
या std::tr1::shared_ptr
के साथ अपने संकलक अभी तक C++ 0x समर्थन नहीं है यदि।]
क्योंकि iterators copyable होना जरूरी यह दृष्टिकोण थोड़ा मुश्किल है और हम यह सुनिश्चित करना होगा कि किसी भी शेष बफ़र पाठ केवल एक बार मुद्रित है। हम इस तथ्य पर निर्भर करते हुए ऐसा करते हैं कि किसी भी समय आउटपुट इटरेटर को लिखा जाता है, इसकी कोई प्रतियां अब उपयोग योग्य नहीं होती हैं।
आपने अभी तक क्या प्रयास किया है? क्या आपने वह कोड लिखा है जो दिए गए पाठ को पढ़ता है? –
मैंने std :: setw() का उपयोग करने का प्रयास किया है। मैंने कोड लिखा है जो दिए गए पाठ को पढ़ता है। मैं यह देखने की कोशिश कर रहा हूं कि प्रत्येक पंक्ति को स्वचालित रूप से पैड करने के लिए एक आसान तरीका है (लाइनवैपिंग से उत्पादित लाइनों सहित) वर्णों की एक निश्चित संख्या के साथ। –
मुझे संभवतः होमवर्क टैग का उपयोग नहीं करना चाहिए था - मैं होमवर्क असाइनमेंट पर काम कर रहा हूं, लेकिन असाइनमेंट आउटपुट के लिए प्रयुक्त प्रारूपण से असंबंधित नहीं है। मैं सिर्फ उत्सुक था कि यह स्वरूपण कैसे पूरा किया जा सकता है। –