2017-11-27 1 views
-1

イテレータを使用してアイテムのの量を表示する方法を理解しようとしています。 1つの例としては、5つのリンゴがあります。私はそれが "5倍アップル"またはそのような何かを出力することを望むでしょう。私はどのようにこれを達成するか分かりません。ここでは、ユーザーが在庫に追加する文字列を入力する単純なコードを示します。イテレータを使用してインベントリ内のアイテムのカウンタ

#include <iostream> 
#include <string> 
#include <vector> 
using namespace std; 

string item; 

vector<string> inventory; 
vector<string>::iterator iter; 

int main() 
{ 
    while(true){ 
     cin >> item; 
     inventory.push_back(item); 

     cout << "INVENTORY:\n"; 

     for(iter = inventory.begin(); iter != inventory.end(); iter++) 
     cout << *iter << endl; 
    } 
} 

EDIT:ゲーム用の在庫システムを作成しようとしています。だから私はイテレータが必要かもしれないと思った。イテレータを使用するよりもインベントリシステムを作成する方が良い場合は、私に教えてください。謝罪、私は明らかにすべきだった。

+0

これはイテレータとは何が関係していますか? – NetMage

+0

イテレータを使用しているので、イテレータと何か関係があると考えるには十分だと思いました。たぶんそれはベクターともっと関係がありますか?あなたは裁判官になります。 –

+0

なぜイテレータをここで使用していますか?そして、「5xアップル」の「5」はどこから来ますか?質問が何であるかは本当に分かりません – UnholySheep

答えて

0

これを単純化する方法は、在庫内のアイテムを並べ替えることです。それは同じものを集めて、それらの数えを単純化します。最初から開始し、現在のものと一致する連続したアイテムの数を数え、それを表示し、最初に一致しないものを続ける。

より詳細には
#include <algorithm> 
#include <iostream> 
#include <string> 
#include <vector> 

int main() 
{ 
    std::string item; 
    std::vector<std::string> inventory; 

    while (true) { 
     std::cin >> item; 
     if (item == "quit") return 0; 

     inventory.push_back(item); 
     std::sort(inventory.begin(), inventory.end()); 

     std::cout << "INVENTORY:\n"; 

     auto current = inventory.begin(); 
     while (current != inventory.end()) { 
      int count = 1; 
      auto probe = current + 1; 
      while (probe != inventory.end() && *probe == *current) { 
       ++count; 
       ++probe; 
      } 
      std::cout << count << "x " << *current << '\n'; 
      current = probe; 
     } 
     std::cout.flush(); 
    } 

    return 0; 
} 

、あなたの在庫がある場合は、{「オレンジ」、「りんご」、「オレンジ」}、その後、ソートは{「オレンジ」、「オレンジ」、「りんご」}に順序を変更します。同じものが一緒にあることに注意してください。

イテレータcurrentが先頭(「apple」)から始まります。 countは、少なくとも1があることを知っているので1に設定します。イテレータprobeを次の項目(「オレンジ」)を指すように設定しました。 probeの値がcurrentの値と一致しないため、内部ループは何も行いません。 countと現在のアイテム(「りんご」)を印刷します。 currentprobeに設定します。この時点では、probeが現在のものと一致しない最初の項目を指しているためです。

2回目の反復では、currentは最初の「オレンジ」を示します。countを1にリセットし、次の項目(「オレンジ」)でprobeを開始します。一致した値のため、count(現在は2)をインクリメントし、probe(今在庫の最後に)を進めます。 count(2)と現在の項目(「オレンジ」)を印刷し、currentprobe(リストの末尾)に設定します。外側のループ条件は、インベントリの最後にあると判断し、ループが終了します。

+0

あなたは、GENIUSです。これは完全に動作します!ありがとう! –

-1
inventory.size() 

は、ベクター内の項目数を返します。

どのようにそのタスクのイテレータが必要かわかりません。

2

イテレータを使用すると、コンテナを反復処理できますが、カウントは行いません。

コンテナのsize()にはコンテナに含まれるアイテムの数が表示されますが、アイテムの種類が異なる場合は、自分で数えなければなりません。

たとえば、4 "apple"と1 "orange"があるとします。例えば、あなたがstd::count_if()使用して検討するかもしれない、

