-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy patharray.cpp
More file actions
46 lines (42 loc) · 1.46 KB
/
Copy patharray.cpp
File metadata and controls
46 lines (42 loc) · 1.46 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
#include <array>
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
using std::string;
long get_a_target_long() {
long target = 0;
std::cout << "target(0~" << RAND_MAX << "): ";
std::cin >> target;
return target;
}
int compareLongs(const void *a, const void* b) {
return (*(long*)a - *(long*)b);
}
namespace test_array {
const int ASIZE = 1000;
void test_array() {
std::cout << "\ntest_array()...... \n";
std::array<long, ASIZE> c;
clock_t timeStart = clock();
for(long i = 0; i < ASIZE; i++) {
c[i] = rand();
}
std::cout << "milli-seconds: " << (clock()-timeStart) << std::endl;
std::cout << "array.size(): " << c.size() << std::endl;
std::cout << "array.front(): " << c.front() << std::endl;
std::cout << "array.back(): " << c.back() << std::endl;
// 数组在内存的起点地址
std::cout << "array.data(): " << c.data() << std::endl;
long target = get_a_target_long();
timeStart = clock();
qsort(c.data(), ASIZE, sizeof(long), compareLongs);
long* pItem = (long*)bsearch(&target, (c.data()), ASIZE, sizeof(long), compareLongs);
std::cout << "qsort()+bsearch(), milli-seconds: " << (clock()-timeStart) << std::endl;
if(pItem!=NULL) {
std::cout << "found, " << *pItem << std::endl;
} else {
std::cout << "not found!" << std::endl;
}
}
}