64,646
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <sstream> // 定义string流
#include <fstream>
#include <string.h>
using namespace std;
struct pyUnit { // 拼音单元数据结构
string word ;
string py ;
int Pert; // 单词 拼音 百分比
};
typedef vector<pyUnit> StringList;
bool cmp(const pyUnit a, const pyUnit b) // 比较函数cmp
{ return a.Pert < b.Pert; }
void loadPYdict(fstream& file, StringList& resultList); // 加载拼音字典file 到容器resultList
int main()
{
StringList sList; // 定义装字典的容器
fstream pyDict("py.dat" , ios_base::in);
loadPYdict(pyDict, sList); // 加载字典到容器
sort(sList.begin(), sList.end() , cmp); // 按Pert排序
for (auto it = sList.begin(); it != sList.end(); ++it) // 遍历输出
cout << it->word << it->py << it->Pert << endl;
cout << "=========================" << endl;
//使用C++11 Lambda 函数,按拼音排序
sort(sList.begin(), sList.end() ,
[](const pyUnit a, const pyUnit b) { return a.py < b.py; });
//使用C++11 Lambda 函数,遍历输出
for_each(sList.begin(), sList.end(),
[](const pyUnit i) { cout << i.word << i.py << i.Pert << endl ;});
return 0;
}
// 加载拼音字典file 到容器resultList
void loadPYdict(fstream& file, StringList& resultList)
{
char word[64] ;
char py[64] ;
int Pert; // 单词 拼音 百分比
string line;
pyUnit tmp;
stringstream oss; // 定义string流
oss << file.rdbuf();
while (getline(oss , line)) { // 获取一行
if (line.empty() || (line[0] == '#')) // 空行或者注解行抛弃
continue;
sscanf(line.c_str(), "%s %s %d" , word, py , &Pert); // 格式化读取一行
tmp.word = word;
tmp.py = py;
tmp.Pert = Pert;
resultList.push_back(tmp); // 插入容器
}
}
/*拼音字典文件: py.dat
中文 Zhōngwén 99
中国 Zhōngguó 1
浙江 Zhèjiāng 5
北京 Běijīng 9
上海 Shànghǎi 8
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
void printHexString(const void* buf , size_t size); // 打印内存数据
int main()
{
vector<char> v;
for (int i = 1; i != 128; i++) v.push_back(i);
// 把容器二进制写文件
ofstream fs("fs.dat", ios_base::binary);
for (auto it = v.begin(); it != v.end(); ++it) {
fs.write((char*) & (*it) , sizeof(*it));
}
// 打印容器内存存储的数据
auto it = v.begin();
printHexString((void*) & (*it), sizeof(*it) * v.size());
return 0;
}
void printHexString(const void* buf , size_t size)
{
unsigned char* str = (unsigned char*)buf;
char line[512] = {0};
const size_t lineLength = 16; // 8或者32
char text[24] = {0};
char* pc;
int textLength = lineLength;
for (size_t ix = 0 ; ix < size ; ix += lineLength) {
sprintf(line, "%.8xh: ", ix);
// 打印16进制
for (size_t jx = 0 ; jx != lineLength ; jx++) {
if (ix + jx >= size) {
sprintf(line + (11 + jx * 3), " "); // 处理最后一行空白
if (ix + jx == size)
textLength = jx; // 处理最后一行文本截断
} else
sprintf(line + (11 + jx * 3), "%.2X ", * (str + ix + jx));
}
// 打印字符串
{
memcpy(text, str + ix, lineLength);
pc = text;
while (pc != text + lineLength) {
if ((unsigned char)*pc < 0x20) // 空格之前为控制码
*pc = '.'; // 控制码转成'.'显示
pc++;
}
text[textLength] = '\0';
sprintf(line + (11 + lineLength * 3), "; %s\n", text);
}
printf("%s", line);
}
}
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <sstream> // 定义string流
#include <fstream>
#include <string.h>
using namespace std;
struct pinyinUnit { // 拼音单元数据结构
char word[8] ;
char py[32] ;
int Pert; // 单词 拼音 百分比
};
typedef vector<pinyinUnit> StringList;
bool cmp(const pinyinUnit a, const pinyinUnit b) // 比较函数cmp
{ return a.Pert < b.Pert; }
void loadPYdict(fstream& file, StringList& resultList); // 加载拼音字典file 到容器resultList
int main()
{
StringList sList; // 定义装字典的容器
fstream pyDict("py.dat" , ios_base::in);
loadPYdict(pyDict, sList); // 加载字典到容器
sort(sList.begin(), sList.end() , cmp); // 按Pert排序
for (auto it = sList.begin(); it != sList.end(); ++it) // 遍历输出
cout << it->word << it->py << it->Pert << endl;
cout << "=========================" << endl;
//使用C++11 Lambda 函数,按拼音排序
sort(sList.begin(), sList.end() ,
[](const pinyinUnit a, const pinyinUnit b) { return strcmp(a.py , b.py) < 0; });
//使用C++11 Lambda 函数,遍历输出
for_each(sList.begin(), sList.end(),
[](const pinyinUnit i) { cout << i.word << i.py << i.Pert << endl ;});
fstream pyDictBin("pybin.dat" , ios_base::out | ios_base::binary);
// for (auto i = 0; i != sList.size(); ++i) // 遍历写文件
// pyDictBin.write((char*) &sList[i] , sizeof(pinyinUnit));
//
// for (auto it = sList.begin(); it != sList.end(); ++it) // 遍历写文件
// pyDictBin.write((char*) & (*it) , sizeof(pinyinUnit)); // 迭代器要先解引用,然后再取地址
auto it = sList.begin();
pyDictBin.write((char*) & (*it) , sizeof(pinyinUnit) * sList.size()); // 一次性把容器写文件
return 0;
}
// 加载拼音字典file 到容器resultList
void loadPYdict(fstream& file, StringList& resultList)
{
char word[64] = {0} ;
char py[64] = {0} ;
int Pert; // 单词 拼音 百分比
string line;
pinyinUnit tmp ;
memset(&tmp, 0 , sizeof(tmp));
stringstream oss; // 定义string流
oss << file.rdbuf();
while (getline(oss , line)) { // 获取一行
if (line.empty() || (line[0] == '#')) // 空行或者注解行抛弃
continue;
sscanf(line.c_str(), "%s %s %d" , word, py , &Pert); // 格式化读取一行
strcpy(tmp.word , word);
strcpy(tmp.py , py);
tmp.Pert = Pert;
resultList.push_back(tmp); // 插入容器
}
}
/*拼音字典文件: py.dat
中文 Zhōngwén 99
中国 Zhōngguó 1
浙江 Zhèjiāng 5
北京 Běijīng 9
上海 Shànghǎi 8
印刷 Yinshua 8
虫虫 Chongchong 4
*/