2017-03-12 74 views
-1

我想知道如何写一个bimap这实际上是一个二进制文件(1.8亿到三千万条目),然后读取做一些操作。要创建一个bimap我有以下代码,我创建了两个流来写入和读取二进制数据。我也将这些元素插入bimap写一个bimap到二进制文件,然后读取它

#include <string> 
#include <iostream> 
#include <utility> 
#include <fstream> 
#include <boost/bimap.hpp> 
#include <boost/bimap/unordered_set_of.hpp> 
#include <boost/bimap/unordered_multiset_of.hpp> 

namespace bimaps = boost::bimaps; 
typedef boost::bimap<bimaps::unordered_set_of<unsigned long long int>, 
     bimaps::unordered_multiset_of<unsigned long long int > > bimap_reference; 
typedef bimap_reference::value_type position; 
bimap_reference numbers; 

int main() 
{ 
    std::ofstream outfile ("bmap",std::ofstream::binary); 
    std::ifstream infile ("bmap",std::ifstream::binary); 

    numbers.insert(position(123456, 100000)); 
    numbers.insert(position(234567, 80000)); 
    numbers.insert(position(345678, 100000)); 
    numbers.insert(position(456789, 80000)); 

    //want to write the file 

    //want to read the file 


    // So that I can perform the following operation 
    using ritr = bimap_reference::right_const_iterator; 
    std::pair<ritr, ritr> range = numbers.right.equal_range(80000); 
    auto itr = range.first; 
    std::cout<<"first: "<<itr->first<<std::endl; 
    if(itr != numbers.right.end() && itr->second ==80000){ 
     for (itr = range.first; itr != range.second; ++itr) 
     { 
      std::cout<<"numbers:"<<itr->second<<"<->"<<itr->first<<std::endl; 
     } 
    } 
    else { 
     std::cout<<"Not found:"<<std::endl; 
    } 
    return 0; 
} 

我想写bimap,然后再读一遍执行一些操作。怎么做。

+0

请看['boost :: archive'](http://www.boost.org/doc/libs/1_39_0/libs/serialization/doc/archives.html)。 –

+0

使用适当的DBMS创建适当的数据库。 –

+0

@πάνταῥεῖ谢谢,看看 – AwaitedOne

回答

1

要处理bimap写入/从二进制文件读取,boost serialization是非常有帮助的。您需要包含

#include <boost/archive/binary_oarchive.hpp> 
#include <boost/archive/binary_iarchive.hpp> 

作为头文件。然后,您需要拥有用于写入和读取的文件流,并使用boost::archive::binary_oarchive来编写,然后使用boost::archive::binary_iarchive进行回读。还请确保使用-lboost_serialization编译代码。完整的代码如下。

#include <string> 
#include <iostream> 
#include <utility> 
#include <fstream> 
#include <boost/bimap.hpp> 
#include <boost/bimap/unordered_set_of.hpp> 
#include <boost/bimap/unordered_multiset_of.hpp> 
#include <boost/archive/binary_oarchive.hpp> 
#include <boost/archive/binary_iarchive.hpp> 



namespace bimaps = boost::bimaps; 
typedef boost::bimap<bimaps::unordered_set_of<unsigned long long int>, 
     bimaps::unordered_multiset_of<unsigned long long int > > bimap_reference; 
typedef bimap_reference::value_type position; 
bimap_reference numbers; 

int main() 
{ 

    // insert elements into bimap and write to a binary file 
    { 
     numbers.insert(position(123456, 100000)); 
     numbers.insert(position(234567, 80000)); 
     numbers.insert(position(345678, 100000)); 
     numbers.insert(position(456789, 80000)); 

     std::ofstream ofs("data"); 
     boost::archive::binary_oarchive oa(ofs); 
     oa << const_cast<const bimap_reference&>(numbers); 
     const bimap_reference::left_iterator left_iter = numbers.left.find(123456); 
     oa << left_iter; 
     const bimap_reference::right_iterator right_iter = numbers.right.find(100000); 
     oa << right_iter; 
    } 

    // load the bimap back to memory 
    { 
     std::ifstream ifs("data", std::ios::binary); 
     boost::archive::binary_iarchive ia(ifs); 
     ia >> numbers; 
     assert(numbers.size() == 4); // to throw an error 
     bimap_reference::left_iterator left_iter; 
     ia >> left_iter; 
     assert(left_iter->first == 123456); 
     bimap_reference::right_iterator right_iter; 
     ia >> right_iter; 
     assert(right_iter->first == 100000); 
    } 

    // then perform the following operation 
    using ritr = bimap_reference::right_const_iterator; 
    std::pair<ritr, ritr> range = numbers.right.equal_range(80000); 
    auto itr = range.first; 
    std::cout<<"first: "<<itr->first<< " <-> " << itr->second<<std::endl; 
    if(itr != numbers.right.end() && itr->first ==80000){ 
     for (itr = range.first; itr != range.second; ++itr) 
     { 
      std::cout<<"numbers:"<<itr->second<<"<->"<<itr->first<<std::endl; 
     } 
    } 
    else { 
     std::cout<<"Not found:"<<std::endl; 
    } 
    return 0; 
} 
相关问题