将键值对文件读取到std :: map中 [英] reading a file of key-value pairs in to a std::map

查看:114
本文介绍了将键值对文件读取到std :: map中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Visual Studio 2008 C ++ 03项目,我想在其中读取键值对文件到std :: map中. 为此,我创建了一个istreambuf_pair_iterator,如下所示:

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;
}

一旦我有一个从文件中读取的字符串制成的std::pair<>,我该在哪里存储它以便std::inserter可以将其插入到std::map中?

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?

推荐答案

为什么将boost用于C ++ std可以实现的任务? 只需将istream_iterator与insert_iterator结合使用即可.为此,您必须在pair<string,string>的std名称空间流<<和'>>'运算符中定义.像这样:

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;
}
}

以及用法:

std插入迭代器:

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

std istream迭代器:

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);

阅读:

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

写作(奖励)

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

在ideone上的工作示例

这篇关于将键值对文件读取到std :: map中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