#include <iostream> 
#include <string> 
#include <vector> 
using namespace std; 

vector<string> inventory; 
int numApples = 0; 
int numOranges = 0; 
int numOther = 0; 

int main() 
{ 
    string item; 

    while (cin >> item) 
    { 
     inventory.push_back(item); 

     if (item == "apples") 
      ++numApples; 
     else if (item == "orange") 
      ++numOranges; 
     else 
      ++numOther; 
    } 

    cout << "INVENTORY:\n"; 

    for (vector<string>::iterator iter = inventory.begin(); iter != inventory.end(); ++iter) 
     cout << *iter << endl; 

    /* or, if you are using C++11 or later: 
    for (string &s : inventory) 
     cout << s << endl; 
    */ 

    cout << "# apples: " << numApples << endl; 
    cout << "# oranges: " << numOranges << endl; 
    cout << "# other: " << numOther << endl; 

    return 0; 
} 

または::

#include <iostream> 
#include <string> 
#include <vector> 
using namespace std; 

vector<string> inventory; 

bool isApple(const string &s) { return (s == "apple"); } 
bool isOrange(const string &s) { return (s == "orange"); } 
bool isOther(const string &s) { return !(isApple(s) || isOrange(s)); } 

int main() 
{ 
    string item; 

    while (cin >> item) 
     inventory.push_back(item); 

    cout << "INVENTORY:\n"; 

    for (vector<string>::iterator iter = inventory.begin(); iter != inventory.end(); ++iter) 
     cout << *iter << endl; 

    /* or, if you are using C++11 or later: 
    for (string &s : inventory) 
     cout << s << endl; 
    */ 

    cout << "# apples: " << count_if(inventory.begin(), inventory.end(), isApple) << endl; 
    cout << "# oranges: " << count_if(inventory.begin(), inventory.end(), isOrange) << endl; 
    cout << "# other: " << count_if(inventory.begin(), inventory.end(), isOther) << endl; 

    /* or, if you are using C++11 or later: 
    cout << "# apples: " << count_if(inventory.begin(), inventory.end(), [](auto &s){ return (s == "apple"); }) << endl; 
    cout << "# oranges: " << count_if(inventory.begin(), inventory.end(), [](auto &s){ return (s == "orange"); }) << endl; 
    cout << "# other: " << count_if(inventory.begin(), inventory.end(), [](auto &s){ return (s != "apple") && (s != "orange"); }) << endl; 
    */ 

    return 0; 
} 

更新

あなたは、入力された各項目を見て、必要に応じて、例えば、それをカウントする必要があり:投稿したanother questionに基づいて、代わりに次のようなものを試してください:

#include <iostream> 
#include <string> 
#include <vector> 
using namespace std; 

vector<string> other_inventory; 
int numApples = 0; 
int numOranges = 0; 

int main() 
{ 
    string item; 

    while (cin >> item) 
    { 
     if (item == "apples") 
      ++numApples; 
     else if (item == "orange") 
      ++numOranges; 
     else 
      other_inventory.push_back(item); 
    } 

    cout << "INVENTORY:\n"; 

    if (numApples > 0) 
     cout << "# apples: " << numApples << endl; 

    if (numOranges > 0) 
     cout << "# oranges: " << numOranges << endl; 

    for (vector<string>::iterator iter = other_inventory.begin(); iter != other_inventory.end(); ++iter) 
     cout << *iter << endl; 

    /* or, if you are using C++11 or later: 
    for (string &s : other_inventory) 
     cout << s << endl; 
    */ 

    return 0; 
} 
+0

ありがとうございました。私はどのようにコードにsize()やcount_if()を実装しますか? –

+1

これらは、標準のC++ベクタータイプの一部です。私はあなたがこの作業を試みる前にもっと多くのことを学ぶ必要があると思います。 – NetMage

+0

ラムダ関数を通常のコースとして使用することはありませんか? 'count_if(inventory.begin()、inventory.end()、[](auto s){return s ==" apple "})'? (このコンテキストでは 'auto'は動作するのでしょうか?適切な答えはC#LINQ' GroupBy'と同等であると思います) – NetMage

関連する問題