Question

In vector c++, I have strings, for example : ACTT CTTG TTGA TG TGA GAG, as you can see, they superpose

ACTT
 CTTG
  TTGA
   TG
    GAG

So in the alignment we can see

ACTTGAG

I want to concatenate them as you can see above and put to another vector. I've tried use substring function, but it doesn't work...

Was it helpful?

Solution 2

Assuming you still use the same code as the last question you might want to consider using the first index you have in your element (it[0]). You could add this result to a string and print it.

Use:

std::string space = "";
std::string result = "";
auto end = vec.rend();

for(auto it = vec.rbegin(); it != vec.rend(); ++it ) {
    if (it == end - 1) {
        result += *it;
    }
    else {
        result += it[0];
    }        

    std::cout << space << *it << std::endl;
    space += " ";
}
std::cout << result << std::endl;

OTHER TIPS

Here's a fairly simple algorithm to overlap two strings:

#include <string>

std::string overlap(std::string first, std::string const & second)
{
    std::size_t pos = 0;
    for (std::size_t i = 1; i < first.size(); ++i)
    {
        if (first.compare(first.size() - i, i, second, 0, i) == 0)
        {
            pos = i;
        }
    }
    first.append(second, pos, second.npos);
    return first;
}

Usage:

std::string result = overlap("abcde", "defgh");

And to overlap a whole range, use std::accumulate:

#include <algorithm>
#include <iostream>
#include <vector>

int main()
{
  std::vector<std::string> strings = {"abc", "def", "fegh", "ghq"};
  std::cout << std::accumulate(strings.begin(), strings.end(), std::string(), overlap) << std::endl;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top