赞
踩
本文旨在对C++学习过程中不熟悉和c语言有区别之处记录,希望能帮助到需要之人。
#include<iostream>
using namespace std;
float a=2;//全局变量
void main()
{
int a=1;//局部变量
cout<<a<<endl;
cout<<::a;
}
运行输出:
1
2
使用时注意以下两点:
#include<iostream> using namespace std; class RMB { public: RMB(double value = 0.0); operator double() { return yuan + jf / 100.0; } void display() { cout << (yuan + jf / 100.0) << endl; } protected: unsigned int yuan; unsigned int jf; }; RMB::RMB(double value) { yuan = value; jf = (value - yuan) * 100 + 0.5; } void main() { RMB d1(2.0), d2(1.5), d3; d3 = RMB((double)d1 + (double)d2); d3.display(); }
#include<iostream> #include<string> using namespace std; class MyStr { private: char *name; int id; public: MyStr() {} MyStr(int _id, char *_name) //constructor { cout << "constructor" << endl; id = _id; name = new char[strlen(_name) + 1]; strcpy_s(name, strlen(_name) + 1, _name); } MyStr(const MyStr& str) { cout << "copy constructor" << endl; id = str.id; if (name != NULL) delete[] name; name = new char[strlen(str.name) + 1]; strcpy_s(name, strlen(str.name) + 1, str.name); } MyStr& operator =(const MyStr& str)//赋值运算符 { cout << "operator =" << endl; if (this != &str) { if (name != NULL) delete[] name; this->id = str.id; int len = strlen(str.name); name = new char[len + 1]; strcpy_s(name, strlen(str.name) + 1, str.name); } return *this; } ~MyStr() { delete[] name; } }; int main() { MyStr str1(1, "hhxx"); cout << "====================" << endl; MyStr str2; str2 = str1; cout << "====================" << endl; MyStr str3 = str2; return 0; }
#include<iostream> using namespace std; void quickSort(int a[],int); int main() { int n; int array[20],k; cout<<"请输入需排序的数组的个数:\n"; cin>>n; cout<<"请输入数组元素:\n";//<<endl; for(k=0;k<n;k++) cin>>array[k]; quickSort(array,n-1); cout<<"输出排序结果:"<<endl; for(k=0;k<n;k++) cout<<array[k]<<","; system("pause"); return 0; } void quickSort(int s[], int r) { int l=0; if (l< r) { int i = l, j = r, x = s[l]; while (i < j) { while(i < j && s[j]>= x) // 从右向左找第一个小于x的数 j--; if(i < j) s[i++] = s[j]; while(i < j && s[i]< x) // 从左向右找第一个大于等于x的数 i++; if(i < j) s[j--] = s[i]; } s[i] = x; quickSort(s, i - 1); // 递归调用 quickSort(s+i + 1, r-i-1); } }
(2)三个参数:
#include <iostream> using namespace std; template<class T> void quick_sort(T *a, int left,int right) //left和right为索引值 { int temp; //存储每次选定的基准数(从最左侧选基准数) int t; int initial=0; //***必须有这一部分 if (left<right) //因为在递归过程中有减1加1的情况,当其越界时,直接return,不返回任何值,即结束当前程序块 { int end=right; temp=a[left]; while(left<right) //此时左右index在移动中,若left==right,则跳出循环,将基数归位 { while(a[right]>=temp && left<right) //直到找到小于基准数的值为准 right--; while(a[left]<=temp && left<right) left++; //进入此部分,表明a[left]>temp而且a[right]<temp,可以进行交换 if(left<right) //交换左右两侧值,当left=right时,跳出外层while循环 { t=a[right]; a[right]=a[left]; a[left]=t; } } a[initial]=a[left]; a[left]=temp; //基数归位 //递归处理归位后的基准数的左右两侧 quick_sort(a,initial,left-1); //此时left=right quick_sort(a,left+1,end); } } int main() { int a[20]; int n; cout<<"请输入需排序的数组的个数:\n"; cin>>n; cout<<"请输入数组元素:\n"; for (int i=0;i<n;i++) cin>>a[i]; quick_sort(a,0,n-1); cout<<"排序结果:\n"; for (int j=0;j<n;j++) cout<<a[j]<<" "; cout<<endl; system("pause"); return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。