-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
88 lines (72 loc) · 1.75 KB
/
main.cpp
File metadata and controls
88 lines (72 loc) · 1.75 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <iterator>
using namespace std;
int strlen(string);
string reverse(string);
void printTrigon(char ,int );
template<class T>
void sort(T a[] ,int len);
int main() {
string str = "abcd";
//第4题
// printTrigon('*',5);
//第5题
// cout << strlen(str) << endl;
// cout << reverse(str);
//第6题
int a[5] ={3,6,11,9,23};
sort(a ,5);
copy(a,a+5,ostream_iterator<int>(cout," "));
return 0;
}
//输出三角形 第4题
void printTrigon(char ch,int num){
for (int i = 0; i <= num; ++i) {
for(int l =0;l<=i;l++){
cout<<ch;
}
cout<<endl;
}
}
//以下是编程题的地5小题
int strlen(string str) {
int len = 0;
// basic_string<char, std::char_traits<char>, std::allocator<char>>::iterator it ;
/* string::iterator it;
for (it = str.begin(); *it != '\0'; it++) {
cout << *it << endl;
len++;
}*/
//*str.end() = '\0'
while (*(str.begin() + len) != *str.end()) {
len++;
}
return len;
}
string reverse(string str) {
char c;
int l = strlen(str) / 2;
for (int i = 0; i < l; i++) {
c = *(str.begin() + i);
*(str.begin() + i) = *(str.end() - i - 1);
*(str.end() - i - 1) = c;
}
return str;
}
//第6题
template<class T>
void sort(T a[] ,int len){
T temp;
//int a1[5] ={3,6,11,9,23};
//int len = (sizeof(a)/ sizeof(a[1])); 使用函数模板后,sizeof不能确定T的具体类型,无法计算数组长度。
for(int i=0;i< len;i++){
temp = a[i];
for(int j = i + 1; j < len; j++) {
if(temp > a[j]) {
a[i] = a[j];
a[j] = temp;
temp = a[i];
}
}
}
}