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.
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::vector
which is know to be the fastest container. Just use push_back
and access the value with at(index)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With