-
Notifications
You must be signed in to change notification settings - Fork 0
/
vecToString.cpp
136 lines (127 loc) · 3.63 KB
/
vecToString.cpp
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
#include <vector>
#include <iterator>
using namespace std;
template<typename InIt, typename OutIt>
bool MakeList1(
InIt begin,
InIt end,
int max_size,
OutIt output,
char delimeter = ','
) {
using namespace std;
static const auto kDelimeterSize = 1;
if (begin == end) return false;
vector<string> result(1);
auto current = result.rbegin();
int delimeter_factor = 0;
for(auto i = begin; i != end; ++i) {
auto item_size = (*i).size();
if (item_size > max_size) {
return false;
}
if (((*current).size() + item_size + delimeter_factor*kDelimeterSize > max_size)) {
result.emplace_back("");
current = result.rbegin();
delimeter_factor = 0;
}
if (delimeter_factor) {
*current += delimeter;
}
*current += (*i);
delimeter_factor = 1;
}
std::copy(result.begin(), result.end(), output);
return true;
}
template<typename InIt, typename OutIt>
bool MakeList2(
InIt begin,
InIt end,
int max_size,
OutIt output,
char delimeter = ','
) {
using namespace std;
static const auto kDelimeterSize = 1;
if (begin == end) return false;
bool new_bucket = true;
vector<string> result(1);
auto current = result.rbegin();
for(auto i = begin; i != end; ++i) {
auto item_size = (*i).size();
if (item_size > max_size) {
return false;
}
if (!new_bucket && ((*current).size() + item_size + kDelimeterSize > max_size)) {
result.emplace_back("");
current = result.rbegin();
new_bucket = true;
}
if (!new_bucket) {
*current += delimeter;
}
*current += (*i);
new_bucket = false;
}
std::copy(result.begin(), result.end(), output);
return true;
}
/* Bug! No empty lines in output
template<typename InIt, typename OutIt>
bool MakeList3(
InIt begin,
InIt end,
int max_size,
OutIt output,
char delimeter = ','
) {
using namespace std;
static const auto kDelimeterSize = 1;
if (begin == end) return false;
vector<string> result(1);
auto current = &result.back();
for(auto i = begin; i != end; ++i) {
auto item_size = (*i).size();
if (item_size > max_size) {
return false;
}
if ((*current).size() && ((*current).size() + item_size + kDelimeterSize > max_size)) {
result.emplace_back("");
current = &result.back();
}
if (!(*current).empty()) {
*current += delimeter;
}
*current += (*i);
}
std::copy(result.begin(), result.end(), output);
return true;
}*/
int main() {
// your code goes here
std::vector<std::string> vec1 = {"foo", "bar", "tar"};
std::vector<std::string> vec2 = {"foo", "bar", "tar", "foobar", "carzar"};
std::vector<std::string> vec = {"aaa", "bbb","b", "bb4", "c2", "c", "d", "e"};
std::vector<std::string> l, l2, l3, l4;
const int max_size = 3;
std::cout << "\n=======================================================\n";
std::vector<std::string> l11, l22, l33, l44;
bool res = MakeList4(vec.begin(), vec.end(), max_size, std::back_inserter(l11));
std::cout << "result: " << std::string(res?"ok":"fail") << "\n";
for(auto i : l11) {
std::cout << i <<" : ";
}
res = MakeList4(vec1.begin(), vec1.end(), max_size, std::back_inserter(l22));
std::cout << "\nresult: " << std::string(res?"ok":"fail") << "\n";
for(auto i : l22) {
std::cout << i <<" : ";
}
res = MakeList4(vec2.begin(), vec2.end(), 7, std::back_inserter(l33));
std::cout << "\nresult: " << std::string(res?"ok":"fail") << "\n";
for(auto i : l33) {
std::cout << i <<" : ";
}
return 0;
}