52 字符串统计 剩余可用字符集

该C++代码定义了一个`solve_method`函数,用于解析命令行输入的键值对,根据第二个键值对调整第一个键的值,并按键排序输出非零值。
摘要由CSDN通过智能技术生成

#include <iostream>
#include <sstream>
#include <vector>
#include <map>

using namespace std;

void solve_method() 
{
    string input;
    getline(cin, input);

    stringstream ss(input);
    string s;
    vector<string> s_list;
    while (getline(ss, s, '@')) 
    {
        s_list.push_back(s);
    }

    if (s_list.size() != 2) 
    {
        cout << input << endl;
        return;
    }

    vector<string> in_key_order_list;
    map<string, int> d_map;
    for (int i = 0; i < 2; i++) 
    {
        stringstream curr_ss(s_list[i]);
        string item;
        while (getline(curr_ss, item, ',')) 
        {
            stringstream item_ss(item);
            string key, value;
            getline(item_ss, key, ':');
            getline(item_ss, value, ':');
            int int_value = stoi(value);
            if(i == 0)
            {
                d_map[key] = int_value;
                in_key_order_list.push_back(key);
            }
            else
            {
                d_map[key] -= int_value;
            }
        }
    }

    for (int i = 0; i < in_key_order_list.size(); i++) 
    {
        string key = in_key_order_list[i];
        int value = d_map[key];
        if (value != 0) 
        {
            if (i != in_key_order_list.size() - 1) 
            {
                cout << key << ":" << value << ",";
            } 
            else 
            {
                cout << key << ":" << value << endl;
            }
        }
    }
}

int main() 
{
    solve_method();
    return 0;
}
 

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值