Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inserting std::strings in to a std::map

I have a program that reads data from a file line-by-line. I would like to copy some substring of that line in to a map as below:

std::map< DWORD, std::string > my_map;
DWORD index;         // populated with some data
char buffer[ 1024 ]; // populated with some data
char* element_begin; // points to some location in buffer
char* element_end;   // points to some location in buffer > element_begin

my_map.insert( std::make_pair( index, std::string( element_begin, element_end ) ) );

This std::map<>::insert() operation takes a long time (It doubles the file parsing time). Is there a way to make this a less expensive operation?

Thanks, PaulH

Edit: to be more specific, I want to know that I'm doing the minimum number of copy operations to get the data from the file in to the map.

like image 675
PaulH Avatar asked Mar 16 '10 13:03

PaulH


1 Answers

Do you really need a map here? As far as I can see in your example you only want to store an index as key value that is, as I suppose, simply incremented for each insertion. You could accomplish this with an std::vectorwhich is know to be the fastest container. Just use push_backand access the value with at(index).

like image 115
Simon Linder Avatar answered Sep 29 '22 20:09

Simon Linder