如何识别和计算字符串流中的唯一字符

How to identify and count unique chars in a stringstream

函数读取文件的每一行。 (字符代表政党。)第一个标记被忽略,但函数必须识别和计算后续字符的实例。每行至少有两个独特的字符,但可以有更多。这些字符在运行时是未知的。输入文件可能如下所示:

district1 D D R R D
district2 D G R R R I
district3 I D D R D D 
district4 R R I

如何应用 streamstream 来识别、读取和计算一行中的单个字符?我将在稍后的代码中使用这些值来计算一些比率。

Map<string, double> gerrymanderingRatios(string file) {
    Map<string, double> gerryMap;
    ifstream file_in(file);

    if (file_in) {
        string line, ignoreMe;
        stringstream ss;
        while (file_in >> ignoreMe, getline(file_in, line)) {
            /* ignore first token and count instances of each
            char in the line. */
        }
        file_in.close();

        /* calculate ratios for "political party" (char)
        and insert into the map. */
    }
    return gerryMap;

结果将是一张地图;键是各方 (char),值是比率,即 { {"D", 0.4543}, {"R", 1.0323}, {"I", 0.343} }

我会做这样的事情,利用异常来知道符号是否已经在地图中。编辑您的代码:

#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
#include<map>
#include<stdexcept>

using namespace std;

map<string, double> gerrymanderingRatios(string file) {
    map<string, int> countMap;
    map<string, double> gerryMap;
    ifstream file_in(file);

    if (file_in) {
        string line, ignoreMe, ch;
        int total = 0;
        while (file_in >> ignoreMe, getline(file_in, line)) {
            /* ignore first token and count instances of each
            char in the line. */
            stringstream ss(line);
            while(ss >> ch) {
                try {
                    countMap.at(ch)++;
                } catch(const out_of_range& oor) {
                    countMap[ch] = 1;
                }
                total++;
            }            
        }
        file_in.close();

        // print the final count for each element
        map<string, int>::iterator it;
        for (it = countMap.begin(); it != countMap.end(); it++ ) {
            cout << it->first
               << ':'
               << it->second
               << endl;
        }

        /* calculate ratios for "political party" (char)
        and insert into the map. */

        //calculate ratios
        for (it = countMap.begin(); it != countMap.end(); it++ ) {
            gerryMap[it->first] = (double)it->second / total;
        }

        //print total ratios
        cout << "ratios" << endl;
        map<string, double>::iterator dit;
        for (dit = gerryMap.begin(); dit != gerryMap.end(); dit++ ) {
          cout << dit->first 
               << ':'
               << dit->second
               << endl;
        }

    }
    return gerryMap;
}

int main() {
    map<string, double> ratiomap = gerrymanderingRatios("example.txt");

    //do whatever you need with the ratios

    return 0;
}

相关部分是这样的:

while(ss >> ch) {
    try {
        countMap.at(ch)++;
    } catch(const out_of_range& oor) {
        countMap[ch] = 1;
    }
    total++;
}
如果键 ch 不在映射中,

countMap.at(ch) 将抛出 out_of_range 异常。所以我可以尝试增加该值,但如果抛出异常,则会添加它,而不是计数为 1。 请注意,我已经引入了一个 map<string, int> countMap 来使用整数来保持每个键的单独计数,并在我计算比率时在最后使用你的 map<string, double> gerryMap