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

问题描述 投票:0回答:1

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

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

 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;
    }
}

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

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

我希望输出看起来像

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

c++
1个回答
0
投票

我稍微改变了你的第三个

for(int i = 0; i<arrayLength; i++) 
{
    string tempString;
    ss >> tempString;
    if (tempString.empty())
        continue;
    words[tempString] = tempString.length();
    cout << tempString << " " <<words[tempString]<<" ";
}
© www.soinside.com 2019 - 2024. All rights reserved.