c++处理文件,对txt进行处理,学了那么久才发现我还不会这些操作,另外c++处理文本真是快得可怕啊

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream infile;
infile.open("E:\\word1.txt");
string s;
ofstream outfile;
outfile.open("E:\\word2.txt");
while(getline(infile,s)) {
for(int i=; s[i]; i++)
if(s.substr(i,)=="n."||s.substr(i,)=="ad."||s.substr(i,)=="a."
||s.substr(i,)=="vi."||s.substr(i,)=="vt."||s.substr(i,)=="conj.") {
outfile<<s.substr(,i)<<" "<<s.substr(i)<<endl;
break;
}
}
return ;
}

各种调顺序,也是很有意思的,MFC也是面对对象啦

我做的课题是基于红黑树的map实现。

再列出一些自己觉得有用的吧

老师提供的宽字节转换,为了兼容汉字

ifstream in(L".\\word.txt");
char s[][];
CString st[];
while (in >> s[] >> s[]){
for (int i = ; i<; i++)
{
int charLen = strlen(s[i]);
int len = MultiByteToWideChar(CP_ACP, , s[i], charLen, NULL, );
TCHAR *buf = new TCHAR[len + ];
MultiByteToWideChar(CP_ACP, , s[i], charLen, buf, len);
buf[len] = '\0';
st[i] = buf;
}
dic.Insert(st[],st[]);
m_num++;
}

实现的时间统计。比timegettime精确地多

LARGE_INTEGER  large_interger;
double dff;
__int64 c1, c2;
QueryPerformanceFrequency(&large_interger);
dff = large_interger.QuadPart;
QueryPerformanceCounter(&large_interger);
c1 = large_interger.QuadPart;
CString s2 = dic.query(s1);
QueryPerformanceCounter(&large_interger);
c2 = large_interger.QuadPart;

所以现在看在我实现的东西并不是很多

RBTree

