I am interested to get a substring and then convert that into a long int
for further processing. I need to do this for a large number of strings. Currently what I do is using .substr()
, as shown in the following example Test.
// Example program
#include <iostream>
#include <string>
int main()
{
std::string content = "123421341234432231112343212343";
unsigned long int sub = atol(content.substr(0,18).c_str());
std::cout << "sub: " << sub << '\n';
return 0;
}
I want to know the fastest way to do this. It's not always .substr(0,18)
, it can be anything of length 18 (remaining length if not 18) .substr(i,18)
.
Edit:
About the number of strings, roughly 30 million, about fast ( i think to get a substring copy and then converting to long int is a slow process. I want it to be faster than .substr()
method). To be honest, I want it to be as fast as it can be.
Actually, the strings are in a fasta
file which I read each at a time and remove the unwanted content by boost::split() and store the wanted content. Then I need to do different passes of getting different substrings of the string for further processing.