0

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.

maxbachmann
  • 2,862
  • 1
  • 11
  • 35
  • Please organize your question so it is focused on actual problem. Begging of question looks detached from code provided. Provide [mcve] using https://godbolt.org/ or other online compiler. For me question has to much noise. – Marek R Feb 25 '20 at 14:17
  • https://wandbox.org/nojs/gcc-head/permlink/OhiPkb1ziIK9Y4e1 – maxbachmann Feb 25 '20 at 14:41
  • 2
    @maxbachmann can you please tell us what's your desired output? what do you want to achieve? – NutCracker Feb 25 '20 at 14:46
  • I would like to get the common suffix -> in this case 6 aswell so I can then remove both the common suffix and prefix from the two ranges – maxbachmann Feb 25 '20 at 15:15
  • Is `split(' ') | join(' ')` not a no-op? I don't understand what this question is asking. – Barry Feb 25 '20 at 16:02
  • Essentially I was hoping I could implement https://wandbox.org/nojs/gcc-head/permlink/txH8B04KO59MU7nc without having to use the nested loops to remove a common prefix and suffix of the two strings with words in sorted order – maxbachmann Feb 25 '20 at 17:05
  • Instead of a comment with a link, please edit the question to show some examples of desired input and output. – cigien Aug 12 '20 at 00:58

0 Answers0