使用户输入一个键,如果多次发现该键,则增加值

我正在处理家庭作业问题。但是,我不确定如何解决我面临的问题。您可以忽略大多数代码,我已经使上半部分开始工作,只是试图弄清楚如何使地图起作用。

我尝试过这段糟糕的代码。

 stringstream ss(str);
    map<string,int> words;
    int arrayLength;
    arrayLength = str.length();

    string userarray[arrayLength];

    for (int i=0;i<arrayLength;i++){
        string tempString;
        ss>>tempString;
        userarray[i] = tempString;
        words[userarray[i]]=i+1;
        if(words[userarray[i]]==1)
        {
            words.insert(make_pair(userarray[i],i++));
        }
        cout<< words.at(userarray[i]);
    }

这是我所有的代码

#include <algorithm>
#include <string>
#include <iostream>
#include <set>
#include <fstream>
#include <sstream>
#include <unordered_set>
#include <map>
#include <utility>
using namespace std;
int word()
{
   {
//str store the string
    string again = "y";
    string str= "",checkStr;
    cout<<"Enter the line : ";
    getline(cin,str);
    checkStr = str;
    checkStr[str.length()-1]=' ';

    while(again =="y")
    {

//store all the tokens
        set<char> tokens;
        unordered_set<char> token;
//if characters are not in range consider as token
         for (int i=0;i<str.length();i++)
        {
            char inChar = str[i];
            if(inChar>='A' && inChar<='Z' || inChar>='a' && inChar<='z' )
            {

            }
            else
            {
                if(str[i]!=' ' && str[i]!='\n' )
                tokens.insert(inChar);
                token.insert(inChar);
            str[i] = ' ';
            }
        }
//print the tokens
         set <char> :: iterator pointer;

        cout<<"tokens are : "<<endl;

        for (pointer = tokens.begin(); pointer != tokens.end(); ++pointer)
            {
            cout<<*pointer<<" ";
            }

        cout<<endl;

//print the tokens
        cout<<"unordered set: "<<endl;
        for(auto it = token.begin(); it != token.end();it++)
            cout<< *it<< " ";
        cout<< endl;

//store string to stream

        stringstream ss(str);
        map<string,int> words;
        int arrayLength;
        arrayLength = str.length();

        string userarray[arrayLength];

        for (int i=0;i<arrayLength;i++){
            string tempString;
            ss>>tempString;
            userarray[i] = tempString;
            words[userarray[i]]=i+1;
            if(words[userarray[i]]==1)
            {
                words.insert(make_pair(userarray[i],i++));
            }
            cout<< words.at(userarray[i]);
        }

    cout<<"Press 'y' to run again: "<<endl;
    getline(cin,again);
    if(again =="y")
    {
    return 1;
    }
    else
    {
    cout<<"Function terminated"<<endl;
    exit(2);
    }

    }
}
}
int main ()
{
    while (word()==1)

    {
    word();
    }
    while(word()==2)
    {
    break;
    }
}

我的问题是当我敲出代码时,会得到一长串数字。

但是,如果我输入 “祝你有美好的一天。祝你有愉快的课堂。“ +”祝你访问愉快。祝你玩得开心!”

我希望输出看起来像

有4 3 好3 第一天 1级 造访1 有趣的1

自那以后,我的代码开始工作了,我的朋友今天为我提供了最后的帮助。代码是...

stringstream ss(str);
    map <string,int> dict;
    map <string,int> dict_counted;
    string line;

    // create an array of strings
    string array[100];

    int counter = 0;
    while (getline(ss,line,' '))
    {
        array[counter] = line;
        counter++;
        if (dict.count(line))
        {
            dict[line]+=1;;
        }
        else
        {
        dict[line] = 1;
        }
    }


    for (int i=0;i<counter;i++)
    {
        if (dict_counted.count(array[i]))
        {

        }
        else
        {
            if (!(array[i].empty()))
            {
                cout<<"The frequency of "<<array[i]<<" is "<<dict[array[i]] <<endl;
                dict_counted[array[i]] = -1;
            }
        }

    }
feifangongzilianjia 回答:使用户输入一个键,如果多次发现该键,则增加值

我稍微改变了你的第三位

for(int i = 0; i<arrayLength; i++) 
{
    string tempString;
    ss >> tempString;
    if (tempString.empty())
        continue;
    words[tempString] = tempString.length();
    cout << tempString << " " <<words[tempString]<<" ";
}
本文链接:https://www.f2er.com/3162168.html

大家都在问