#define _RB_TREE_H_
#include<iostream>
#include<string>
#include<sstream>
#include<fstream>
#include <time.h>
using namespace std;
template<class KEY, class U>
class RB_Tree
{
private:
RB_Tree(const RB_Tree& input){}
const RB_Tree& operator=(const RB_Tree& input){}
private:
enum COLOR{ RED, BLACK };
class RB_Node
{
public:
RB_Node()
{
RB_COLOR = BLACK;
right = NULL;
left = NULL;
parent = NULL;
}
COLOR RB_COLOR;
RB_Node* right;
RB_Node* left;
RB_Node* parent;
KEY key;
U data;
};
public:
RB_Tree()
{
this->m_nullNode = new RB_Node();
this->m_root = m_nullNode;
this->m_nullNode->right = this->m_root;
this->m_nullNode->left = this->m_root;
this->m_nullNode->parent = this->m_root;
this->m_nullNode->RB_COLOR = BLACK;
}
bool Empty()
{
if (this->m_root == this->m_nullNode)
{
return true;
}
else
{
return false;
}
}
//find node who's key equals to key,else find the insert point;
RB_Node* find(KEY key)
{
RB_Node* index = m_root;
while (index != m_nullNode)
{
if (key<index->key)
{
index = index->left;
}
else if (key>index->key)
{
index = index->right;
}
else
{
break;
}
}
return index;
}
const U& RB_Tree<KEY, U>::query(const KEY& key)
{
RB_Node *p = find(key);
if (p == NULL)
return NULL;
else
return p->data ;
}
bool Insert(KEY key, U data)
{
RB_Node* insert_point = m_nullNode;
RB_Node* index = m_root;
while (index != m_nullNode)
{
insert_point = index;
if (key<index->key)
{
index = index->left;
}
else if (key>index->key)
{
index = index->right;
}
else
{
return false;
}
}
RB_Node* insert_node = new RB_Node();
insert_node->key = key;
insert_node->data = data;
insert_node->RB_COLOR = RED;
insert_node->right = m_nullNode;
insert_node->left = m_nullNode;
if (insert_point == m_nullNode) //empty tree
{
m_root = insert_node;
m_root->parent = m_nullNode;
m_nullNode->left = m_root;
m_nullNode->right = m_root;
m_nullNode->parent = m_root;
}
else
{
if (key<insert_point->key)
{
insert_point->left = insert_node;
}
else
{
insert_point->right = insert_node;
}
insert_node->parent = insert_point;
}
InsertFixUp(insert_node);
return true;
}
void InsertFixUp(RB_Node* node)
{
while (node->parent->RB_COLOR == RED)
{
if (node->parent == node->parent->parent->left)
{
RB_Node* uncle = node->parent->parent->right;
if (uncle->RB_COLOR == RED)
{
node->parent->RB_COLOR = BLACK;
uncle->RB_COLOR = BLACK;
node->parent->parent->RB_COLOR = RED;
node = node->parent->parent;
}
else if (uncle->RB_COLOR == BLACK)
{
if (node == node->parent->right)
{
node = node->parent;
RotateLeft(node);
}
node->parent->RB_COLOR = BLACK;
node->parent->parent->RB_COLOR = RED;
RotateRight(node->parent->parent);
}
}
else
{
RB_Node* uncle = node->parent->parent->left;
if (uncle->RB_COLOR == RED)
{
node->parent->RB_COLOR = BLACK;
uncle->RB_COLOR = BLACK;
uncle->parent->RB_COLOR = RED;
node = node->parent->parent;
}
else if (uncle->RB_COLOR == BLACK)
{
if (node == node->parent->left)
{
node = node->parent;
RotateRight(node);
}
else
{
node->parent->RB_COLOR = BLACK;
node->parent->parent->RB_COLOR = RED;
RotateLeft(node->parent->parent);
}
}
}
}
m_root->RB_COLOR = BLACK;
}
bool RotateLeft(RB_Node* node)
{
if (node == m_nullNode || node->right == m_nullNode)
{
return false;//can't rotate
}
RB_Node* lower_right = node->right;
lower_right->parent = node->parent;
node->right = lower_right->left;
if (lower_right->left != m_nullNode)
{
lower_right->left->parent = node;
}
if (node->parent == m_nullNode) //rotate node is root
{
m_root = lower_right;
m_nullNode->left = m_root;
m_nullNode->right = m_root;
//m_nullNode->parent = m_root;
}
else
{
if (node == node->parent->left)
{
node->parent->left = lower_right;
}
else
{
node->parent->right = lower_right;
}
}
node->parent = lower_right;
lower_right->left = node;
return true;
}
bool RotateRight(RB_Node* node)
{
if (node == m_nullNode || node->left == m_nullNode)
{
return false;//can't rotate
}
RB_Node* lower_left = node->left;
node->left = lower_left->right;
lower_left->parent = node->parent;
if (lower_left->right != m_nullNode)
{
lower_left->right->parent = node;
}
if (node->parent == m_nullNode) //node is root
{
m_root = lower_left;
m_nullNode->left = m_root;
m_nullNode->right = m_root;
//m_nullNode->parent = m_root;
}
else
{
if (node == node->parent->right)
{
node->parent->right = lower_left;
}
else
{
node->parent->left = lower_left;
}
}
node->parent = lower_left;
lower_left->right = node;
return true;
}
bool Delete(KEY key)
{
RB_Node* delete_point = find(key);
if (delete_point == m_nullNode)
{
return false;
}
if (delete_point->left != m_nullNode && delete_point->right != m_nullNode)
{
RB_Node* successor = InOrderSuccessor(delete_point);
delete_point->data = successor->data;
delete_point->key = successor->key;
delete_point = successor;
}
RB_Node* delete_point_child;
if (delete_point->right != m_nullNode)
{
delete_point_child = delete_point->right;
}
else if (delete_point->left != m_nullNode)
{
delete_point_child = delete_point->left;
}
else
{
delete_point_child = m_nullNode;
}
delete_point_child->parent = delete_point->parent;
if (delete_point->parent == m_nullNode)//delete root node
{
m_root = delete_point_child;
m_nullNode->parent = m_root;
m_nullNode->left = m_root;
m_nullNode->right = m_root;
}
else if (delete_point == delete_point->parent->right)
{
delete_point->parent->right = delete_point_child;
}
else
{
delete_point->parent->left = delete_point_child;
}
if (delete_point->RB_COLOR == BLACK && !(delete_point_child == m_nullNode && delete_point_child->parent == m_nullNode))
{
DeleteFixUp(delete_point_child);
}
delete delete_point;
return true;
}
void DeleteFixUp(RB_Node* node)
{
while (node != m_root && node->RB_COLOR == BLACK)
{
if (node == node->parent->left)
{
RB_Node* brother = node->parent->right;
if (brother->RB_COLOR == RED)
{
brother->RB_COLOR = BLACK;
node->parent->RB_COLOR = RED;
RotateLeft(node->parent);
}
else
{
if (brother->left->RB_COLOR == BLACK && brother->right->RB_COLOR == BLACK)
{
brother->RB_COLOR = RED;
node = node->parent;
}
else if (brother->right->RB_COLOR == BLACK)//left red and right black
{
brother->RB_COLOR = RED;
brother->left->RB_COLOR = BLACK;
RotateRight(brother);
}
else if (brother->right->RB_COLOR == RED)
{
brother->RB_COLOR = node->parent->RB_COLOR;
node->parent->RB_COLOR = BLACK;
brother->right->RB_COLOR = BLACK;
RotateLeft(node->parent);
node = m_root;
}
}
}
else
{
RB_Node* brother = node->parent->left;
if (brother->RB_COLOR == RED)
{
brother->RB_COLOR = BLACK;
node->parent->RB_COLOR = RED;
RotateRight(node->parent);
}
else
{
if (brother->left->RB_COLOR == BLACK && brother->right->RB_COLOR == BLACK)
{
brother->RB_COLOR = RED;
node = node->parent;
}
else if (brother->left->RB_COLOR == BLACK)
{
brother->RB_COLOR = RED;
brother->right->RB_COLOR = BLACK;
RotateLeft(brother);
}
else if (brother->left->RB_COLOR == RED)
{
brother->RB_COLOR = node->parent->RB_COLOR;
node->parent->RB_COLOR = BLACK;
brother->left->RB_COLOR = BLACK;
RotateRight(node->parent);
node = m_root;
}
}
}
}
m_nullNode->parent = m_root;
node->RB_COLOR = BLACK;
}
inline RB_Node* InOrderPredecessor(RB_Node* node)
{
if (node == m_nullNode) //null node has no predecessor
{
return m_nullNode;
}
RB_Node* result = node->left; //get node's left child
while (result != m_nullNode) //try to find node's left subtree's right most node
{
if (result->right != m_nullNode)
{
result = result->right;
}
else
{
break;
}
} //after while loop result==null or result's right child is null
if (result == m_nullNode)
{
RB_Node* index = node->parent;
result = node;
while (index != m_nullNode && result == index->left)
{
result = index;
index = index->parent;
}
result = index; // first right parent or null
}
return result;
}
inline RB_Node* InOrderSuccessor(RB_Node* node)
{
if (node == m_nullNode) //null node has no successor
{
return m_nullNode;
}
RB_Node* result = node->right; //get node's right node
while (result != m_nullNode) //try to find node's right subtree's left most node
{
if (result->left != m_nullNode)
{
result = result->left;
}
else
{
break;
}
} //after while loop result==null or result's left child is null
if (result == m_nullNode)
{
RB_Node* index = node->parent;
result = node;
while (index != m_nullNode && result == index->right)
{
result = index;
index = index->parent;
}
result = index; //first parent's left or null
}
return result;
}
void InOrderTraverse()
{
ofstream outfile;
outfile.open(".\\word.txt");
outfile.close();
InOrderTraverse(m_root);
}
void InOrderTraverse(RB_Node* node)
{
if (node == m_nullNode)
{
return;
}
else
{
InOrderTraverse(node->left);
ofstream outfile;
outfile.open(".\\word.txt", ios::app);
CStringA sa(node->key);
CStringA sb(node->data);
outfile << sa << " " << sb << endl;
outfile.close();
InOrderTraverse(node->right);
}
}
~RB_Tree()
{
clear(m_root);
delete m_nullNode;
}
private:
void clear(RB_Node* node)
{
if (node == m_nullNode)
{
return;
}
else
{
clear(node->left);
clear(node->right);
delete node;
}
}
private:
RB_Node *m_nullNode;
RB_Node *m_root;
};

MFC拾遗的更多相关文章

  1. vs2010静态链接MFC库报链接错误

    由于需要将MFC程序在其它电脑上运行,所以需要将动态链接的MFC改成静态链接,本以为很简单,没想到链接的时候出现下面的链接错误: uafxcw.lib(afxmem.obj) : error LNK2 ...

  2. MFC中成员变量的声明顺序与析构顺序

    第一次用博客,第一篇随笔,就写今天遇到的一个问题吧. 在VS2008的MFC对话框程序,窗口成员变量的声明顺序与其析构顺序相反,即,先声明的变量后析构,后声明的变量先析构.未在其他模式下测试. cla ...

  3. VC中的MFC到底是什么?

    1. 微软基础类库(英语:Microsoft Foundation Classes,简称MFC)是一个微软公司提供的类库(class libraries),以C++类的形式封装了Windows API ...

  4. MFC&Halcon之实时视频监控

    上一篇实现了在MFC的窗体内显示图片,本篇介绍如何在MFC窗体内实时显示摄像头的影像. 要实现的功能是点击一个“开始”按钮,可以显示影像,再点击“停止”按钮,可以停止显示. 因为实时显示影像需要在一个 ...

  5. Redis命令拾遗二(散列类型)

    本文版权归博客园和作者吴双共同所有,欢迎转载,转载和爬虫请注明原文地址 :博客园蜗牛NoSql系列地址  http://www.cnblogs.com/tdws/tag/NoSql/ Redis命令拾 ...

  6. 基础拾遗------redis详解

    基础拾遗 基础拾遗------特性详解 基础拾遗------webservice详解 基础拾遗------redis详解 基础拾遗------反射详解 基础拾遗------委托详解 基础拾遗----- ...

  7. MFC快速入门 - 菜单

    本文仅用于学习交流,商业用途请支持正版!转载请注明:http://www.cnblogs.com/mxbs/p/6231104.html 打开VS2010,依次打开File – New – Proje ...

  8. MFC画线功能总结

    本文仅用于学习交流,商业用途请支持正版!转载请注明:http://www.cnblogs.com/mxbs/p/6216464.html MFC画线功能要点有二:其一,鼠标按下时记录初始位置为线的起始 ...

  9. MFC消息映射机制以及画线功能实现

    ---此仅供用于学习交流,切勿用于商业用途,转载请注明http://www.cnblogs.com/mxbs/p/6213404.html. 利用VS2010创建一个单文档标准MFC工程,工程名为Dr ...

随机推荐

  1. Docker学习资料汇总

    一.Docker问答录(100问)  链接:https://blog.lab99.org/post/docker-2016-07-14-faq.html 二.Windows 10 如何安装Docker ...

  2. Jenkins中启动从节点时,出现问题如何解决,问题:No Known Hosts...

    Jenkins中,启动从节点时,出现如下问题如何解决:/root/.ssh/known_hosts [SSH] No Known Hosts file was found at /root/.ssh/ ...

  3. python读xml文件

    # -*- coding:utf-8 -*- import jsonimport requestsimport os curpath=os.path.dirname(os.path.realpath( ...

  4. Redis相关注意事项

    本文介绍了五个使用Redis使用时的注意事项.如果你在使用或者考虑使用Redis,你可以学习一下下面的一些建议,避免遇到以下提到的问题. 一.配置相关注意事项 1.涉及到内存的单位注意添加 b 1k ...

  5. 洛谷 P2383 狗哥玩木棒

    题目背景 狗哥又趁着语文课干些无聊的事了... 题目描述 现给出一些木棒长度,那么狗哥能否用给出的木棒(木棒全用完)组成一个正方形呢? 输入输出格式 输入格式: 输入文件中的第一行是一个整数n表示测试 ...

  6. 协议详解3——IP

    1. 特点: 所有的TCP,UDP,ICMP,IGMP数据都以IP数据报格式传输.  提供不可靠,无连接服务. 不可靠: 不能保证IP数据报能成功到达目的.IP仅提供最好的传输服务.如果发生某种错误时 ...

  7. ABAP system landscape和vue项目webpack构建的最佳实践

    基于Netweaver的ABAP transport route一般都有dev,test和prod三种类型的系统. 而Vue前端项目的webpack build设置也类似. 以SAP成都研究院数字创新 ...

  8. vue validate多表单验证思考 之前写过一个里外层,现在觉得不合适,应该平行的写,然后都给ret,最后判断ret 再做出反应,这样整体表单的所有验证就都报验证,然后最后提交的时候把组件内的对象合并到总的对象,再提交

    vue validate多表单验证思考 之前写过一个里外层,现在觉得不合适,应该平行的写,然后都给ret,最后判断ret 再做出反应,这样整体表单的所有验证就都报验证,然后最后提交的时候把组件内的对象 ...

  9. python 基础之格式化输出

    字符占位符%s #_cvvh:"chenxi" #date: 2019/6/24 print ('chhjg') # 格式化输出 name = input("Name:& ...

  10. php的字符转换 & php登入注册界面设计以及源码 & 分离公共部分

    我们在编写的时候总是会出现乱码 https://www.cnblogs.com/mafeng/p/5827215.html php登入注册界面设计以及源码 https://blog.csdn.net/ ...