随意设计输出格式
输入文件为in.txt
输出文件为out.txt
样例???????????????
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::ifstream fi;
fi.open("in.txt", std::ios::in);
if (!fi.is_open())
{
std::cout << "open file in.txt fail" << std::endl;
return -1;
}
std::ofstream fo;
fo.open("out.txt", std::ios::out);
if (!fo.is_open())
{
std::cout << "open file out.txt fail" << std::endl;
return -1;
}
std::string str, str1;
getline(fi, str1);
int start = 0;
int line = 1;
int count = 1;
while (getline(fi, str))
{
if (str == str1)
{
count++;
}
else
{
if (count > 1)
{
fo << str1 << " " << start << " " << count << std::endl;
}
start = line;
str1 = str;
count = 1;
}
line++;
}
if (count > 1)
{
fo << str1 << " " << start << " " << count << std::endl;
}
return 0;
}
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <utility>
int main(int argc, char *argv[])
{
std::ifstream fi;
fi.open("in.txt", std::ios::in);
if (!fi.is_open())
{
std::cout << "open file in.txt fail" << std::endl;
return -1;
}
std::ofstream fo;
fo.open("out.txt", std::ios::out);
if (!fo.is_open())
{
std::cout << "open file out.txt fail" << std::endl;
return -1;
}
std::map<string,std::pair<int,int>> m_str;
std::string str;
int line_no = 1;
while (getline(fi, str))
{
if(m_str.count(str))
{
m_str[str].second++;
line_no++;
}
else
{
m_str[str].first = line_no;
m_str[str].second++;
line_no++;
}
}
for(std::map<string,std::pair<int,int>>::iterator it = m_str.begin();it != m_str.end();++it)
{
if(it->second.second == 1) continue;
fo<<it->first<<" "<<it->second.first<<" "<<it->second.second<<endl;
}
cout<<"finish..."<<endl;
return 0;
}