2

In order to split a string into a vector I use

std::vector<std::string> v;
boost::split(v, input, boost::is_any_of("|"));

is there a function either in Boost or STL that performs the reverse of this operation, i.e. a join function, of the form

join(v, output, "|")
P45 Imminent
  • 8,319
  • 4
  • 35
  • 78
  • Please take some time to refresh [the help pages](http://stackoverflow.com/help), especially the section named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) – Some programmer dude Aug 28 '19 at 08:18
  • 1
    [boost::join](https://www.boost.org/doc/libs/1_71_0/doc/html/boost/algorithm/join.html) ? – Jarod42 Aug 28 '19 at 08:20
  • Maybe this could help you: https://stackoverflow.com/questions/9277906/stdvector-to-string-with-custom-delimiter – mfnx Aug 28 '19 at 08:22
  • @Jarod42: Thanks - careless of me to not see that -, do please put as an answer and I'll vote and accept. – P45 Imminent Aug 28 '19 at 08:26
  • @Someprogrammerdude: Could you please reference the parts of those pages that explicitly makes my question off topic? – P45 Imminent Aug 28 '19 at 08:34
  • 1
    @P45Imminent "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." Asking for functions (even in the standard library) is off-topic. If you had *tried* something, and included an [mcve] and explained how your attempt didn't work (with expected and actual output) then that would be pretty good question. – Some programmer dude Aug 28 '19 at 08:43

2 Answers2

5

There is boost::join:

std::vector<std::string> v = {"Hello", "world"};
const std::string separator = " ";
std::string s = boost::join(v, separator);

Demo

Jarod42
  • 203,559
  • 14
  • 181
  • 302
-2

Yes there is such a function, it is called std::accumulate:

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


int main() {
    std::vector<std::string> v { "a", "b" };
    std::string r = std::accumulate( v.begin(), v.end(), std::string(""),[](std::string a,std::string b){ return a + " | " + b; });
    std::cout << r;
}

output:

 | a | b

Handling the boundaries correct (no | in the beginning), the code gets a tiny bit more verbose: pass v.begin() + 1 and the inital string accordingly, but take care of the edge case of an empty v.

However, not that this naive application of std::accumulate is far from being efficient (see here for details).

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • https://stackoverflow.com/a/18703743/10933809.downvote is not mine – Oblivion Aug 28 '19 at 08:43
  • @Oblivion i stoped caring about votes some time ago... I dont get the `O(n^2)` argument in the link you posted. Passing around reference instead of values is something I considered already when writing the answer and actually dont remember why I didnt do it. PS: I do care about constructive critisism, so thanks for the link – 463035818_is_not_an_ai Aug 28 '19 at 09:45