$ ls
main.cpp
$ cat main.cpp
#include <iostream>
#include <string>
using std::string;
using std::cout, std::endl;
// [start, end)
const string substr(const string &s, size_t start, size_t end) {
if(start > s.size()) start = s.size();
if(end > s.size()) end = s.size();
if(end <= start) return "";
#if 0
return s.substr(start, end - start);
#else
string result;
for(size_t i = start; i < end; ++i) {
result += s[i];
}
return result;
#endif
}
int find(int *data, size_t size, int x) {
for(size_t i = 0; i < size; ++i) {
if(data[i] == x) return i;
}
return -1;
}
int main() {
cout << substr("hello world!", 3, 8) << endl;
cout << substr("hello world!", 3, 99) << endl;
cout << substr("hello world!", 99, 3) << endl;
cout << substr("hello world!", 99, 99) << endl;
cout << substr("hello world!", 8, 3) << endl;
cout << substr("hello world!", 0, 11) << endl;
cout << substr("hello world!", 0, 12) << endl;
cout << substr("hello world!", 1, 11) << endl;
cout << substr("hello world!", 1, 12) << endl;
int data[] = {1, 9, 2, 3, 8, 6, 7, 5, 4};
const size_t data_size = sizeof(data) / sizeof(data[0]);
cout << find(data, data_size, 0) << endl;
cout << find(data, data_size, -1) << endl;
cout << find(data, data_size, -99) << endl;
cout << find(data, data_size, 99) << endl;
cout << find(data, data_size, 10) << endl;
cout << find(data, data_size, 9) << endl;
cout << find(data, data_size, 1) << endl;
cout << find(data, data_size, 5) << endl;
cout << find(data, data_size, 7) << endl;
return 0;
}
$ g++-debug -o main main.cpp
$ ./main
lo wo
lo world!
hello world
hello world!
ello world
ello world!
-1
-1
-1
-1
-1
1
0
7
6
$