大纲
变量和基本类型;
常量;
表达式,各种操作符等;
控制语句;
强制类型转换;
异常处理;
函数;
标准输入输出类;
类,封装性,继承性,多态性;
标准类;
泛型编程,类模板,函数模板;
标准容器类,泛型算法;
类是什么,类是对具有相同特性以及行为的一类对象的高度归纳,类是一种抽象数据类型;
快慢指针:多个指针,前进的步长不一样,例如快指针每步前进两个单位,慢指针每次前进1个单位;
使用快慢指针判断单链表是否为循环链表(快指针步长为2,慢指针步长为1);
fast=slow=head;
while (fast&&slow)
{
if (fast->next==NULL)
return slow ->data;
else if (fast->next!= NULL && fast->next->next== NULL)
return (slow ->data + slow ->next->data)/2;
else
{
fast= fast->next;
fast= fast->next;
slow = slow ->next;
}
}
const_cast强制类型转换,将const类型的的const属性去掉; 针对指针,非指针类型不管用 ,
例如:
有效:int a1=0; const int * a=&a1 ; int *b =const_cast<int*>(a ) ; *b=2 ; 编译运行均无错误;
编译错误: const int a=2; int b=const_cast<int>(a);
逻辑移位和算术移位
逻辑左移和算术左移相同 ;
c语言标准中只规定了无符号数的移位为逻辑移位;
对于有符号数移位操作应不同的编译器而不同 ,有的使用逻辑移位,有的使用算术移位;
逻辑右移移动符号位,高位补0;
算术右移不移动符号位,高位补符号位;
重载函数名不应声明在局部作用域,因为会屏蔽外层的重载函数;
volatile 是类型修饰符, 表示该数据经常会被无意修改, 每次读取该数据都是从重新到内存中去取而不是使用备份寄存器 ;
常用于多线程 并行 共享变量,或者一个子服务修改一个指向buffer 的指针;
赋值运算符必须定义为类的成员函数,因为这样才能实现多个不同版本赋值运算符的重载 ,如果定义为友元函数不能实现重载;
编程:接收任意个整数的输入,然后输出 :
#include<iostream>
#include<string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string inPut;
int inPutArr[100];
getline(cin,inPut);
int arrCout=0;
int result;
for(string::size_type index=0; index!=inPut.size();++index){
result=(inPut[index]-'0');
if(result>=0)
inPutArr[arrCout++]=result;
}
for(int ix=0;ix!=arrCout;++ix){
cout<<inPutArr[ix];
}
_getch();
return 0;
}
LCS(求所有公共最长子序列)蛮力法
// BiShi.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<string>
#include<iostream>
#include<conio.h>
#include<vector>
using namespace std;
//求所有公共最长子序列算法
//针对一个字符,求字符串中等于该字符的位置,并将位置存储在posArray中
void GetSameCharPosInStr(string str,char which,int *posArray)
{
int pos=0;
for(int index=0;index!=str.size();++index){
if(str[index]==which){
posArray[pos++]=index+1;
}
}
}
vector<string> LCS(string motStr,string subStr,int &longestLen)
{
int *posArr=NULL;
longestLen=0;
int startPos=-1;
vector<string> LongestSubStr;
for(int i=0;i!=motStr.size();++i){
/*
delete[] posArr;
posArr=new int[subStr.size()];
*/
//针对母串中的某个字符,在子串中寻找与其相同字符的位置,并存放在posArr数组中
free(posArr);
posArr=static_cast<int*>(calloc(subStr.size(),sizeof(int)));
GetSameCharPosInStr(subStr,motStr[i],posArr);
int posArrIndex=0;
//基于posArr数组中的元素表示的相同初始位置,继续比较子串中接下来的字符,更新最长串
while(posArrIndex!=subStr.size()&&posArr[posArrIndex]!=0){
int motStrStartPos=i;
int motStrendPos=i+1;
int subNextPos=posArr[posArrIndex];
//比较接下来的字符
while(subNextPos!=subStr.size()&&motStrendPos!=motStr.size()&&subStr[subNextPos]==motStr[motStrendPos]){
++motStrendPos;
++subNextPos;
}
if((motStrendPos-motStrStartPos)>longestLen){
longestLen=(motStrendPos-motStrStartPos);
startPos=motStrStartPos;
string tempStr;
tempStr.assign(motStr,motStrStartPos,longestLen);
LongestSubStr.clear();
LongestSubStr.push_back(tempStr);
}
else if((motStrendPos-motStrStartPos)==longestLen){
string tempStr;
tempStr.assign(motStr,motStrStartPos,longestLen);
LongestSubStr.push_back(tempStr);
}
++posArrIndex;
}
} //end for
return LongestSubStr;
}
int _tmain(int argc, _TCHAR* argv[])
{
string mottherStr,subStr,longestStr;
vector<string> longSubStr;
int len=0;
cout<<"please input the mother string:"<<endl;
cin>>mottherStr;
cout<<"please input the substring string:"<<endl;
cin>>subStr;
longSubStr=LCS(mottherStr,subStr,len);
cout<<"the longest is: "<<len<<"the longest substr is : "<<endl;
for(vector<string>::iterator ite=longSubStr.begin();ite!=longSubStr.end();++ite)
cout<<*ite<<endl;
_getch();
return 0;
}
//最长公共子序列长度,递归方法
int lengthStr1,lengthStr2;
string str1,str2;
int LCS(int beginPos_Str1,int beginPos_Str2)
{
if(beginPos_Str1>=lengthStr1||beginPos_Str2>=lengthStr2)
return 0;
else if(str1[beginPos_Str1]==str2[beginPos_Str2])
return (LCS(beginPos_Str1+1,beginPos_Str2+1)+1);
else
return ( LCS(beginPos_Str1+1,beginPos_Str2)>LCS(beginPos_Str1,beginPos_Str2+1)? LCS(beginPos_Str1+1,beginPos_Str2):LCS(beginPos_Str1,beginPos_Str2+1)) ;
}
int _tmain(int argc, _TCHAR* argv[])
{
/*
string mottherStr,subStr,longestStr;
vector<string> longSubStr;
int len=0;
cout<<"please input the mother string:"<<endl;
cin>>mottherStr;
cout<<"please input the substring string:"<<endl;
cin>>subStr;
longSubStr=LCS(mottherStr,subStr,len);
cout<<"the longest is: "<<len<<"the longest substr is : "<<endl;
for(vector<string>::iterator ite=longSubStr.begin();ite!=longSubStr.end();++ite)
cout<<*ite<<endl;
*/
cout<<"please input the mother string:"<<endl;
cin>>str1;
lengthStr1=str1.size();
cout<<"please input the substring string:"<<endl;
cin>>str2;
lengthStr2=str2.size();
cout<<LCS(0,0)<<endl;
_getch();
return 0;
}
//动态规划 LCS
// 动态规划法
// 初始化并且用子串长度填充比较数组,初始化并且用子串长度填充方向数组
void DynamicPlanLCS(const string &str1,const string &str2,int **compareArr,int **directionArr)
{
int row=str1.size();
int col=str2.size();
for(int i=0;i!=row+1;++i)
for(int j=0;j!=col+1;++j)
compareArr[i][j]=0;
for(int i=0;i!=row+1;++i)
for(int j=0;j!=col+1;++j)
directionArr[i][j]=-1;
for(int i=1;i!=row+1;++i)
for(int j=1;j!=col+1;++j){
if(str1[i-1]==str2[j-1]){
compareArr[i][j]=compareArr[i-1][j-1]+1;
directionArr[i][j]=1; // 从左上角获得
}
else if(compareArr[i][j-1]>=compareArr[i-1][j]){
compareArr[i][j]=compareArr[i][j-1];
directionArr[i][j]=0; // 从左边获得
}
else {
compareArr[i][j]=compareArr[i-1][j];
directionArr[i][j]=2; // 从上边获得
}
}
}
//回溯方向数组,递归打印最长公共子序列
void PrintLCS(int **directionArr,const string &str1 ,int row,int col)
{
if(0==row||0==col)
return ;
else if(1==directionArr[row][col]) {
cout<<str1[row-1];
PrintLCS(directionArr,str1,row-1,col-1);
}
else if(0==directionArr[row][col]) {
PrintLCS(directionArr,str1,row,col-1);
}
else if(2==directionArr[row][col])
PrintLCS(directionArr,str1,row-1,col);
}
int _tmain(int argc, _TCHAR* argv[])
{
string str1,str2;
int lengthStr1=0,lengthStr2=0;
cout<<"please input str1:"<<endl;
cin>>str1;
lengthStr1=str1.size();
cout<<"please input str2:"<<endl;
cin>>str2;
lengthStr2=str2.size();
int **compareArr=NULL,**directionArr=NULL;
compareArr=new int*[lengthStr1+1];
for(int i=0;i!=(lengthStr1+1);++i)
compareArr[i]=new int[lengthStr2+1];
directionArr=new int*[lengthStr1+1];
for(int i=0;i!=(lengthStr1+1);++i)
directionArr[i]=new int[lengthStr2+1];
DynamicPlanLCS(str1,str2,compareArr,directionArr);
PrintLCS(directionArr,str1 ,lengthStr1,lengthStr2);
_getch();
return 0;
}
/** 字符串操作实现
*/
// 字符串比较实现strcmp,相等返回0,前者小于后者返回负数,前者大于后者返回整数
int StrCmp(const char*str1,const char*str2)
{
if(str1==null || str2==null)
exit();
while(*str1==*str2){
if(0==*str1)
return 0;
++str1;
++str2;
}
return (*str1-*str2);
}
// 复制字符串
char* StrCpy(const char *source,char *des)
{
assert(source!=NULL&&des!=NULL);
char *ret;
while(((*des++)=(*source++))!='\0');
return ret;
}
//拼接字符串
char *Mystrcat(char *pDst,const char *pSrc)
//
拼接字符串
{
assert((pDst!=NULL)&&(pSrc!=NULL));
char *ptemp =pDst;
for(;*pDst; ++pDst);
while((*pDst++ = *pSrc++)!= '\0');
return ptemp;
}
char *StrCat(char *pDst,const char *pSrc){
assert((pDst!=NULL)&&(pSrc!=NULL));
char *ptemp =pDst;
for(;*pDst; ++pDst);
while((*pDst++ = *pSrc++)!= '\0');
return ptemp;
}
16进制转化为10进制数字
int ahextoi(const char* str)
{
int n = 0;
char* p=const_cast<char*>(str);
char* q = p;
/* reach its tail */
while(*q)
q++;
if(*p == '0' && *(p + 1) != 0)
/* skip "0x" or "0X" */
p += 2;
while(*p)
{
int c;
if(*p >= '0' && *p <= '9')
c = *p - '0';
else if(*p >= 'A' && *p <= 'F')
c = *p - 'A' + 0xA;
else if(*p >= 'a' && *p <= 'f')
c = *p - 'a' + 0xA;
else
/* invalid char */
return 0;
//n += c << ((int)(q - p - 1) * 4);
int factor=1;
for(int i=0;i!=q-p-1;++i)
factor*=16;
n=n+c*factor;
p++;
}
return n;
}