分享:使用C++11 Lambda 函数,实现自定义结构的排序

hongwenjun 2012-09-28 11:07:15


#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
*/


...全文
494 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
hongwenjun 2012-09-28
  • 打赏
  • 举报
回复

不能编辑帖子,只有 再贴图片了
hongwenjun 2012-09-28
  • 打赏
  • 举报
回复
hongwenjun 2012-09-28
  • 打赏
  • 举报
回复
#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);
}
}


vector 容器如果存的是 标准数据 char int double ,还有 struct fo{ char s[4] ..... };
容器就相当于C语言的数组

  • 打赏
  • 举报
回复
hongwenjun 2012-09-28
  • 打赏
  • 举报
回复
结构中使用 string 不用纠结 结构大小
但是不能直接写文件

使用 char[] 字符数组,可以直接写文件效率上好点

#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
*/

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