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 8610851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:13:31+00:00 2026-06-12T04:13:31+00:00

I have a Visual Studio 2008 C++03 project where I would like to read

  • 0

I have a Visual Studio 2008 C++03 project where I would like to read a file of key-value pairs in to a std::map.
To do that I’ve created an istreambuf_pair_iterator as below:

typedef std::map< std::string, std::string > Properties;

class istreambuf_pair_iterator : 
    public boost::iterator_adaptor< istreambuf_pair_iterator, 
                                    std::pair< std::string, std::string >*,
                                    boost::use_default, 
                                    boost::forward_traversal_tag >
{
public:
    istreambuf_pair_iterator() : sb_( 0 ) { };
    explicit istreambuf_pair_iterator( std::istream& is ) : sb_( is.rdbuf() ) { };

    private:
    void increment()
    {
        std::string line;
        std::istream is( sb_ );
        std::getline( is, line );

        // TODO: parse the key=value to a std::pair 
        // where do I store the pair???
    };

    friend class boost::iterator_core_access;
    std::streambuf* sb_;
};

Properties ReadProperties( const char* file )
{
    std::ifstream f( file );
    Properties p;
    std::copy( istreambuf_pair_iterator( f ),
               istreambuf_pair_iterator(),
               std::inserter( p, p.end() ) );
    return p;
}

Once I have a std::pair<> made from the string read from the file, where do I store it such that it can be inserted by the std::inserter in to the std::map?

  • 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-12T04:13:33+00:00Added an answer on June 12, 2026 at 4:13 am

    Why using boost for tasks which can be achieved with C++ std stuff?
    Just use istream_iterator with insert_iterator. To do that you must define in std namespace stream << and ‘>>’ operators for your pair<string,string>. Something like this:

    namespace std {
    // I am not happy that I had to put these stream operators in std namespace.
    // I had to because otherwise std iterators cannot find them 
    // - you know this annoying C++ lookup rules...
    // I know one solution is to create new type inter-operable with this pair...
    // Just to lazy to do this - anyone knows workaround?
    istream& operator >> (istream& is, pair<string, string>& ps)
    {
       return is >> ps.first >> ps.second;
    }
    ostream& operator << (ostream& os, const pair<const string, string>& ps)
    {
       return os << ps.first << "==>>" << ps.second;
    }
    }
    

    And the usage:

    std insert iterator:

      std::map<std::string, std::string> mps;
      std::insert_iterator< std::map<std::string, std::string> > mpsi(mps, mps.begin());
    

    std istream iterator:

      const std::istream_iterator<std::pair<std::string,std::string> > eos; 
      std::istream_iterator<std::pair<std::string,std::string> > its (is);
    

    Reading:

      std::copy(its, eos, mpsi);
    

    Writing (bonus):

      std::copy(mps.begin(), mps.end(),   std::ostream_iterator<std::pair<std::string,std::string> >(std::cout, "\n"));
    

    Working example at ideone

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

Sidebar

Related Questions

I have a Visual Studio 2008 C++ project where I would like to copy
I have a Visual Studio 2008 C++03 project where I would like to verify
I have a Visual Studio 2008 C++03 project where I would like to use
I have a Visual Studio 2008 C++ project where I would like to heap
I have a simple unmanaged c++ project in Visual Studio 2008, and would like
I have a Visual Studio 2008 project that produces a file called: Game-Release.exe .
I have a visual studio 2008 project in the directory like C:\Users\espl\Desktop\hello\world\helloworld(.sln file) Now
I have a website project in Visual Studio 2008. I would like to build
I have a Visual Studio 2008 c++03 project where I've come across something like
I have a Visual Studio 2008 C++ project that has support for using multiple

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.