查找字符串是否包含 C++ 中的字符(允许提升)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14028343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find if a string contains a character in C++ (boost allowed)
提问by psyche
Suppose I have a string and I want to find whether a specific character (like '|') is present or not, what is the best and fastest technique to do so? I know string find implementation.I am asking for even faster implementation than this one.
假设我有一个字符串并且我想查找特定字符(如“|”)是否存在,那么最好和最快的技术是什么?我知道字符串查找实现。我要求比这更快的实现。
回答by Fred Larson
if (str.find('|') != std::string::npos)
{
// ...
}
There's unlikely to be anything more efficient. O(n) is the best you can do. The standard library implementation should be pretty much optimal.
不太可能有更有效的方法。O(n) 是你能做的最好的。标准库实现应该是非常优化的。
回答by Mouze
From this sourceempirical test done with Visual Studio 2013 Compiler shows that strchrroutine is about 2x fasterthan std::string::findimplementation.
从使用 Visual Studio 2013 Compiler 完成的这个源经验测试表明,strchr例程比std::string::find实现快大约2 倍。
回答by afakih
Another way is to use the strchr function on the corresponding c_str string:
另一种方法是在相应的 c_str 字符串上使用 strchr 函数:
if(strchr(str.c_str(), '|'))
{
\found
}
Not sure how it compares to std find in terms of speed though...
不知道它在速度方面与 std find 相比如何......
The position of the found character is
找到的字符的位置是
size_t pos = strchr(str.c_str(),'|') - str.c_str();
回答by Simon Schmei?er
Adding on the answer of Tom Tanner. If you don't want to do any a priori calculations you will be stuck at O(n), ie there is a linear correlation between the length of the string you are searching in and the time consumption. Tom suggested to set up an array (or vector) of booleans that indicate whether a certain character occurred. It would need O(n) once to index the string, but then you can check for any number of characters in O(1) (constant time) if it is included. The downside with this approach is that you will need a lot of memory (once you decide you need to support unicode).
添加汤姆·坦纳的答案。如果您不想进行任何先验计算,您将陷入 O(n),即您搜索的字符串长度与时间消耗之间存在线性相关性。Tom 建议设置一个布尔值数组(或向量)来指示某个字符是否出现。它需要 O(n) 一次来索引字符串,但是如果包含它,您可以检查 O(1) (恒定时间)中的任意数量的字符。这种方法的缺点是您将需要大量内存(一旦您决定需要支持 unicode)。
As a compromise you could use a std::set or similar, storing only the characters that actually exist in your input string. Memory consumption would then be around linear with regard to the number of different characters in the string but lookup would be O(log n), ie logarithmic in time.
作为妥协,您可以使用 std::set 或类似的,只存储输入字符串中实际存在的字符。内存消耗将与字符串中不同字符的数量呈线性关系,但查找将是 O(log n),即时间对数。
Of course you should measure/profile and then explain here what use case you are actually optimizing for. Until you have done so, stick with what is easiest to understand and read.
当然,您应该测量/分析,然后在此处解释您实际优化的用例。在您这样做之前,请坚持使用最容易理解和阅读的内容。
回答by Rontogiannis Aristofanis
There is only one way to do this. Just iterate over the string to check if the character you are seeking exists. You can do this by using the string::find
function, which gets a character and returns the first position that it occurs in the string, or string::npos
if the value is not present. You could also use std::find
, which gets two iterators, begin
and end
and a key value 'k' and returns an iterator pointing at the first occurrence of k in the range [begin, end]
, or end
if k
wasn't found. And of course, you can implement the find function by yourself, like this:
只有一种方法可以做到这一点。只需遍历字符串以检查您正在寻找的字符是否存在。您可以使用该string::find
函数执行此操作,该函数获取一个字符并返回它在字符串中出现的第一个位置,或者string::npos
如果该值不存在。你也可以使用std::find
,它可以获取两个迭代器,begin
并end
和一个键值“K”,并返回k的范围内第一次出现指向一个迭代器[begin, end]
,或者end
如果k
没有被发现。当然,你也可以自己实现 find 函数,像这样:
string::size_type pos=string::npos;
for(string::size_type i=0; i<s.size(); ++i) {
if(s[i] == key) {
pos=i;
break;
}
}
if(pos != string::npos) {
// key was found
} else {
// not found
}
or this:
或这个:
string::iterator pos=s.end();
for(string::iterator i=s.begin(); i!=s.end(); ++i) {
if(*i == key) {
pos=i;
break;
}
}
if(pos != s.end()) {
// found
} else {
// not found
}
More about the std::string::find
and std::find
:
更多关于std::string::find
和std::find
:
回答by Tom Tanner
Given your statement that you want something faster than string::find, the only thing I can think of would be to create a class which had highly customised assignment operators which on every update of the string updated an internal table which contained the first position in the string of every possible character (256 for a char string, 65536(?) for a wide string). This has O(1) lookup at the expense of quite a lot of complexity added to non-const operations.
鉴于您声明您想要比 string::find 更快的东西,我唯一能想到的就是创建一个具有高度自定义赋值运算符的类,该类在每次更新字符串时都会更新一个包含第一个位置的内部表每个可能字符的字符串(字符字符串为 256,宽字符串为 65536(?))。这具有 O(1) 查找,但代价是增加了非常量操作的大量复杂性。
回答by HappyTran
You can try this:
你可以试试这个:
string s1 = "Hello";
string s2 = "el";
if(strstr(s1.c_str(),s2.c_str()))
{
cout << " S1 Contains S2";
}