Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8243387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:30:08+00:00 2026-06-07T21:30:08+00:00

I want to provide a generic line based IO for containers of std::string in

  • 0

I want to provide a generic line based IO for containers of std::string in a library.
Line based since the strings may contain spaces.
The following code seems to work fine but I’m not sure whether this is the best way to go or whether it creates some ambiguities, I’m failing to grasp.

#define boostForeach BOOST_FOREACH

template< template<typename ELEM, typename ALLOC=std::allocator<ELEM> > class Container >
std::ostream& operator<< (std::ostream& o, Container<std::string>const & container){
  boostForeach(std::string const& str, container) {
    o << str << "\n";
  }
  return o;
}

template< template<typename ELEM, typename ALLOC=std::allocator<ELEM> > class Container >
std::istream& operator>> (std::istream& in, Container<std::string>& container){
  container.clear();
  std::string buf;
  while(getline(in, buf)) {
    if(buf.empty()) break; //stop if empty line found to separate map from other data
    container.insert(container.end(),buf);
  }
  return in;
}

So the question is: Is this safe and sound ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T21:30:11+00:00Added an answer on June 7, 2026 at 9:30 pm

    You can write the output algorithm using std::copy():

    std::copy(container.begin(),
        container.end(),
        std::ostream_iterator<std::string>(o, "\n"));
    

    You might use an input iterator for paragraph input, i.e., multiple lines separated by blank lines:

    class istream_paragraph_iterator: public std::iterator<std::forward_iterator_tag,std::string>{
        std::istream* stream;
        std::string line;
    public:
    
        istream_paragraph_iterator() : stream(0) {}
    
        istream_paragraph_iterator(std::istream& stream) : stream(&stream) {++*this; //get the first element
        }
    
        std::string operator*() const {
            return line;
        }
    
        const std::string* operator->() const {
            return &line;
        }
    
        istream_paragraph_iterator& operator++() {
            if (stream && (!std::getline(*stream, line) || line.empty()))
                stream = 0;
            return *this;
        }
    
        istream_paragraph_iterator operator++(int) {
            istream_paragraph_iterator previous(*this);
            ++*this;
            return previous;
        }
    
        bool operator==(const istream_paragraph_iterator& other) const {
            return stream == other.stream;
        }
    
        bool operator!=(const istream_paragraph_iterator& other) const {
            return !(*this == other);
        }
    
    };
    

    Then you can write the input algorithm using std::copy() as well:

    std::copy(istream_paragraph_iterator(in),
        istream_paragraph_iterator(),
        std::back_inserter(container));
    

    Separating the logic into an iterator type lets you make the input and output algorithms parameterisable, and thus more general. In a template library, that’s typically a good thing. I would avoid adding overloads for standard containers, because you cannot know that they do the right thing on every platform; iterator-based algorithms are more portable, and you don’t have to write all the template<template<...> class ...> cruft.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want sell some .NET library and I want provide edition with full source
For our SWT application I want to provide a generic Linux download bundle which
I want to provide a templated function that converts most basic types to string.
I want to provide my user with some meaningful error messages when network requests
I want to provide my colleagues with an interface (using Windows Forms or WPF)
I want to provide the ability to save some rendered data/charts (with the titles
I want to provide a custom Batcher for Hibernate to use (for this reason:
I want to provide for partial matching, so I am tacking on * to
I want to provide a simple XHTML representation of each of the resources in
I want to provide a virtual webcam via DirectShow that will use the video

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.