c++ - 我想存储从文件中获取的数字及其计数

标签 c++ file stl

我的文件内容是:

1,2,5
2,4
2,3
1,2,4
1,3
2,3 
1,3 
1,2,3,5
1,2,3

我的代码是:

#include<iostream>
#include<set>
#include<vector>
#include<fstream>
#include<sstream>
#include<set>
#include<cstdlib>
#include<algorithm>
using namespace std;
struct store {
    string a;
    int count;
};
int main() {
    store ap[100];
    vector<string> v;
    set<string> input;
    int mycount;
    ifstream fin("trans.txt");
    string line, s, str1, token;
    while(!fin.eof()) {
        fin >> line;
        //  cout<<"i cant understand but correct"<<line<<endl;
        istringstream str1(line);
        while(getline(str1, token, ',')) {
            //cout<<"the token are\t"<<token<<endl;
            v.push_back(token);
            input.insert(token);
        }
        //v.push_back(token);
        //input.insert(token);
        int i = 0;
        for(set<string>::iterator it = input.begin(); it != input.end(); it++) {
            mycount = count(v.begin(), v.end(), *it);
            s = *it;
            ap[i].a = s;
            ap[i].count = mycount;
            cout << ap[i].a << "\t" << "mycount" << ap[i].a << endl;
            i++;
        }
    }
}

我正在实现 Apriori 算法,每一行代表事务,即存储在文件中的项目我的文件由这样的数字组成如何存储每个数字的出现及其计数

我的输出应该是这样的:

 1 6
 2 7
 3 7
 4 2
 5 2

但是我无法单独存储我的意思是 1 及其所有出现 2 及其所有出现等等。

谁能告诉我如何像上面的例子那样存储

最佳答案

如果您正在阅读的数字在一个小范围内(例如 0-10,甚至 0-200 等),而不是使用 [ std::map ( http://en.cppreference.com/w/cpp/container/map ),您可以使用简单数组。 map 的键是数组索引, map 的值(即出现次数)是该索引的数组值。 例如数字 3 的出现存储在索引 3 处的整数数组中。

有关详细信息,请参阅以下注释代码。

我用 VS2010 SP1 (VC10) 编译了该代码并执行了它,它似乎工作正常(至少对于您的输入文件示例数据)。

#include <cstdlib>
#include <exception>
#include <fstream>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>

using namespace std;

int main() 
{
    static const int kExitOk = 0;
    static const int kExitError = 1;

    try
    {
        // Open text file for reading
        ifstream inFile("data.txt");

        // Occurrence table
        static const int kMaxNum = 10;
        int occurrences[kMaxNum + 1] = {0}; // init to 0s

        // Line read from file       
        string line;

        // For each line in file
        while (getline(inFile, line))
        {
            // Process each line content using string streams
            istringstream iss(line);

            // Read numbers (separated by comma) from current line
            string token;
            while (getline(iss, token, ','))
            {
                // Convert from string to integer
                const int num = atoi(token.c_str());

                // Just do a bounds checking for safety...
                if (num < 0 || num > kMaxNum)
                    throw runtime_error("Bad input number found in file.");

                // Update occurrence of given number    
                occurrences[num]++;
            }
        }

        // Print occurrences
        for (int i = 0; i <= kMaxNum; i++)
        {
            if ( occurrences[i] != 0 )
            {
                cout << i << ' ' << occurrences[i] << '\n';
            }
        }

        return kExitOk;
    }
    catch(const exception& e)
    {
        cerr << "\n*** ERROR: " << e.what() << endl;
        return kExitError;
    }        
}

如果你想使用 std::map , 只需添加 #include <map> ,并将数组定义替换为:

// Occurrence table
map<int, int> occurrences;

您可以使用如下代码打印 map 内容:

// Print occurrences
for (auto it = occurrences.begin(); it != occurrences.end(); ++it)
{
    cout << it->first << ' ' << it->second << '\n';
}

请注意,使用 std::map出现更新代码在形式上与数组情况相同:

// Update occurrence of given number    
occurrences[num]++; // works also for std::map

关于c++ - 我想存储从文件中获取的数字及其计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15177410/

相关文章:

c++ - 为什么 fmt 库不是仅 header ?

c++ - 查找图中两个节点之间的所有路径

python - 如何用python中文件中的数据填充数组

android - 如何列出设备上的所有图像?

file - 读取/写入/查找/替换巨大的csv文件

c++ - STL 中的比较器

c++ - C++ 中的 key_compare 与 key_comp

c++ - MinGW 作为可靠的 64 位 GCC 编译器

c++ - 无法创建具有值和绑定(bind)的 std::ranges::iota_view

c++ - 使用 gnuplot 制作 3D 曲面图