Need Help with map.

I have a txt file with 150 000 words and insert them to a map, thats not problem, but every time when i insert them take a 3,4 second to do this.I know takes time to adjust the words but my question: is there a way to save map serialization in a file then just open it.Just search a fast way to open it.
i am not sure about time optimization for insertion, but for search optimization u can use a hash map, instead of a normal map, tradeoff can be done based on what operation ull be dng more frequently, if reading the map/fetching values from the map is the answer go for Hash map.
Can you show code for map insertion and provide a link to file?
map<string,string> diction;

ifstream wordlist("example.txt");
string declaration;

for(int i=1;i<150000;i++)

diction[declaration] = declaration; // first i write with insert function but has no difference in time
Why do you map string to itself? Upload file somewhere to test it.

Also you can probably use range-insert which will be faster.
Why not use std::set? Will be faster than std::map. You're storing each word twice by using std::map.

If your file ordered? If so, as MiiNiPaa suggested, using range insert would be significantly faster.
iterator insert (iterator position, const value_type& val);
Where position is the iterator returned by the previous insert.

You might also consider std::unordered_set.
Last edited on
You would have to measure it on your implementation, but something like this ought to be the fastest:
#include <unordered_set>
#include <fstream>
#include <string>
#include <iterator>

std::unordered_set<std::string> from_file( const std::string& path )
    std::ifstream file(path) ;
    return { std::istream_iterator<std::string>(file),
              std::istream_iterator<std::string>() } ;

I try with range insert and its work. Start in 2 seconds.Thank you.
Topic archived. No new replies allowed.