I have some legacy C which iterates through buffer_1
allocated with some valid memory. buffer_2
is made to point to parts of buffer_1
and the iteration keeps moving random 20 places ahead. Following is the code:
char* buffer_1;
char* buffer_2;
size_t buffer_length;
buffer_length = 2000; // somerandom value
if (buffer_length > 0) {
buffer_1 = malloc(buffer_length);
for(buffer_2 = buffer_1; buffer_2 < buffer_1 + buffer_length; buffer_2 += 20) {
// Some random logic
}
}
I want to make the above code C++ 11 compliant by declaring buffer_1
and buffer_2
as std::vector<char>
s like below.
std::vector<char> buffer_1;
std::vector<char> buffer_2;
Then I think I would need to use an iterator over buffer_1
do something like below?
if (buffer_length > 0) {
buffer_1.reserve(buffer_length);
for(std::vector<char>::iterator it = buffer_1.begin(); it != buffer_1.end(); it += 20) {
buffer_2.data() = *it;
}
}
is the above way of iterating through a std::vector<char>
correct? Does it do exactly what the following for statement does?
for(buffer_2 = buffer_1; buffer_2 < buffer_1 + buffer_length; buffer_2 += 20)
Aucun commentaire:
Enregistrer un commentaire