观看

#include<iostream>
#include<sstream>
using namespace std;
int main()
{
void compare(string *a);
string a[3];
cout<<"请输入需比较的三个字符串:";
for(int i=0;i<3;++i)
cin>>a[i];
compare(a);
cout<<"从小到大排序为:";
for(i=0;i<3;++i)
cout<<a[i]<<" ";
cout<<endl;
return 1;
} //======================================
void compare(string *a)
{
string *b;
string temp;
b=a;
for(int i=0;i<3;++i)
for(int j=0;j<2-i;++j)
{
if(*(b+j)>*(b+j+1))
{
temp=*(b+j);
*(b+j)=*(b+j+1);
*(b+j+1)=temp;
}
}
}

//------------------------------------------

#include<iostream>
using namespace std;
int main()
{
int n,m;
cout<<"请输入整数的个数:";
cin>>n;
int *p=new int[n];
cout<<"请输入这n个数:";
for(int i=0;i<n;++i)
cin>>*(p+i);
cout<<"请输入需要调整的位数:";
cin>>m;
cout<<"调整后为:";
for(i=0;i<n;++i)
cout<<*(p+(i+n-m)%n)<<" ";//漂亮,笔记
cout<<endl;
return 1;

}

//---------------------------------------

关于C++的new int()与new int[]

编写一个List类:
class List
{
int length; //列表长度
int* lpInt; //列表指针
List(int size);
~List();
}

List::List(int size)
{
length = size;
lpInt = new int(length); //关键点
int n;
for(n=0;n<length;n++)
{
lpInt[n] = n;
}
}

List::~List()
{
delete []lpInt; //出错,访问越界
}

在调试时,运行报错:
Debug Error!
Frogram: C:\Test\Debug\Test.exe
DAMAGE: after Normal block(#xxxx) at 0x00430040 (Press Retry to debug the application)

出错的原因:
lpInt = new int(length);
应该修改为:
lpInt = new int[length];

说明:
int* lpInt = new int(10)是分配一个int,也就是*lpInt = 10。   //是值
int* lpInt = new int[10]是分配10个int数组,lpInt是数组的首地址。//是数组

//----------------------------------

new int[n]得到指向一个动态数组的指针,且只能用默认构造函数。
new int(n)则得到一个指向一个int值的指针,且用n来初始化这个int值

//----------------------

posted @ 2013-05-14 18:35  herizai  阅读(158)  评论(0编辑  收藏  举报