簡體   English   中英

混合造成的許多錯誤<cstdlib>函數調用<string>職能

[英]Many errors from mixing <cstdlib> function calls with <string> functions

我正在嘗試創建一個程序來計算用戶給出的單詞中元音和輔音的數量。 我使用 strlen() 之類的函數來獲取用戶輸入數組的長度以進行迭代。 我還使用了 bits/stdc++.h,因此我可以為用戶輸入中出現的任何元音調用 count() 函數。

為了檢查元音的出現,我嘗試了幾個函數,從 count 開始,到 find_first_of,到 find(),再到 count()。 我的第一個錯誤是說它無法識別對 strlen() 的調用。 我檢查以確保我包含了正確的包來使用 strlen(),但這似乎不是問題。 我在裝有 High Sierra 的 MacBook Pro 上運行這個程序。

  #include <iostream>//std::cout
  #include <string>//std::string
  #include <cstdlib>
  #include <set>// std::size_t
  #include "/Users/Desktop/stdc++.h"

  using namespace std;

  int main(){
      string input = "hello";
      cout << " The input is " << input << endl;
      std::size_t vowelsFound = 0;

      /*cout << " Enter a word, and we will count the vowels and    consonants: ";
      cin >> input;
      cout << " You entered " << input << endl;*/

      char cons[] =    {'b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','z'};

      char vows[] = {'a','e','i','o','u','y'};

      std::size_t n = strlen(input);
      std::size_t v = strlen(vows);

      for(int i = 0; i < v; i++){
          if(count(input,input + n,vows[i]) != 0){
              vowelsFound += count(input,input + n, vows[i]);//count of vowels in vows[] encountered in input
              cout << " The vowel(s) found is " << vowelsFound << endl;
    }
          else{
              cout << " The vowel " << vows[i] << " is not found in input " << endl;
    }
}

  cout << " The amount of vowels found is " << vowelsFound << endl;
  cout << " The expected amount of vowels found is 2 " << endl;
}

我對輸入的短語“hello”進行了硬編碼,所以當一切都說完並完成后,元音數應該是 2。

您的代碼中有許多問題:

  1. 我不知道包含在"/Users/richardlopez/Desktop/stdc++.h"但包含它不太可能是一件好事。 您可能希望#include <cstring>獲得strlen
  2. 你不能在std::string上使用strlen ,它只適用於空終止字符數組。 您應該只使用input.size()代替。
  3. 你也不應該在vows上使用strlen ,因為它是一個字符數組,所以編譯它不是空終止,所以strlen的返回值是未定義的。 您可以使用sizeof(vows)或只是將vows改為std::stringstd::vector<char>
  4. count(input,input + n,vows[i])不正確。 input + n不編譯。 你大概的意思是count(input.begin(),input.end(),vows[i])

更正上述問題並使用現代 C++ 會產生簡化的代碼:

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

int main()
{
  std::string input = "hello";
  std::cout << " The input is " << input << "\n";
  std::size_t vowelsFound = 0;

  std::string cons = "bcdfghjklmnpqrstvwxyz";
  std::string vows = "aeiou";

  for ( auto v : vows )
  {
      size_t c = std::count(input.begin(), input.end(), v);
      if ( c != 0 )
      {
          vowelsFound += c;
          std::cout << "found " << c << " " << v << "\n";
      }
      else
      {
          std::cout << " The vowel " << v << " is not found in input\n";              
      }
  }

  std::cout << " The amount of vowels found is " << vowelsFound << "\n";
  std::cout << " The expected amount of vowels found is 2\n";
}

如果您只需要元音的總數,您可以使用:

std::cout << " The amount of vowels found is " << std::count_if( input.begin(), input.end(), [&](char c)
  {
    return vows.find(c) != std::string::npos;
  }) << "\n";

幾個問題,在 "/Users/richardlopez/Desktop/stdc++.h" 之上。 如果您需要特定功能,請查看參考並包含該功能的相應官方 C++ 標頭。

第一個strlen用於以 0 結尾的char* 它不適用於input 為此,只需使用:

auto n = input.size();

然后同樣的vows

std::vector<char> vows{'a','e','i','o','u','y'};
auto v = vows.size();

然后count也不同:

std::count(std::begin(input) ,std::end(input), vows[i])

如果您有 C++17,請執行以下操作:

if(auto count = std::count(std::begin(input), std::end(input), vows[i]); count > 0)
{
    vowelsFound += count;
    std::cout << count << std::endl;
}

您可以使用分區算法。

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

int main() {

    std::string input = "hello";
    std::cout << " The input is " << input << std::endl;

    std::vector<char> vows = {'a','e','i','o','u','y'};
    auto it_vows_end = std::partition(input.begin(), input.end(), [vows](char c){return find(vows.begin(), vows.end(), c) != vows.end();});
    std::cout << " The amount of vowels found is " << std::distance(input.begin(), it_vows_end) << std::endl;
    std::cout << " The expected amount of vowels found is 2 " << std::endl;


    // If you also want to count consonants (for example if the input can also have digits or punctuation marks)
    std::vector<char> cons = {'b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','z'};
    auto it_cons_end = std::partition(it_vows_end, input.end(), [cons](char c){return find(cons.begin(), cons.end(), c) != cons.end();});
    std::cout << " The amount of consonants found is " << std::distance(it_vows_end, it_cons_end) << std::endl;

}

輸出:

輸入是你好
找到的元音數量是 2
找到的元音的預期數量是 2
找到的輔音數量是 3


std::size_t 是 sizeof 的返回類型。 所以使用它來迭代容器是很自然的。 此外,由於它是某些庫使用的標准類型,因此它使代碼更易於閱讀。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM