c++ 编程规范
2016-05-13 19:08
316 查看
以前敲ACM时习惯了单文件,全局变量的风格,要努力向规范看齐
这是一个的数据库中间件程序,软件杯题目
这是一个的数据库中间件程序,软件杯题目
自己以前的风格
上面的是中间件程序,下面的是单元测试memDB.cpp
#include <cstdio> #include <iostream> #include <vector> #include <map> #include<time.h> using namespace std; #define MAX 200000 struct Index{ // 卡口每次的记录索引 int time; int index; public: Index(int time, int index){ this -> time = time; this -> index = index; } }; struct Record { // 卡口每次记录的数据类型 string car; int posx; int posy; int time; void addrd(string car, int x, int y, int time){ this -> car = car; this -> posx = x; this -> posy = y; this -> time = time; } }; typedef struct { // 记录5分钟内的所有记录 Record rd[MAX * 10]; // record }Record_Of_Five; struct Global{ int idx = 0; // 五分钟内的记录索引 Record_Of_Five *rdf; // record_of_five Record_Of_Five *rdfbk; //另外备份的5分钟指针 map<int, string> mapstr; // 将车牌号 映射为整形 vector<Index> vec[MAX]; //根据车牌号索引卡口记录 map<string, vector<Index> > mapvec; // 为每辆车创建一个vector,存放它经过卡口的记录的索引 int term_five; // 记录是第几个五分钟 }; void init(Global &global){ global.rdf = new Record_Of_Five(); global.rdfbk = new Record_Of_Five(); // 初始化 map<int ,string> 将 车牌号和 整型映射 char buf[10]; for(int i = 0; i < MAX; i++) { int j = 1000000 + i + 1; sprintf(buf, "%d", j); string str = "A"; for(int ii = 1; ii < 7; ii++) str += buf[ii]; global.mapstr[i] = str; } // 初始化 map<string, vector> for(int i = 0; i < MAX; i++){ global.mapvec[(global.mapstr)[i]] = (global.vec)[i]; } } bool insert(Global &global, string str, int x, int y, int time) { int term_five_tmp = time / 300; if(term_five_tmp != global.term_five){ // 判断当前的5分钟是否满了 global.term_five = term_five_tmp; Record_Of_Five *tmp = global.rdf; global.rdf = global.rdfbk; global.rdfbk = tmp; global.idx = 0; } global.rdf -> rd[(global.idx)++].addrd(str, x, y, time); Index index(time, global.idx - 1); global.mapvec[str].push_back(index); return true; } vector<Index> select(Global &global, string car) { vector<Index> tmp = global.mapvec[car]; int len = tmp.size(); for(int i = 0; i < len; i++){ cout << tmp[i].index; } return global.mapvec[car]; }
unitTest.cpp
#include <cstdio> #include "middleware.cpp" using namespace std; int main() { Global global; init(global); clock_t start_time=clock(); for(int i = 0; i < 200000; i++){ string car = global.mapstr[i]; // cout << car << endl; for(int j = 0; j < 5; j++) { // 共测试 i.max * j.max 次数据 insert(global, car, j, 1, 1); } } cout << "当前已经insert数目: " << global.idx << endl; // insert("A000001", 1, 1, 1); // insert("A000002", 1, 1, 1); // insert("A000001", 1, 1, 1); // insert("A000001", 1, 1, 1); // cout << mapvec["A000001"].at(1).index << endl; // cout << rdf -> rd[199998].time << endl; clock_t end_time=clock(); cout<< "Running time is: "<<static_cast<double>(end_time-start_time)/CLOCKS_PER_SEC*1000<<"ms"<<endl;//输出运行时间 vector<Index> tmp = select(global, "A000002"); return 0; }
规范代码
出自叶大神之手,后来我改了一些memDB.h
一些结构体的先后顺序别弄乱了#pragma once #include <vector> #include <map> #define MAX 200000 namespace memDB { using namespace std; struct Record { string car; int posx; int posy; int time; void addrd(string car, int x, int y, int time) { this->car = car; this->posx = x; this->posy = y; this->time = time; } }; struct Record_Of_Five{ // 记录5分钟内的所有记录 Record rd[MAX * 10]; // record }; struct Index { int time; int index; public: Index(int time, int index) { this->time = time; this->index = index; } }; struct Global{ int idx = 0; // 五分钟内的记录索引 Record_Of_Five *rdf; // record_of_five Record_Of_Five *rdfbk; //另外备份的5分钟指针 map<int, string> mapstr; // 将车牌号 映射为整形 vector<Index> vec[MAX]; //根据车牌号索引卡口记录 map<string, vector<Index> > mapvec; // 为每辆车创建一个vector,存放它经过卡口的记录的索引 int term_five; // 记录是第几个五分钟 }; void init(); bool insert(string str, int x, int y, int time); vector<Index> select(string car); }
memDB.cpp
// memDB.cpp : Defines the exported functions for the static library. // #include "memDB.h" #include <iostream> using namespace memDB; void init(Global &global){ global.rdf = new Record_Of_Five(); global.rdfbk = new Record_Of_Five(); // 初始化 map<int ,string> 将 车牌号和 整型映射 char buf[10]; for(int i = 0; i < MAX; i++) { int j = 1000000 + i + 1; sprintf(buf, "%d", j); string str = "A"; for(int ii = 1; ii < 7; ii++) str += buf[ii]; global.mapstr[i] = str; } // 初始化 map<string, vector> for(int i = 0; i < MAX; i++){ global.mapvec[(global.mapstr)[i]] = (global.vec)[i]; } } bool insert(Global &global, string str, int x, int y, int time) { int term_five_tmp = time / 300; if(term_five_tmp != global.term_five){ // 判断当前的5分钟是否满了 global.term_five = term_five_tmp; Record_Of_Five *tmp = global.rdf; global.rdf = global.rdfbk; global.rdfbk = tmp; global.idx = 0; } global.rdf -> rd[(global.idx)++].addrd(str, x, y, time); Index index(time, global.idx - 1); global.mapvec[str].push_back(index); return true; } vector<Index> select(Global &global, string car) { vector<Index> tmp = global.mapvec[car]; int len = tmp.size(); for(int i = 0; i < len; i++){ cout << tmp[i].index; } return global.mapvec[car]; }
相关文章推荐
- C++ list的基本操作和使用
- Mother's Milk
- C++的四舍五入函数
- 编译VC++6.0时出出现unexpected end of file while looking for precompiled header directived解决方法
- 静态链表(C++实现)
- C++primer plus 第九章习题
- C++中substr函数的用法
- 1004. 成绩排名 (20)
- 极客班GeekBand C++第一周笔记
- 关于在C/C++语言中,函数如何返回数组,数组如何作为参数传递以及返回数组的函数该如何调用问题的总结
- c++语法——类和对象的学习(1)
- LevenbergMarquardt 算法 eigen实现(c++)
- POJ 1159 Palindrome
- C++虚函数与JAVA中抽象函数比较 接口与抽象类比较
- 【C++】:C++的境界1
- c++中箭头操作符(->)和点操作符(.)的不同
- C++ vector多维数组初始化及清零
- c++中类的学习(二)
- C++ int转string
- c++ 非常量引用产生临时对象