I am splitting two strings into std::vector's by whitespace, remove duplicates and sort them. Afterwards I would like to join each vector to one continous range with a whitespace as delimiter. Using ranges v3 this works by using views::join(' ')
.
As a preprocessing I would like to remove the common prefix and suffix of the two ranges.
Right now I detect the prefix_len using (simplified version without sorting and deduping):
#include <vector>
#include <range/v3/all.hpp>
#include <string_view>
int main() {
std::string_view a = "hello world";
std::string_view b = "hello beautiful world";
auto a_words = a
| ranges::views::split(' ')
| ranges::to<std::vector>;
auto a_sentence = a_words | ranges::views::join(' ');
auto b_words = b
| ranges::views::split(' ')
| ranges::to<std::vector>;
// missing sortings and removing duplicates in between
auto b_sentence = b_words | ranges::views::join(' ');
auto a_iter = a_sentence.begin();
auto b_iter = b_sentence.begin();
size_t prefix_len = 0;
while (*a_iter == *b_iter) {
ranges::advance(a_iter, 1);
ranges::advance(b_iter, 1);
++prefix_len;
}
size_t suffix_len = 0;
// not quite sure how to iterate backwards to get the suffix length
}
For the suffix my thought was to reverse the range using ranges::views::reverse
. However this appears to be not possible.
So my question is how I can find the suffix when joining a vector like I did in the example.