//WorldTest.h
<span style="font-family:Courier New;font-size:10px;"><span style="font-family:Courier New;font-size:10px;">/*
//Function : count world
//Date : 2015-09-24
//Author : pengl
*/
#ifndef _WORLD_TEST_H_
#define _WORLD_TEST_H_
#include <string>
#include <iostream>
#include <map>
#include <vector>
#include <iterator>
#include <numeric>
#include <algorithm>
#include <fstream>
#include <tchar.h>
#if defined(_UNICODE) || defined(UNICODE)
#define StringBase std::wstring
#else
#define StringBase std::string
#endif
class WorldTest
{
public:
WorldTest();
WorldTest(StringBase _str_input_file_name, StringBase _str_output_file_name);
~WorldTest();
public:
bool run();
private:
bool stringSplit(const StringBase &_str_source, std::vector<StringBase> &_vec_dst, const TCHAR _c_flag = _T('\0'));
bool readIn();
bool writeOut();
// bool ReadStringW(std::wstring& rString);
private:
StringBase m_str_input_file_name;
StringBase m_str_output_file_name;
std::wifstream m_infile;
std::wofstream m_outfile;
std::map<StringBase, int> m_map_world_count;
};
#endif</span></span>
//WorldTest.cpp
#include "WorldTest.h"
using namespace std;
WorldTest::WorldTest()
{
}
WorldTest::WorldTest(StringBase _str_input_file_name, StringBase _str_output_file_name)
: m_str_input_file_name(_str_input_file_name),
m_str_output_file_name(_str_output_file_name)
{
m_infile.open(m_str_input_file_name);
if (!m_infile.is_open())
{
wcout << _T("input file open field!") << endl;
}
m_outfile.open(m_str_output_file_name);
if (!m_outfile.is_open())
{
wcout << _T("output file open field!") << endl;
}
}
WorldTest::~WorldTest()
{
m_infile.close();
m_outfile.close();
}
bool WorldTest::run()
{
bool success = true;
success &= readIn();
success &= writeOut();
return (success);
}
bool WorldTest::readIn()
{
bool success = true;
// 读取文件的每一行
StringBase str_temp = _T("");
map<StringBase, int>::iterator it_map_pos;
while (!m_infile.eof())
{
vector<StringBase> vec_temp;
getline(m_infile, str_temp);
stringSplit(str_temp, vec_temp); // 对每一行数据进行分割处理
for (int i = 0; i < vec_temp.size(); ++i)
{
it_map_pos = m_map_world_count.find(vec_temp[i]);
if (it_map_pos == m_map_world_count.end())
{
m_map_world_count.insert(make_pair(vec_temp[i], 1)); // 保存数据
}
else
{
++(it_map_pos->second);
}
}
}
return (success);
}
bool WorldTest::writeOut()
{
bool success = true;
// 写出到文件count.txt
map<StringBase, int>::iterator it_map_pos;
int isum = 0;
for (it_map_pos = m_map_world_count.begin(); it_map_pos != m_map_world_count.end(); ++it_map_pos)
{
isum += it_map_pos->second;
m_outfile << it_map_pos->first << _T(": ") << it_map_pos->second << endl;
}
m_outfile << endl << _T("all count: ") << isum << endl;
return (success);
}
// 分割字符串
bool WorldTest::stringSplit(
const StringBase &_str_source,
vector<StringBase> &_vec_dst,
const TCHAR _c_flag
)
{
bool success = true;
StringBase::const_iterator cit_pos1;
StringBase::const_iterator cit_pos2;
StringBase::const_iterator cit_pos;
if (_str_source == _T(""))
{
return false;
}
if (_c_flag == _T('\0')) // 默认非字母的都是分隔符
{
auto auto_lam_find =
[](TCHAR _ch)->bool
{
if ((_ch >= _T('a') && _ch <= _T('z'))
|| (_ch >= _T('A') && _ch <= _T('Z')))
{
return false;
}
return true;
};
cit_pos1 = _str_source.begin();
cit_pos2 = find_if(cit_pos1, _str_source.end(), auto_lam_find);
while (cit_pos2 != _str_source.end())
{
StringBase str_temp(cit_pos1, cit_pos2);
if (str_temp != _T(""))
{
_vec_dst.push_back(str_temp);
}
cit_pos1 = cit_pos2 + 1;
cit_pos2 = find_if(cit_pos1, _str_source.end(), auto_lam_find);
}
if (cit_pos1 != _str_source.end())
{
StringBase str_temp(cit_pos1, cit_pos2);
if (str_temp != _T(""))
{
_vec_dst.push_back(str_temp);
}
}
}
else // 使用参数作为分隔符
{
auto auto_lam_find =
[&_c_flag](TCHAR _ch)->bool
{
if (_ch == _c_flag)
{
return true;
}
return false;
};
cit_pos1 = _str_source.begin();
cit_pos2 = find_if(cit_pos1, _str_source.end(), auto_lam_find);
while (cit_pos2 != _str_source.end())
{
StringBase str_temp(cit_pos1, cit_pos2);
if (str_temp != _T(""))
{
_vec_dst.push_back(str_temp);
}
cit_pos1 = cit_pos2 + 1;
cit_pos2 = find_if(cit_pos1, _str_source.end(), auto_lam_find);
}
if (cit_pos1 != _str_source.end())
{
StringBase str_temp(cit_pos1, cit_pos2);
if (str_temp != _T(""))
{
_vec_dst.push_back(str_temp);
}
}
}
return (success);
}
//main.cpp
#include "WorldTest.h"
// 若要程序支持unicode字符集,请注意在程序中使用wstring字符串
// 并在所有常量字符或字符串前加上_T()
// vs默认工程属性为unicode字符集。
int main()
{
WorldTest wt(_T("world.txt"), _T("count.txt"));
wt.run();
}