2016-11-29 6 views
0

文字列の最小、最大、およびモードを出力するようにタスクを解決する必要があります。最初に文字列をベクトルに格納します。文字列をソートしてから、ベクトルの最初と最後の要素を出力します。これは問題ありません。しかし、私は彼らのモードを見つけるのは難しい時があります。ここに私が書いたコードがあります。文字列を含むベクトルの検索モード

#include<stdafx.h> 
#include<iostream> 
#include<vector> 
#include<string> 
#include<algorithm> 

using namespace std; 

int main() 

{ 
    vector<string>words; 

    cout << "Please enter some words. When finished, just enter 'stop'.\n"; 

    string ant; 
    int count = 0; 
    int max = 0; 

    while (cin >> ant) { 
     if (ant != "stop") { 
      words.push_back(ant); 
          } 
     else 
      break; 
         } 

    sort(words.begin(), words.end()); 

    cout << "The min of the entered words is " << words[0] << "\n"; 
    cout << "The max of the entered words is " << words.back() << "\n"; 



} 

これまでのところこれが良いです。それは私が望むように仕事をします。しかし、文字列のモードを見つけなければならないときに問題が発生します。私は与えられた整数のモードを見つけるためにネット上にいくつかのコードを見つけました。そしてそれは整数のために働きます。私は文字列のためにそれを修正しようとしましたが、動作させることができませんでした。

これは私のエラーの多くを与え
for (string test = 0; test<words.size(); ++test) { 
    if (words[test] == words[test + 1]) { 
     count++; 
             } 
    else if (words[test] != words[test + 1]) { 
     if (count>max) { 
      max = count; 
      mode = words[test]; 
         } 

     count = 0; 

              } 

               } 

が、私はどこを開始するのか分からない:ここではコードです。問題は、これが文字列の反復を行う適切な方法ではないということです。私は文字列の反復についていくつかの説明を見つけましたが、それは私にとってはとても混乱しています。どんな助けもありがとう。

+2

Ahhhh、my eyessss !!!!! – George

+0

あなたはとても近いです! 'string test = 0;'を 'int test = 0; 'に変更すると、ほとんどのエラーを取り除くはずです。 – Omada

+1

ここで 'words [test + 1]'はUBです。プログラムがクラッシュしたり、コンピュータが足を伸ばしてガールフレンドと決闘する可能性があります。 – George

答えて

0

入力した単語のモードを検出するアルゴリズムを修正しました。私が言うことができるのは、countmaxはC++のキーワードなので、望ましくない影響を避けるために名前を変更する必要があります。また、をタイプstringとして宣言することを忘れないでください。

コードの変更はほとんどありません。

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

using namespace std; 

int main() { 
    vector<string>words; 

    cout << "Please enter some words. When finished, just enter 'stop'.\n"; 

    string ant; 
    int count = 0; 
    int max = 0; 

    while (cin >> ant) { 
     if (ant != "stop") { 
      words.push_back(ant); 
     } 
     else 
      break; 
    } 

    sort(words.begin(), words.end()); 

    cout << "The min of the entered words is " << words[0] << "\n"; 
    cout << "The max of the entered words is " << words.back() << "\n"; 

    //Finding the mode of the entered words 
    int _count = 0; 
    int _max = 0; 
    string mode; 
    for (unsigned int test = 0, j = 1; test<words.size(); test++, j++) { 
     if (words[test] == words[j]) { 
      _count++; 
     } 
     else if (words[test] != words[j]) { 
      if (_count>_max) { 
       _max = _count; 
       mode = words[test]; 
      } 

      _count = 0; 

     } 
    } 

    cout << "The mode of the entered words is " << mode; 

} 
+0

これはうまくいきません、コンパイルされますが、文字列を入力して停止すると、それが押しつぶされます。 – i3aze

+0

ここでクラッシュしません。モードアルゴリズムのforループで '++ test'の代わりに' test ++ 'を試してみてください –

+1

このコードはUBを生成します。 – George

関連する問題