【问题标题】:Compare vector<string> and print yes or no using c++比较 vector<string> 并使用 c++ 打印是或否
【发布时间】:2020-03-28 10:45:16
【问题描述】:

我有两个字符串向量 aba[0]="hello",a[1]="world",b[0]="heyyy"b[1]="namaste" 我必须确定以下 a[0] and b[0] 是否有任何匹配和 a[1] and b[1]任何匹配的东西。如果a[0] and b[0] 有一个或多个字符匹配,则打印"YES",否则打印"NO"。同样,如果a[1] and b[1] 有一个或多个字符匹配,则打印"YES",否则打印"NO"。例如从上面的信息来看,a[0] and b[0]'h' and 'e' 匹配,a[1] and b[1] 没有字符匹配。最后,预期的输出是

"YES" "NO"

基于以上信息,作为 C++ 的初学者,我开发了一个 C++ 程序,它甚至不是部分正确的。如果有人解决这个问题,那就太好了。提前致谢。

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main()
{
 std::vector<string>a,b;
 a={"hello", "world"};
 b={"heyyy", "namaste"};
 const char* acstr;
 const char* bcstr;   
 acstr = a[0].c_str();
 bcstr = b[0].c_str();
 for(int i=0;i<sizeof(acstr);i++)
 {
    for(int j=0;j<sizeof(bcstr);j++)
    {
        if(acstr[i]==bcstr[j])
        {
            cout << "YES";
        }
        else{
            continue;
        }             
    }
 }
  return 0;
}

【问题讨论】:

  • 我会编写一个函数,对两个strings 进行比较并返回一个bool。一旦可行,那么我会担心遍历向量。
  • 为了您的比较,您是否要查看两个字符串 ab 是否有任何等效的子字符串? (例如,s = "axxx"b = "yyyya" 将返回 true,因为它们都包含 a)或者您是否只检查 a[i] == b[i] 是否为 [0, min(a.size(), b.size()) 中的任何 i
  • 你为什么要把std::stringconst char*混在一起? std::string 可以做所有const char* 可以(以及更多)的事情

标签: c++ string vector


【解决方案1】:

无需将 std::string 类型的对象转换为类似指针

acstr = a[0].c_str();

也在这个循环中

for(int i=0;i<sizeof(acstr);i++)

表达式sizeof(acstr) 没有给出指向字符串的长度。它给出了指针本身的大小,取决于所使用的系统,它可以等于 4 或 8 个字节,与指向的字符串的长度无关。

一般来说,向量可以有不同数量的元素。所以你需要在循环中使用最小向量的大小。

要确定字符串中是否存在字符,您可以使用std::string 类的方法find

这是一个演示程序。

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

int main() 
{
    std::vector<std::string> v1 = { "hello", "world" };
    std::vector<std::string> v2 = { "heyyy", "namaste" };

    std::vector<std::string>::size_type n = std::min( v1.size(), v2.size() );

    for ( std::vector<std::string>::size_type i = 0; i < n; i++ )
    {
        std::string::size_type j = 0;
        while ( j < v1[i].size() && v2[i].find( v1[i][j] ) == std::string::npos )
        {
            ++j;
        }

        std::cout << ( j == v1[i].size() ? "NO" : "YES" ) << '\n';
    }

    return 0;
}

它的输出是

YES
NO

【讨论】:

    【解决方案2】:

    也许你想喜欢:

    #include <bits/stdc++.h>  // do not use this header
    using namespace std;
    
    int main()
    {
        std::vector<string>a, b;
        a = { "hello", "world" };
        b = { "heyyy", "namaste" };
        for (int i = 0; i < (int)a.size(); i++)
        {
            bool bFound = false;
            for (int j = 0; j < min(a[i].length(), b[i].length()); j++)
            {
                if (a[i][j] == b[i][j])
                {
                    bFound = true;
                }
            }
            cout << (bFound ? "YES" : "NO") << endl;
        }
        return 0;
    }
    

    一些建议:

    1. 您不必使用c_str() 进行字符比较。
    2. sizeof(const char*) 不会给你字符串的长度。如果需要,请使用 strlen(const char*)

    【讨论】:

      【解决方案3】:
      #include <iostream>
      #include <bits/stdc++.h>
      using namespace std;
      
      int main()
      {
       int flag = 0;
       std::vector<string>a,b;
       a={"hello", "world"};
       b={"heyyy", "namaste"};  
       for(int k=0;k<2;k++){  //You can find and put the length of a or b, instead of using 2
           for(int i=0;i<a[k].length();i++)
           {
              for(int j=0;j<b[k].length();j++)
              {
                  if(a[k][i]==b[k][j])
                  {
                      flag = 1;
                  }            
              }
           }
          if(flag == 1){
               cout << "YES\n";
          }else{
              cout << "NO\n";
          }
          flag = 0;
       }
        return 0;
      }
      

      【讨论】:

      • 你测试过这个吗? sizeof(acstr) 应该是指针的大小,而不是 c 样式字符串的大小。
      • 嘿@JohnFilleau,你可能是对的,因为我只来自 C 背景。但是当我在在线 c++ 编译器中进行测试时,它对我来说效果很好。
      • 在C语言中也是如此。我想说,尤其是有C语言背景的你应该知道如何正确确定字符串的长度。 sizeof(void*) 通常是 4 或 8,所以你可能很幸运,因为所有字符串都少于 8 个字符。但它仍然是错误的,并导致一些越界访问。
      • sizeof 指针肯定是错误的。请更正 strlen 或删除您的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多