| | |
std:string::find vs std::find
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2009
Posts: 2
Reputation:
Solved Threads: 0
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: B:
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
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:
C++ Syntax (Toggle Plain Text)
std::string str = "\nElement1\nElement2...element1000\n" str.find("\nElement3\n");
C++ Syntax (Toggle Plain Text)
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 by petry; Jul 5th, 2009 at 5:11 am.
•
•
Join Date: Jun 2006
Posts: 147
Reputation:
Solved Threads: 20
•
•
•
•
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:B:C++ Syntax (Toggle Plain Text)
std::string str = "\nElement1\nElement2...element1000\n" str.find("\nElement3\n");C++ Syntax (Toggle Plain Text)
std::vector<string> v; v.pushback("Element1"); v.pushback("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
•
•
•
•
Rabin-Carp
Boyer- more
etc
as far as vector is concern again the complexity does matter.
if the size of the string is N elements(ignoring the string comparision cost). its O(N)...
its upto the implementation in which you are about to use .., because vector is more managed then the string which is delimited by some character(in your case \n).
what if you write the modified algorithm that meets your need, like binary search for vector container. or use some other container like BST or hash tables...
or use the STL binary_search with the predicate on string comparision.
Hope this helps
Last edited by Laiq Ahmed; Jul 5th, 2009 at 5:26 am. Reason: forgot STL binary_search
•
•
Join Date: Dec 2007
Posts: 360
Reputation:
Solved Threads: 69
If a STL container offers a specialized algorithm, use that one. Normally it is the one with the better performance, i.e. std::list::sort vs. std::sort
Last edited by jencas; Jul 6th, 2009 at 5:24 am.
If you are forced to reinvent the wheel at least try to invent a better one!
Please use code tags - Please mark solved threads as solved
Please use code tags - Please mark solved threads as solved
![]() |
Similar Threads
- LPWSTR to std::string help! (C++)
- Converting std::string to System::String^ NEED HELP (C++)
- Extract string and int from std::string (C++)
- HELP Plz:Memory Leak in std::string (C++)
- int to std::string (C++)
- no matching function for call to 'strcmp(std::string&, std::string&)' (C++)
- C++ - std::string input (C++)
Other Threads in the C++ Forum
- Previous Thread: Help please
- Next Thread: please help
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





