代码的品味:字符串join实现

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
#include <iostream>
#include <sstream>
#include <vector>

template <class Container>
std::string join(const Container& data, const std::string& sep = " ") {
std::ostringstream os;
std::string tmp;
for (auto it = data.begin(); it != data.end(); ++it) {
os << tmp << *it;
tmp = sep;
}
return os.str();
}

template <class Container>
std::string join2(const Container& data, const std::string& sep = " ") {
std::ostringstream os;
for (auto it = data.begin(); it != data.end(); ++it) {
os << ((it == data.begin()) ? "" : sep) << *it;
}
return os.str();
}

template <class Container>
std::string join3(const Container& data, const std::string& sep = " ") {
std::ostringstream os;
bool first = true;
for (auto it = data.begin(); it != data.end(); ++it) {
if (!first) {
os << sep;
}
first = false;
os << *it;
}
return os.str();
}

void print(const int *arr, int n) {
for (int i = 0; i < n; i++) std::cout << arr[i] << ",\n"[i == n - 1];
}

int main() {
const std::vector<int> data{1, 2, 4, 1, 9};
std::cout << join(data, ",") << std::endl;
std::cout << join2(data, ",") << std::endl;
std::cout << join3(data, ",") << std::endl;
print(data.data(), data.size());
}