繁体   English   中英

字符串 c++ == 比较

[英]string c++ == comparing

    char words[5] = { 't', 'd' , 'o', 'g', 'i', };
    cout << "enter letter ";


    char dogsearch[3] = { 'd', 'o', 'g' };

    if (words == dogsearch)
        cout << "found words";
    else
        cout << "Not found word";

我有两个数组并将它们相互比较以找到彼此之间的相似性和差异,如果有相似性,我想输出它是如何的,反之亦然

您可以使用 string::find 查找子字符串匹配项

char words[5] = { 't', 'd' , 'o', 'g', 'i', };
char dogsearch[3] = { 'd', 'o', 'g' };

string words_str(words, sizeof(words));
string needle(dogsearch, sizeof(dogsearch));

size_t pos = words_str.find(needle);
if (pos != std::string::npos)
   cout << "found words at position " << pos << '\n';
else
   cout << "Not found word;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM