分析ip访问次数的记录文件

“如:
192.168.0.1 20
192.168.0.2 30
192.168.0.3 5

右边的表示各个ip出现的次数
我想根据次数对文本进行重新的排列
谢谢


偶:
int main()
{

    string fileName;
    cout << "please input the file name to read from : ";
    cin >> fileName;

    multimap<unsigned int, string> mapLst;
    fstream infile(fileName.c_str());
    if (!infile)
    {
        return 1;
    }
    while (infile)
    {
        string str;
        infile >> str;
        unsigned int uin = 0;
        infile >> uin;    
        mapLst.insert(make_pair(uin, str));
    }
    mapLst.erase(0);
    infile.close();

    if (mapLst.empty())
    {
        cout << "no data !" << endl;
        return 1;
    }

    cout << "please input the file name to write to : ";
    cin >> fileName;
    fstream outfle;    //(fileName.c_str());
    outfle.open(fileName.c_str(), ios_base::out);

    if (outfle)
    {
        multimap<unsigned int, string>::const_iterator cit = mapLst.begin();
        while (cit != mapLst.end())
        {
            outfle << cit -> second << " " << cit -> first << endl;
            cit ++;
        }
        outfle.close();
    }
    else
        return 1;
    return 0;
}


taodm:
 

ifstream ifs("xxx");
string ip;
int count;
multimap<int, string> m;
while (ifs >> ip >> count)
{
m.insert(make_pair(count, ip));
}
ifs.close();
ofstream ofs("xxx");
for (multimap<int, string>::iterator iter = m.begin(); iter != m.end(); ++iter)
{
ofs << iter->second << " " << iter->first << "/n";
}

ifs >> ip >> count;
妙在同时成功的时候才能继续执行
学习之

你可能感兴趣的:(ios,String,File,iterator,input,pair)