Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to implement analog of Python's 'separator'.join() in C++?

Tags:

All I've found is boost::algorithm::string::join. However, it seems like overkill to use Boost only for join. So maybe there are some time-tested recipes?

UPDATE:
Sorry, the question caption were bad. I'm looking for method to concatenate strings with separator, not just to concatenate one-by-one.

like image 852
Kirill Avatar asked May 23 '11 13:05

Kirill


People also ask

How do you join a separator in Python?

Python String join() Methodjoin() is an inbuilt string function in Python used to join elements of the sequence separated by a string separator. This function joins elements of a sequence and makes it a string.

What does join () do in Python?

The join() method takes all items in an iterable and joins them into one string. A string must be specified as the separator.

Can you join a set Python?

Sets can be joined in Python in a number of different ways. For instance, update() adds all the elements of one set to the other. Similarly, union() combines all the elements of the two sets and returns them in a new set. Both union() and update() operations exclude duplicate elements.

How do you join a list of objects in Python?

join() to combine a list of objects into a string. Use list comprehension by using str(object) on each object to return a list of strings. Then use the str. join() method to combine the list of strings into a single string.


3 Answers

Since you're looking for a recipe, go ahead and use the one from Boost. Once you get past all the genericity, it's not too complicated:

  1. Allocate a place to store the result.
  2. Add the first element of the sequence to the result.
  3. While there are additional elements, append the separator and the next element to the result.
  4. Return the result.

Here's a version that works on two iterators (as opposed to the Boost version, which operates on a range.

template <typename Iter>
std::string join(Iter begin, Iter end, std::string const& separator)
{
  std::ostringstream result;
  if (begin != end)
    result << *begin++;
  while (begin != end)
    result << separator << *begin++;
  return result.str();
}
like image 91
Rob Kennedy Avatar answered Oct 12 '22 22:10

Rob Kennedy


If you really want ''.join(), you can use std::copy with an std::ostream_iterator to a std::stringstream.

#include <algorithm> // for std::copy
#include <iterator>  // for std::ostream_iterator

std::vector<int> values(); // initialize these
std::stringstream buffer;
std::copy(values.begin(), values.end(), std::ostream_iterator<int>(buffer));

This will insert all the values to buffer. You can also specify a custom separator for std::ostream_iterator but this will get appended at the end (this is the significant difference to join). If you don't want a separator, this will do just what you want.

like image 41
Björn Pollex Avatar answered Oct 12 '22 22:10

Björn Pollex


simply, where the type in the container is an int:

std::string s = std::accumulate(++v.begin(), v.end(), std::to_string(v[0]),
                     [](const std::string& a, int b){
                           return a + ", " + std::to_string(b);
                     });
like image 25
man Avatar answered Oct 12 '22 22:10

man