标签:复习c++长文
/* 文档较长 注意这分区 文档包括 哈希表平面点距 字典项目 排序等 */ ☆★☆★☆★☆★☆★☆★☆★☆★☆★☆ ★ │ 心想 │ 事成 │ ☆ ╭═╮ ★ ☆ ★ ☆ ╰═╯ ★☆★☆★☆★☆★☆★☆★☆★☆★☆★. {@} {@} * {@} {@} * {@} * {@} {@}* {@} * {@} * {@} \ {@} * {@} * {@} / \ \ \ l / / / \ \ \ Y / / / \ \ l / / \\Y// >=< //㊣\\ LIST size 遍历出值 vector size 指针相减 http://alidomo.3vdo.net memory不可以 出现 string对象 str 均指向 同一空间 原位置删除后 就没有数据 类型萃取 引用计数 或者深拷贝 解决 /*********************** c++全特化 与偏特化 http://www.jb51.net/article/56004.htm 萃取 http://blog.163.com/wslngcjsdxdr@126/blog/static/1621962302014931114833331/ ***********************/ #include <iostream> using namespace std; class BirdType{};/*鸟类*/ class InsectType{}; /* 昆虫类*/ class BeastType{};/*走兽类*/ template <typename T> class Animal { template <typename A> class Traits {}; template <> class Traits<BirdType> { public: void fly() { cout<<"Birder fly "<<endl;} }; template <> class Traits<InsectType> { public: void fly() { cout<<"Insect fly "<<endl;} }; template <> class Traits<BeastType> { public: void fly() { cout<<"Beast can‘t fly "<<endl; } }; public: void Fly() { Traits<T>().fly(); }; }; int main() { Animal<BirdType> brider; brider.Fly(); Animal<InsectType> insect; insect.Fly(); Animal<BeastType> beast; beast.Fly(); return 0; } #pragma warning(disable : 4786) /* http://blog.csdn.net/eof_2011/article/details/7256127 */ #include <iostream> #include <string.h> #include <cstdlib> #include <cctype> #include <ctime> #include <map> #include <fstream> #include <string> #include <cctype> using namespace std; struct data { string EN; string CN; }; data DT; int choice; bool t = false; string str1, str2, str3; string strr = " .txt"; map<string, string> dictmap; map<string, string> dictmapp; string str = "z.txt"; const char *p; int count = 0; int Read(); void Menu(); void Do(); int main(); void Translate(); void Add(); void Search(); void Change(); void Save( const char *p ); int Read() { p = str.c_str(); cout << p << endl; FILE *fp; fp = fopen( p, "r" ); char Line[1024]; char *ppos = NULL; while ( fgets( Line, 1024, fp ) != NULL ) { if ( (ppos = strchr( Line, ‘ ‘ ) ) == NULL && (ppos = strchr( Line, ‘\t‘ ) ) == NULL ) { continue; } *ppos++ = ‘\0‘; if ( ppos[strlen( ppos ) - 1] = ‘\n‘ ) { ppos[strlen( ppos ) - 1] = ‘\0‘; } DT.EN = Line; DT.CN = ppos; map<string, string>::iterator it; if ( (it = dictmap.find( DT.EN ) ) != dictmap.end() ) { dictmap.erase( DT.EN ); } dictmap.insert( make_pair( DT.EN, DT.CN ) ); } map<string, string>::iterator it; for ( it = dictmap.begin(); it != dictmap.end(); it++ ) { cout << it->first.c_str() << "****" << it->second.c_str() << endl; } /* system("pause"); */ fclose( fp ); return(0); } void Save( const char *p ) { ofstream fout( p ); map<string, string>::iterator it; for ( it = dictmap.begin(); it != dictmap.end(); it++ ) { fout << it->first.c_str() << " " << it->second.c_str() << endl; } fout.close(); cout << "保存成功 !" << endl; } void Add() { cout << "请输入两个字符串,以空格分隔:" << endl; cin >> str1 >> str2; map<string, string>::iterator mapIter; if ( (mapIter = dictmap.find( str1 ) ) != dictmap.end() ) { cout << "此词已经存在!" << endl; }else { dictmap.insert( make_pair( str1, str2 ) ); t = true; } } void Search() { cout << "请输入需要查找的单词(输入不要带空格):" << endl; cin >> str1; cout << str1 << endl; /* 转换大小写 并重新读文件填充map */ char ss = str1[0] - 0; /* if(ss==" "){cout<<"输入带空格"<<endl; return;} */ ss = tolower( str1[0] ); str[0] = ss; if ( str1[0] != strr[0] ) { dictmap.clear(); Read(); strr = str1; cout << " not same" << endl; } map<string, string>::iterator mapIter; if ( (mapIter = dictmap.find( str1 ) ) != dictmap.end() ) { cout << "\"" << str1 << "\"" << " 在字典中翻译为: " << "\"" << mapIter->second.c_str() << "\"" << endl; }else cout << "此词不存在字典中!" << endl; } void Menu() { /* system("cls"); */ cout << "\t================================================" << endl; cout << "\t* *" << endl; cout << "\t* 1、翻译文档 *" << endl; cout << "\t* 2、添加单词 *" << endl; cout << "\t* 3、查找单词 *" << endl; cout << "\t* 4、修改字典 *" << endl; cout << "\t* 0、退 出 *" << endl; cout << "\t* *" << endl; cout << "\t================================================" << endl; cout << "请输入您的选项:"; cin >> choice; while ( choice < -1 || choice > 4 ) { cout << "输入错误!" << endl << "请重新输入:" << endl; cin >> choice; } Do(); } void Do() { char a; switch ( choice ) { case 1: /* system("cls"); */ Translate(); /* * system("pause"); * system("cls"); */ Menu(); break; case 2: /* system("cls"); */ Add(); /* * system("pause"); * system("cls"); */ Menu(); break; case 3: /* system("cls"); */ Search(); /* * system("pause"); * system("cls"); */ Menu(); break; case 4: /* system("cls"); */ Change(); /* * system("pause"); * system("cls"); */ Menu(); break; case 0: if ( t ) { cout << "是否保存 (Y/N)?" << endl; cin >> a; if ( a == ‘Y‘ || a == ‘y‘ ) { Save( p ); } } exit( 0 ); break; } } void Translate() { string str_1, str_2; const char *p1, *p2; for ( char i = ‘a‘; i <= ‘z‘ && count == 0; i++ ) { str[0] = i; Read(); map<string, string>::iterator it = dictmap.begin(); for (; it != dictmap.end(); ++it ) { dictmapp[it->first] = it->second; } dictmap.clear(); } count++; /* * map<string,string>::iterator it = dictmapp.begin(); * for(;it != dictmapp.end();++it) */ /* { cout<<it->first<<" "<<it->second<<endl; } */ cout << "请输入翻译原文路径:" << endl; cin >> str_1; p1 = str_1.c_str(); cout << "请输入译文存储路径:" << endl; cin >> str_2; p2 = str_2.c_str(); ifstream fin1( p1 ); ofstream fout2( p2 ); if ( !fin1 ) { cout << "打开翻译源文件失败 !" << endl; } if ( !fout2 ) { cout << "创建译文文件失败 !" << endl; } map<string, string>::iterator mapIter; string strText; while ( fin1 ) { fin1 >> strText; if ( (mapIter = dictmapp.find( strText ) ) != dictmapp.end() ) { fout2 << mapIter->second.c_str() << "\t"; }else { fout2 << strText.c_str() << "\t"; } } cout << "翻译成功 !" << endl; } void Change() { string chgf, chgt; cout << "请输入您要替换的单词:" << endl; cin >> chgf; map<string, string>::iterator mapIter; if ( (mapIter = dictmap.find( chgf ) ) != dictmap.end() ) { cout << "\"" << chgf << "\"" << " 在字典中翻译为: " << "\"" << mapIter->second.c_str() << "\"" << endl; cout << "请输入您所要翻译成的意思:"; cin >> chgt; dictmap.erase( chgf ); dictmap.insert( make_pair( chgf, chgt ) ); cout << "修改成功!" << endl; cout << "\"" << chgf << "\"" << " 在字典中翻译成为: " << "\"" << chgt << "\"" << endl; t = true; }else { cout << "此词不存在字典中!" << endl; return; } } int main() { /* Read(); */ Menu(); return(1); } sql http://www.cnblogs.com/xiaoluo501395377/archive/2013/04/07/3003278.html sudo yum install mysql-devel (18个文件 5.5MB) V6 128 bite 16字节 MAC帧不分片 46字节到128字节 定义了数据 的最大传送单元 三位标志 表面后续是否有包裹 快递版本的ip讲解 ARP 作用在局域网有效 目标网络:目标主机 >msk(111) raip msk>ip 单对单 msk层扔掉 IP数据报 20-60字节 类里面的成员函数默认内联 10行内 省压栈 ctrl+F3 shift+F3 匿名构造补缺省值 node( T x=T()) { data=x; } Cd-->Cf d--> f Cd-!!->f 【单参数的构造函数支持类型强转】 struct AA { // explicit AA(int d):a(d){} AA(int d):a(d){} int a; }; AA a1(1); AA a2=2; int i=10; double & d=i; //证明 const double & d=i; STL解决迭代器失效的统一解法 http://blog.csdn.net/My_heart_ //http://blog.csdn.net/yanxiaolx/article/details/51986428 #include<iostream> #include<list> #include<cmath> #include <iomanip> #include<stdio.h> #include<stdlib.h> using namespace std; const int N = 9; double x[N] = {0,0,0,1,1,1,2,2,2}; double y[N] = {0,1,2,0,1,2,0,1,2}; double ds[N][N]= {0}; void Rand() { srand(time(0)); int n = 0; while(n < N) { x[n] = rand()%100; y[n] = rand()%100; ++n; } } class Point { public: Point(double x,double y,double ox,double oy) :startx(x),starty(y),endx(ox),endy(oy) {} Point(const Point& point) { startx = point.startx; starty = point.starty; endx = point.endx; endy = point.endy; } private: double startx; double starty; double endx; double endy; friend ostream& operator<<(ostream &out,Point &point); }; ostream& operator<<(ostream &out,Point &point) { out<<"("<<point.startx<<","<<point.starty<<")"<<" " <<"("<<point.endx<<","<<point.endy<<")"; return out; } template<class _Ty> class List { public: List(){} ~List(){} void add(const _Ty&point) { List_.push_back(point); } bool IsExist(double s) { it = List_.begin(); for(;it != List_.end();++it) { if(s == (*it).getdistence()) { return true; } } return false; } bool Insert(double s,Point point) { it = List_.begin(); for(;it != List_.end();++it) { if((*it).getdistence() == s) { (*it).add(point); return true; } } return false; } void disp() { it = List_.begin(); for(;it != List_.end();++it) { (*it).disp(); } cout<<endl; } private: list<_Ty> List_; typename list<_Ty>::iterator it; }; template<typename _Ty=Point> class SList { public: SList(double s=0):d(s){} ~SList(){} void add(const _Ty&point) { slist.push_back(point); } double getdistence() { return d; } void disp() { it = slist.begin(); cout<<"距离:"<<d<<endl; for(;it != slist.end();++it) { cout<<*it<<endl; } } private: double d; list<_Ty> slist; typename list<_Ty>::iterator it; }; void calculate() { double d = 0; for(int i=0;i < N;++i) { for(int j=i+1;j < N;++j) { d = sqrt((x[i]-x[j])*(x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j])); ds[i][j] = d; } } } void show() { for(int i=0;i < N;++i) { for(int j=0;j < N;++j) { cout<<setw(6)<<ds[i][j]<<" "; } cout<<endl; } } int main() { List<SList<Point> > List; //Rand(); calculate(); show(); for(int i=0;i < N;++i) { for(int j=0;j < N;++j) { if(ds[i][j] != 0 ) { if(!List.IsExist(ds[i][j])) { List.add(SList<Point>(ds[i][j])); } List.Insert(ds[i][j],Point(x[i],y[i],x[j],y[j])); } } } List.disp(); return 0; } int x=1; x=1,3,4; cout<<x<<endl;// 1 // http://noalgo.info/793.html #include <cstdio> #include <algorithm> #include <cmath> using namespace std; const int mxsz = 100000 + 10; //最大点数 const double oo = 1e20; //无穷大距离 struct Point { double x, y; Point(double a = 0, double b = 0) : x(a), y(b) {} } v[mxsz], tem[mxsz]; //点集, 中间点集 bool cmpByX (const Point &p1, const Point &p2) { return p1.x < p2.x; } bool cmpByY (const Point &p1, const Point &p2) { return p1.y < p2.y; } //计算欧几里得距离 inline double calDis(const Point &p1, const Point &p2) { return sqrt((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y)); } //寻找下标在[st, ed]之间的最近点对 double closestPair(int st, int ed, int &p1, int &p2) { double dis = oo, temdis; if (st >= ed) return dis; int mid = st + ((ed - st) >> 1), t1, t2; if ((temdis = closestPair(st, mid, t1, t2)) < dis) //左边 dis = temdis, p1 = t2, p2 = t2; if ((temdis = closestPair(mid+1, ed, t1, t2)) < dis)//右边 dis = temdis, p1 = t1, p2 = t2; //寻找距离中间x坐标小于dis的点,并按Y坐标排序 int len = 0; for (int i = st; i <= ed; i++) if (fabs(v[i].x - v[mid].x) < dis) tem[len++] = v[i]; sort(tem, tem + len, cmpByY); //考虑每个点附近至多8个点,由距离控制 for (int i = 0; i < len; i++) for (int j = i + 1; j < len && tem[j].y - tem[i].y <= dis; j++) if ((temdis = calDis(tem[i], tem[j])) < dis) dis = temdis, p1 = i, p2 = j; return dis; } int main() { //输入n个点,并按X坐标排序 int n; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%lf%lf", &v[i].x, &v[i].y); sort(v, v + n, cmpByX); int p1, p2; double dis = closestPair(0, n - 1, p1, p2); printf("Cloest Pair:(%lf,%lf)-(%lf,%lf) %.2lf\n", v[p1].x, v[p1].y, v[p2].x, v[p2].y, dis/2); } #include<iostream> #include<queue> using namespace std; int partition(int *arr,int low,int high) { int tmp = arr[low]; while(low < high) { while(low < high && tmp <= arr[high]) --high; arr[low] = arr[high]; while(low < high && tmp >= arr[low]) ++low; arr[high] = arr[low]; } arr[low] = tmp; return low; } void Quick(int *arr,int low,int high) { if(low < high) { int m = partition(arr,low,high); Quick(arr,low,m-1); Quick(arr,m+1,high); } } void QuickSort(int *arr,int n) { if(arr == NULL || n < 0) return ; Quick(arr,0,n-1); } void Quickloopsort(int *arr,int n) { if(arr == NULL ||n < 0) return ; int top = 0; // int *stack = (int *)malloc(sizeof(int)*n); int stack[n]; int tmp; int low = 0; int high = n-1; stack[top++] = low; stack[top++] = high; while(top != 0) { high = stack[--top]; low = stack[--top]; tmp = arr[low]; int m = partition(arr,low,high); if(low < m-1) { stack[top++] = low; stack[top++] = m-1; } if(m+1 < high) { stack[top++] = m+1; stack[top++] = high; } } } void BubbleSort(int *arr,int len) { int flag = 1; int i,j; for(i=0;i < len-1;++i) { for(j=0;j < len-1-i;++j) { if(arr[j] > arr[j+1]) { arr[j] ^= arr[j+1]; arr[j+1] ^= arr[j]; arr[j] ^= arr[j+1]; flag = 0; } } if(flag == 1) break; flag = 1; } } void SwapSort(int *arr,int len) { int i,j; for(i=0;i < len-1;++i) { for(j=i+1;j < len;++j) { if(arr[j] < arr[i]) { arr[j] ^= arr[i];arr[i] ^= arr[j];arr[j] ^= arr[i];} } } } void ChooseSort(int *arr,int len) { int min; int i,j; for(i=0;i < len;++i) { min = i; for(j=i+1;j < len;++j) { if(arr[j] < arr[min]){ min = j;} } if(min != i) { arr[min] ^= arr[i]; arr[i] ^= arr[min]; arr[min] ^= arr[i]; } } } void Insert_sort(int *arr,int len,int gap) { int tmp; int i,j; for(i=gap;i < len;i += gap) { tmp = arr[i]; for(j=i-gap;j >= 0;j -= gap) { if(arr[j] <= tmp) { break; } arr[j+gap] = arr[j]; } arr[j+gap] = tmp; } } void Shell_Sort(int *arr,int len) { int gap[] = {4,2,1}; int i=0; for(;i < 3;++i) Insert_sort(arr,len,gap[i]); } void swap(int *a,int *b){ *a ^= *b; *b ^= *a;*a ^= *b;} void head_adjust(int *arr,int start,int end) { int tmp = arr[start]; int i = 2*start+1; while(i <= end) { if(i+1 <= end && arr[i+1] > arr[i]) { ++i; } if(arr[i] > tmp) { arr[start] = arr[i]; } else { break; } start = i; i = 2*start+1; } arr[start] = tmp; } void heapsort(int *arr,int len) { if(arr == NULL || len < 0) return; int i; for(i=len/2-1;i >= 0;--i) { head_adjust(arr,i,len-1); } swap(&arr[0],&arr[len-1]); for(i=len-2;i > 0;--i) { head_adjust(arr,0,i); swap(&arr[0],&arr[i]); } } void merge(int *arr,int len,int gap) { // int *brr = (int *)malloc(sizeof(int)*len); int brr[len]; int left = 0; int left_end = left+gap-1; int right = left_end+1; int right_end = right+gap-1 >= len?len-1:right+gap-1; int i = 0; int j = 0; while(right < len) { while(left <= left_end && right <= right_end) { if(arr[left] <= arr[right]) { brr[i++] = arr[left++]; } else { brr[i++] = arr[right++]; } } while(left <= left_end) { brr[i++] = arr[left++]; } while(right <= right_end) { brr[i++] = arr[right++]; } left = right_end+1; left_end = left+gap-1; right = left_end+1; right_end = right+gap-1 >= len?len-1:right+gap-1; } while(left < len && left <= left_end) { brr[i++] = arr[left++]; } for(;j < len;++j) { arr[j] = brr[j]; } } void mergesort(int *arr,int len) { if(arr == NULL || len < 0) return ; int i = 1; for(;i < len;i*=2) { merge(arr,len,i); } } void Print(int *arr,int n) { for(int i=0;i < n;++i) { cout<<arr[i]<<" "; } cout<<endl; } void Base(int *arr,int len,int n) { queue<int> que[10]; int m = 1; int index = 0; for(int i=0;i < n;++i) { for(int j=0;j < len;++j) { index = (arr[j]/m)%10; que[index].push(arr[j]); } int r = 0; for(int k=0;k < 10;++k) { while(!que[k].empty()) { arr[r] = que[k].front(); que[k].pop(); ++r; } } m *= 10; } } void basesort(int *arr,int len) { if(arr == NULL || len < 0) return; int max = arr[0]; int n = 1; for(int i=1;i < len;++i) { if(arr[i] > max) { max = arr[i]; } } while((max /= 10) != 0) { ++n; } Base(arr,len,n); } int main() { //int arr[] = {6,1,17,89,23,56,76,9,93,86}; int arr[] = {1,2,9,5,8,6,4,3,7}; int n = sizeof(arr)/sizeof(arr[0]); // QuickSort(arr,n); // QuickloopSort(arr,n); // BubbleSort(arr,n); // SwapSort(arr,n); // ChooseSort(arr,n); // Insert_sort(arr,n); // Shell_Sort(arr,n); // Quickloopsort(arr,n); // heapsort(arr,n); // mergesort(arr,n); // Print(arr,n);/ // basesort(arr,5); // Print(arr,5); return 0; } #include <stdio.h> #include <stdlib.h> #include <string.h> /* 函数功能:以char类型交换两个元素,大小为size。 */ void swap(char *p1,char *p2,int size) { int i = 0; char tmp = 0; for(i = 0;i<size;i++) { tmp = *(p1+i); *(p1+i) = *(p2+i); *(p2+i) = tmp; } } /* 函数功能:比较整数的回调函数。 */ int compare_int(const void *elem1,const void *elem2) { return (*(int *)elem1) - (*(int *)elem2); } /* 函数功能:比较字符串的回调函数。 */ int compare_str(const void *elem1,const void *elem2) { return strcmp((char *)(*(int *)elem1), (char *)(*(int *)elem2)); } /* 函数功能:冒泡排序函数。 */ void bubble(void *base,int size,int width,int ( *cmp )( void *elem1, void *elem2 ))//const { int i = 0; int j = 0; for(i = 0;i<size-1;i++) { for(j = 0;j<size-i-1;j++) { if(cmp(((char *)base+(j*width)),((char *)base+(j+1)*width))<0) { swap((char *)base+(j*width),(char *)base+(j+1)*width,width); } } } } int funint() { int arr_int[] = {1,3,5,7}; int i = 0; int size = sizeof(arr_int)/sizeof(arr_int[0]); bubble(arr_int,size,sizeof(arr_int[0]),compare_int); for(i = 0;i<size_int;i++) printf("%d\n ",arr_int[i]); } void funstring() { char *str[] = {"ddddd","aaaaa","ccccc","bbbbb","zzzzz"}; int i = 0; int size= sizeof(arr_str)/sizeof(arr_str[0]); bubble(str,size,sizeof(str[0]),compare_str); for(i = 0;i<size;i++) printf("%s\n",str[i]); } int main() { funint(); funstring(); return 0; }
标签:复习c++长文
原文地址:http://wzsts.blog.51cto.com/10251779/1841211