2017-03-12 7 views
-1

bimapというバイナリファイルには実際には大きすぎる(1億8000万~3000万エントリ)書き込み方法を知りたいと思います。 bimapを作成するには、次のコードを用意します。ここでは、2つのストリームを作成してバイナリデータを読み込んで読み込みます。また、要素を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; 
} 
関連する問題