#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
using namespace std;
#define m 10//预编译不在意你什么类型,我只是简单的文本替换
bool test_func1(char* p)
{
if (p == NULL)
{
cout << "指针为空" << endl;
return false;
}
cout << p << endl;
return true;
}
int main()
{
/********************************************************************************************
* const 用法小展示&&数组给数组赋值
* *******************************************************************************************
* 宏定义只是简单的文本替换,不代表什么类型
* 数组给数组赋值不能直接赋值
* 两个char类型的数组相互复制要用头文件是<string>的memcpy(des,src,size)函数
* memcpy和strcpy都可以用数组给数组赋值,但是memcpy可以指定大小
********************************************************************************************/
const int n = 10;
char a[n] = "gdfgsh";
char b[m] = "0";
memcpy(b, a, 7);//把a数组的值赋值给b数组,就是简单的替换
cout << b << endl;//gdfgs
cout << a[3] << endl;//g
strcpy(b, a);
cout << b << endl ;//gdfgsh
cout<< endl << endl << endl << endl << endl << endl;
/********************************************************************************************
*字符串大小比较
* ********************************************************************************************
* (1)
* int compare (const basic_string& s) const;//两个字符串值相等返回0
*int compare (const Ch* p) const;
*int compare (size_type pos, size_type n, const basic_string& s) const;
*int compare (size_type pos, size_type n, const basic_string& s,size_type pos2, size_type n2) const;
*int compare (size_type pos, size_type n, const Ch* p, size_type = npos) const;
* (2)
* String 类的常见运算符包括 >、<、==、>=、<=、!=也可以比较大小
********************************************************************************************/
string str1 = "abcd";
string str2 = "abcd";
const char* pstr= "abc";
if (str1 == str2)
cout << "=" << endl;
else
cout << "!=" << endl;
if (!str1.compare(str2))//两个字符串值相等返回0
cout << "=" << endl;
else
cout << "!=" << endl;
if (!str1.compare(1,2,str2,2,3))
cout << "=" << endl;
else
cout << "!=" << endl;
if (!str1.compare(pstr))
cout << "=" << endl;
else
cout << "!=" << endl;
if (!str1.compare(0,3,pstr))
cout << "=" << endl;
else
cout << "!=" << endl;
if (!str1.compare(0, 2, pstr))
cout << "=" << endl;
else
cout << "!=" << endl;
cout << endl << endl << endl << endl << endl << endl;
/********************************************************************************************
*指针易错点
* ********************************************************************************************
*指针用new初始化时必须指定数组的大小
* const char *p2; p2 = "test";//赋值给p2的是字符串“test”首元素的首地址,并不是一个直接赋值操作
* p2= 't';//err//赋值的“字符串”不是字符串,而是字符串的首元素首地址,而字符就是字符了
* 数组不能直接给数组赋值,可以参考使用memcpy和strcpy
********************************************************************************************/
const char* p = new char[100];//必须要在初始化时指定数组大小
p = "fdgsdfgfsdg";
const char *p1= "test";//常量指针
cout << p1 << endl;//test
const char *p2= "test";//赋值给p2的是字符串“test”第一个元素的地址,并不是一个直接赋值操作
// p2= 't';//err//赋值的“字符串”不是字符串,而是字符串的首元素首地址,而字符就是字符了
cout << p2 << endl;//test
cout << endl << endl << endl << endl << endl << endl;
/********************************************************************************************
*指针的比较
* ********************************************************************************************
*
********************************************************************************************/
if (p1 == p2)//=//都指向常量区,地址是一样的
cout << "=" << endl;
else
cout << "!=" << endl;
cout << &p1 << endl;//p1的地址
printf("\n&p1=%p", &p1);
cout << &p2 << endl;//p2的地址
printf("\np1=%p", p1);
printf("\np2=%p", p2);
printf("\np1+1=%p", p1+1);//比p1多一//+sizeof(char)
printf("\np2+1=%p", p2+1);
printf("\n&p1+1=%p", &p1+1);//比&p1多4//+sizeof(char *)
printf("\n&p2+1=%p", &p2+1);
cout << endl << endl << endl << endl << endl << endl;
int x = 0x87654321;
printf("%p\n", x);//指针的值
printf("%p\n", (void*)x);//指针的值
printf("%p\n", &x);//指针的地址
const char pa[] = "test";
const char pb[] = "test";
if (pa == pb)//在对数组名称使用 == 运算符时,运算符会比较数组的开始内存地址,而不是数组的内容。这个代码中的两个数组显然会有不同的内存地址
cout << "=" << endl;
else
cout << "!=" << endl;
printf("\n&pa=%p", &pa);
printf("\n&pb=%p", &pb);
printf("\npa=%p", pa);
printf("\npb=%p", pb);
printf("\n&pa[0]=%p", &pa[0]);
printf("\n&pa[1]=%p", &pa[1]);
printf("\npa[3]=%p", pa[3]);
printf("\npb[0]=%p", pb[0]);
printf("\npb[3]=%p", pb[3]);
printf("\npa=%p", pa);
printf("\npa+1=%p", pa + 1);//+sizeof(char)
printf("\n&pa+1=%p", &pa + 1);//+sizeof(pa)//都是跳所指对象类型的大小
/********************************************************************************************
*指针和数组的内存分配
* ********************************************************************************************
********************************************************************************************/
char cstr1[10] = "abc";//在声明中=不是赋值运算符
cout << cstr1 << endl;//
char cstr2[9]= "1516";//数组在栈上分配内存
//ctsr2 = ctsr1;//数组不能直接给数组赋值
cout << cstr2 << endl;//
if (cstr1== cstr2)//!=
cout << "=" << endl;
else
cout << "!=" << endl;
if (!strcmp(cstr1, cstr2))//!=//两个字符串相等strcmp返回0
cout << "=" << endl;
else
cout << "!=" << endl;
cout << endl << endl << endl << endl << endl << endl;
/********************************************************************************************
*cstr1/cstr1[0]/&cstr1[0]/&cstr1/cout
* ********************************************************************************************
*cstr1是数组首元素的首地址
*&cstr1是数组的首地址
* &cstr1和cstr1的值是一样的,但意义不一样,而且cout后也不一样
* cout根据<<后面的“参数的意义”确定输出的形式******
********************************************************************************************/
cout << cstr1[0] << endl;//a
cout << cstr2[0] << endl;//a
cout << cstr1 << endl;//abc//数组首元素的首地址
cout << cstr2 << endl;//abc
cout << &cstr1[0] << endl;//abc//数组首元素的首地址
cout << &cstr2[0] << endl;//abc
cout << &cstr1 << endl;//012FFAA4//数组首地址值
cout << &cstr2 << endl;//012FFA94//
/********************************************************************************************
*printf输出形式
* ********************************************************************************************
*int printf(const char *format,[argument]);
* printf函数根据前面“格式”,确定后面参数的输出形式
********************************************************************************************/
printf("\n%d", cstr1);//2029580//输出的是十进制地址
printf("\n%s", cstr1);//abc//输出的是字符串
printf("\n%d", cstr2);//2029560
printf("\n%d", &cstr2);//2029560
cout << endl << endl << endl << endl << endl << endl;
/********************************************************************************************
*类成员的const修饰的是this指针
* ********************************************************************************************
* ********************************************************************************************/
class Test
{
public:
Test(int a, int b)
{
this->a = a;
this->b = b;
}
int add( int c, int d)const
{
c = 10;//可以修改,因为const修饰的是this指针(当前对象),不能修改的是对象的数据成员
//a = 111;//由于正在通过常量对象访问“a”,因此无法对其进行修改
return c + d;
}
protected:
private:
int a;
int b;
};
/********************************************************************************************
* ascii&&printf&&cout
* ********************************************************************************************
* printf想输出%要前面加%
********************************************************************************************/
printf("\n%%c%%d%%x");
/**************************************/
char da48 = 48;//这个9是数字
printf("\nda48=%c", da48);
printf("\nda48=%d", da48);
printf("\nda48=%x", da48);
/**************************************/
char da9 = 9;//这个9是数字
printf("\nda9=%c", da9);
printf("\nda9=%d", da9);
printf("\nda9=%x", da9);
/**************************************/
char ca9 = '9';//这个9是字符
printf("\nca9=%c", ca9);
printf("\nca9=%d", ca9);
printf("\nca9=%x", ca9);
/**************************************/
char ha21 = 0x21;//这个9是数字
printf("\nha21=%c", ha21);
printf("\nha21=%d", ha21);
printf("\nha21=%x", ha21);
/**************************************/
int na = 33;
//char a=33;//!//err:不是int的不能进制转换
cout << dec << na << endl;
cout << oct << na << endl;
cout << hex << na << endl;
/**************************************/
//汉字占两个字节,只能以字符串的形式赋值打印
char a[10] = "我是蜗牛";//只有指针才能赋值汉字,char类型不行;//char a[8] = "我是蜗牛";会地址越界
char b[10] = "他是蜗牛";//只有指针才能赋值汉字,char类型不行;
if (a[0] == b[0])
cout << "=" << endl;
if (a[1] == b[1])
cout << "=" << endl;
else
cout << "!=" << endl;
if (a[3] == b[3])
cout << "=" << endl;
else
cout << "!=" << endl;
cout << a << endl; //cout << a[0] << endl;输出也要是指针类型,char类型也不行
//{#define _CRT_SECURE_NO_WARNINGS
//#include<iostream>
//#include<stdio.h>
//#include <vector>
//#include <string>
//#include <sstream>
//using namespace std;
//int main()
//{ //汉字占两个字节,只能以字符串的形式赋值打印
// /**************************************/
// char aa[3] = { 0xB0,0xA1 };//用两个16制字节可以给2
// cout << aa << endl;//啊
// char* a = (char*)0xf1;
// *a = 0xB0;
// char* b = (char*)0xf2;
// *b = 0xB0;
//
// return 0;
//}
}
/********************************************************************************************
*数组指针
* ********************************************************************************************
*
********************************************************************************************/
/********************************************************************************************
*指针数组
* ********************************************************************************************
*
********************************************************************************************/
/********************************************************************************************
*动态申请
* ********************************************************************************************
*
********************************************************************************************/
/********************************************************************************************
*静态编译
* ********************************************************************************************
*
********************************************************************************************/
/********************************************************************************************
*进制转换sscanf/strol/itoa
* ********************************************************************************************
*
********************************************************************************************/
/********************************************************************************************
*补码
* ********************************************************************************************
********************************************************************************************/
return 0;
}