2011-10-26 20 views
2

我试图检查在尽可能少的线路尽可能例如数组中是否存在字符串:什么是Javascript的“in”的C++等价物?

string foo[] = {blah,blahhh,blahhhh} 
string bar = "blah"; 

if (bar in foo){ 
cout << "true"; 
}else { 
cout << "false"; 
} 
+1

已使标题更具描述性。尽管如果我的语言错了,请随时纠正它。 –

+1

**没有。**部分原因是因为JavaScript的'in'没有做你认为它正在做的事情,正如它被证明*使用错误* ;-)问什么是真正需要的* - 没有必要带来另一种语言。我要重新提出这个问题,但我必须*抛出它并重新开始。 – 2011-10-26 00:38:39

+0

好地方。 'in'寻找键,而不是数值。 –

回答

2

在C++中没有这个关键字和这个构造。 你必须做一个循环或使用查找功能。

http://www.cplusplus.com/reference/algorithm/find/

对于试图做同样的事情(因为是一个问题很多人问我在过去的)其他人:字符数组和字符串(即是为const char数组)不能与平等或与内置运算符相比:比较两个char文字与比较两个指针相同。 但是,您可以使用std :: string类型或使用旧的C风格函数(如strcmp)。

如果您真的想避免编写太多的代码行,只需编写更多行的另一个函数并用一行来调用它即可。然后,您可以把它放在一个漂亮的头文件,包括它时,你需要:)

#include <iostream> 
#include <string> 

using namespace std; 

template<typename T, size_t N> 
inline bool arraycontains(const T (&array)[N], const T& value) 
{ 
    for (size_t i = 0; i < N; ++i) 
     if (array[i] == value) 
      return true; 
    return false; 
} 

int main() 
{ 
    string foo[] = { "blah", "blahhh", "blahhhh" }; 
    string bar = "blah"; 

    cout << (arraycontains(foo, bar) ? "true" : "false"); 
    return 0; 
} 

如果改为数组中有你应该使用函数指针,其中u可以通过大小作为参数。

现在,如果你的数组真的很大,我不认为你想在数组中使用O(n)线性搜索,那么我知道你更喜欢使用std :: map或std :: hash_map(注意hash_map在所有版本的STL中都不可用)。

std :: map在内部使用红黑树(或AVL树,取决于实现),所以查找操作是O(log n)最坏的情况,比O(n)快。

的std ::代替了hash_map内部使用的哈希表,给人一种最坏情况复杂度为O(N),但Ø的平均复杂性(1),很快在现实世界中的应用。

+0

如果您已经在使用模板,为什么不推导数组大小呢?模板已经是内联的;让编译器弄清楚。''template bool arraycontains(const T(&array)[N],const T&value){}' –

+0

哦,请不要传播这个可怕的'cin.get()'hack!诱骗周围环境行为不符合要求并不是该计划的任务。 –

+0

感谢您的评论!按照建议更正! –

0

无需操作。你必须使用一个函数来查找它。要么自己写(通过你的C风格数组的成员),要么使用STL容器和algorithms

+0

...或者标准库的等价物,就像你链接到的那些! –

0

使用for循环,找到字符串时使用break语句。

2

没有内置运营商。 C++是比较级低的比,也就是说,JavaScript,因此你必须建立这些东西你自己。

幸运的是,标准库提供与做这项工作的算法。

使用C++11 ranges获得您的C数组的起点和终点:

std::string foo[] = {"lol", "stack", "overflow"}; 
std::string bar = "stack"; 

if (std::find(std::begin(foo), std::end(foo), bar) != std::end(foo)) 
    found(); 
else 
    not_found(); 

如果没有这些范围,手动提供数组的起点和终点:

std::string foo[] = {"lol", "stack", "overflow"}; 
std::string bar = "stack"; 

if (std::find(foo, foo+3, bar) != foo+3) 
    found(); 
else 
    not_found(); 

或者,更好然后,使用一个矢量(我已经在这里使用C++ 11初始化)

std::vector<std::string> foo{"lol", "stack", "overflow"}; 
std::string bar = "stack"; 

if (std::find(foo.begin(), foo.end(), bar) != foo.end()) 
    found(); 
else 
    not_found(); 

^我相信这也适用于std::array/boost::array,这是您的C样式数组的更直接的模拟。

0

您应该使用std::find,这是简单的使用标准容器,但工作不少用普通阵列:

string *end = foo + sizeof foo/sizeof *foo; 
if (std::find(foo, end, bar) != end) { 
    std::cout << "found"; 
} else { 
    std::cout << "not found"; 
} 

如果您使用的是正确的容器中,如std::vector,你可以使用:

if (std::find(foo.begin(), foo.end(), bar) != foo.end()) 

其去除一点从阵列问题的魔法。

0

C++缺乏像这样的运营商;你会做这样的事情:

bool containsBar = false; 
for(int i = 0; i < 3; i++) { 
    if(foo[i] == bar) { // need to use strcmp or something here, just put == for simplicity. 
     cout << "true"; 
     containsBar = true; 
     break; 
    } 
} 
if(!containsBar) 
    cout << "false"; 
+0

**注意:**直接模拟会要求您在成功条件下“休息”。 –

+0

休息不是真的需要,但当然它总是很好的性能的原因。在酒吧是一个巨大阵列中的第一个元素的可能性,你可以节省很多时间。我将它添加到帖子中。 – CuddleBunny

+1

需要维护OP程序的语义,其中'cout <<“true”'只在执行多次匹配时执行一次。 –

0

在C和C++是不是真的有一个特定的关键字为“中”

可能是最简单的方法是做一个循环这样

(?)
bool isInsideArray = false; //by default isInsideArray is false 
for (int counter = 0;counter>(sizeof(foo)/sizeof(foo[0]));counter++) //find the amount of elements in the array by finding the size of the whole array divided by the size of one element 
{ 
    if (foo[counter] == bar) // check if bar is equal to the current foo element 
    { 
    isInsideArray = true; //set isInsideArray to true 
    break; // break from the loop so we don't do any unnessecary procceessing 
    } 
} 
if(isInsideArray) // if bar was encountered inside foo 
{ 
    //code to be done if bar is inside foo 
} 
else // isInsideArray equals false 
{ 
    //otherwise 
} 

此遍历阵列,并检查它是否等于禁止的每个元素,如果如果该标志被设置为true做 此(referrin所以设置一个标志为真并退出

克至if语句中的代码)

否则这样做(指else语句内码)

这是(从我所知道的......最简单的方法)

希望这有助于

+0

字符串对象在数组中的大小始终相同,因此您可以使用sizeof(foo)/ sizeof(foo [0])? – CuddleBunny

+0

@CuddleBunny,你是什么意思......这正是我的代码 – luckyl

+0

我的意思是如果你有一个数组{“dog”,“pickle”,“cat”},如果你得到sizeof“dog”如果“腌菜”更大? – CuddleBunny

相关问题