一些C++小记
一 排序模板
1. 直接插入排序
1 | template<class T> |
2. 直接选择排序
1 | template<class T> |
3. 气泡排序
1 | void swap(T *a,T *b) { |
二 查找模板
1. 顺序查找
1 | template<class T> |
2. 折半查找
1 | template<class T> |
1 | template<class T> |
第六章
- 6-21读取字符串统计英文字母个数
1 |
|
6-22 字符串递归逆置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using namespace std;
void reverse(string &s,int n) {
static int a = 0; //重要
if (n / 2 <= a ) {
return;
}
else {
char temp = s[a];
s[a] = s[n - a -1];
s[n -a -1] = temp;
a++;
reverse(s,n);
}
}
int main() {
string a = "12345";
reverse(a, a.length());
cout << a;
}6-26 矩阵转置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using namespace std;
int row;
int arrange;
void output(int *p,int row, int arrange) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < arrange; j++) {
cout << setw(4)<< p[i*arrange + j];
}
cout << endl;
}
}
void transfom(int *p) {
for (int i = 0; i < row; i++) {
for (int j = i; j < arrange; j++) {
int temp = p[i*row + j];
p[i*row + j] = p[j*arrange + i];
p[j*arrange + i] = temp;
}
cout << endl;
}
}
int main() {
cout << "输入行和列" << endl;
cin >> row >> arrange;
int *matrxi = new int[row * arrange];
for (int i = 0; i < row; i++) {
for (int j = 0; j < arrange; j++) {
matrxi[i*arrange + j] = rand()%20;
}
}
output(matrxi,row,arrange);
transfom(matrxi);
output(matrxi, arrange, row);
}基于char*设计一个字符串类
Mystring
,并且具有构造函数,析构函数,赋值构造函数,重载运算符等
1 |
|
第八章
- 时钟类重载
1 | /* |
重载complex类
<<
<<左操作数为
ostream
类型的引用,ostream
是cout
的基类执行cout<<c1
——->operator<<(cout,c1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using namespace std;
class Complex
{
public:
Complex() { real = 0; imag = 0; }
Complex(double r, double i) :real(r), imag(i) {}
void display();
Complex operator + (Complex &c);
Complex operator + (int &i);
friend Complex operator + (int &, Complex &); // i + c
friend ostream & operator << (ostream &out, const Complex &c);
private:
double real; // 复数实部
double imag; // 复数虚部
};
Complex Complex::operator + (Complex &c)
{
cout << "Complex operator + (Complex &c)" << endl;
return Complex(real + c.real, imag + c.imag);
}
Complex Complex::operator + (int &i)
{
cout << "Complex operator + (int &i)" << endl;
return Complex(real + i, imag);
}
Complex operator + (int &i, Complex &c)
{
cout << "Complex operator + (int &i, Complex &c)" << endl;
return Complex(i + c.real, c.imag);
}
void Complex::display()
{
cout << "(" << real << ", " << imag << ")" << endl;
}
ostream & operator <<(ostream &out, const Complex &c) {
out << c.real << "+" << c.imag << endl;
return out;
}
int main()
{
Complex c1(5, -2), c2(-4, 3), c3;
int i = 6;
c3 = c1 + i;
cout << "c1 + i = ";
c3.display();
c3 = i + c1;
cout << "i + c1 = ";
c3.display();
c3 = c1 + c2;
cout << " c1 + c2 = ";
cout << c3;
system("pause");
return 0;
}例题8-4 重写
<<
和+
前++
和后++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using namespace std;
class Counter {
private:
int count;
public:
Counter() :count(0) { }
Counter(int num):count(num) { }
friend ostream& operator <<(ostream &out, const Counter &c);
Counter operator +(int num);
Counter operator ++();//前置++
Counter operator ++(int); //后置++
};
Counter Counter::operator+(int num){
count += num;
return *this;
}
ostream& operator <<(ostream &out, const Counter &c) {
out << "Count: " << c.count << endl;
return out;
}
Counter Counter::operator++() {
count++;
return *this;
}
Counter Counter::operator++(int){
Counter old = *this;
++(*this);
return old;
}
int main() {
Counter c1(10);
c1 = c1 + 5;
cout << c1 << endl;
cout << c1++ << endl;
cout << ++c1 << endl;
}
2010
1. 输入n个十进制数转换成二进制写到文件
1 |
|
2. 写两个模板函数:插入排序算法的迭代和递归实现
- 迭代
1 |
|
3. 字符串解析
- 文件内有
(010)(150123456789)|123|(430070)
- 按照以下格式输出”区号|电话号码|城市编号|邮编”
1 |
|