std::string::find vs std::find

Hi All:

I have a large number of elements to be stored into vector or basic string(delimited by \n). I need to search for an element and I would like to know what is faster:

A:
1
2
std::string str = "\nElement1\nElement2...element1000\n"
str.find("\nElement3\n");


B:
1
2
3
std::vector<string> v;
v.push_back("Element1"); v.push_back("Element2");  ....
std::find(v.begin(), v.end(), "Element3") != v.end() )


I'm only interested in search time, I don't need to access any of the elements for reading, modifying or removing.

Thanks a lot,
Petry
Last edited on
I have just tested the difference.

Filesize: 8mb
Lines: 700.000
Average line length: 15 characters

Time to fill:
vector: 3843ms
string: 3891ms

The search time for the last unique element is:
vector: 218
string: 172
In general, the class-specific find functions should be no worse than std::find, which is linear.
If speed was of the essence, A cannot be worse than be. But your B could potentially be made faster by reserving the correct number of elements prior to doing any of the push_backs.

Topic archived. No new replies allowed.