C++ 将范围之间的字符串向量转换为分隔字符串(允许boost)

C++ 将范围之间的字符串向量转换为分隔字符串(允许boost),c++,boost,C++,Boost,我有一个大小为10的字符串向量。我想把它连接到一个从索引3到索引6的字符串中,用空格作为分隔符。我知道,boost::algorithm::join可以对整个向量执行,但我希望在特定范围内执行该操作,并具有最小的复制和最佳的效率。我知道很多基于stringstream的解决方案,但我想要的是一些没有stringstream到字符串开销的解决方案。由于您需要减少开销,我建议为std::string实现自定义版本的std::back\u insert\u迭代器。 所以你可以这样做: class my

我有一个大小为10的字符串向量。我想把它连接到一个从索引3到索引6的字符串中,用空格作为分隔符。我知道,
boost::algorithm::join
可以对整个向量执行,但我希望在特定范围内执行该操作,并具有最小的复制和最佳的效率。我知道很多基于stringstream的解决方案,但我想要的是一些没有stringstream到字符串开销的解决方案。

由于您需要减少开销,我建议为
std::string
实现自定义版本的
std::back\u insert\u迭代器。
所以你可以这样做:

class my_back_insert_iterator :
    public std::iterator<std::output_iterator_tag,void,void,void,void>
{
protected:
  std::string* container;

public:
  typedef std::string container_type;
  explicit my_back_insert_iterator(std::string& x) : container(std::addressof(x)) {}

  my_back_insert_iterator& operator=(const std::string& value)
    { container->append(value + " "); return *this; }

  my_back_insert_iterator& operator=(std::string&& value)
    { container->append(std::move(value + " ")); return *this; }

  my_back_insert_iterator& operator*()
    { return *this; }

  my_back_insert_iterator& operator++()
    { return *this; }

  my_back_insert_iterator& operator++(int)
    { return *this; }
};
class my\u back\u insert\u迭代器:
公共标准:迭代器
{
受保护的:
std::string*容器;
公众:
typedef std::字符串容器类型;
显式my_back_insert_迭代器(std::string&x):容器(std::addressof(x)){
my_back_insert_迭代器和运算符=(常量std::字符串和值)
{container->append(value+“”);返回*this;}
我的返回插入迭代器和运算符=(标准::字符串和值)
{container->append(std::move(value+“”);返回*this;}
我的返回插入迭代器和运算符*()
{return*this;}
my_back_insert_迭代器和运算符++()
{return*this;}
my_back_insert_迭代器和运算符++(int)
{return*this;}
};
之后,您可以轻松地执行以下操作:

std::vector<std::string> strings = {"0", "1", "2", "3", "4",
                                    "5", "6", "7", "8", "9"};
std::ptrdiff_t from = 3ULL;
std::ptrdiff_t to = 6ULL;

std::string joinedStrings;
std::move(strings.begin()+from, strings.begin()+to,
          my_back_insert_iterator(joinedStrings));
joinedStrings += *(strings.begin()+to);
std::vector<std::string> strings = {"0", "1", "2", "3", "4",
                                    "5", "6", "7", "8", "9"};
std::ptrdiff_t from = 3ULL;
std::ptrdiff_t to = 6ULL;

std::stringstream ss;
std::copy(strings.begin()+from, strings.begin()+to,
          std::ostream_iterator<std::string>(ss, " "));
ss << *(strings.begin()+to);

std::string joinedStrings = ss.str();
std::向量字符串={“0”、“1”、“2”、“3”、“4”,
"5", "6", "7", "8", "9"};
std::ptrdiff_t from=3ULL;
标准:ptrdiff_t to=6ULL;
std::字符串连接字符串;
std::move(strings.begin()+from,strings.begin()+to,
my_back_insert_迭代器(JoinedString));
joinedStrings+=*(strings.begin()+to);
也试试这个

旧答案 您可以执行以下操作:

std::vector<std::string> strings = {"0", "1", "2", "3", "4",
                                    "5", "6", "7", "8", "9"};
std::ptrdiff_t from = 3ULL;
std::ptrdiff_t to = 6ULL;

std::string joinedStrings;
std::move(strings.begin()+from, strings.begin()+to,
          my_back_insert_iterator(joinedStrings));
joinedStrings += *(strings.begin()+to);
std::vector<std::string> strings = {"0", "1", "2", "3", "4",
                                    "5", "6", "7", "8", "9"};
std::ptrdiff_t from = 3ULL;
std::ptrdiff_t to = 6ULL;

std::stringstream ss;
std::copy(strings.begin()+from, strings.begin()+to,
          std::ostream_iterator<std::string>(ss, " "));
ss << *(strings.begin()+to);

std::string joinedStrings = ss.str();
std::向量字符串={“0”、“1”、“2”、“3”、“4”,
"5", "6", "7", "8", "9"};
std::ptrdiff_t from=3ULL;
标准:ptrdiff_t to=6ULL;
std::stringstream-ss;
std::copy(strings.begin()+from,strings.begin()+to,
std::ostream_迭代器(ss,“”);

ss为了减少开销,我建议为
std::string
实现一个自定义版本的
std::back\u insert\u迭代器。
所以你可以这样做:

class my_back_insert_iterator :
    public std::iterator<std::output_iterator_tag,void,void,void,void>
{
protected:
  std::string* container;

public:
  typedef std::string container_type;
  explicit my_back_insert_iterator(std::string& x) : container(std::addressof(x)) {}

  my_back_insert_iterator& operator=(const std::string& value)
    { container->append(value + " "); return *this; }

  my_back_insert_iterator& operator=(std::string&& value)
    { container->append(std::move(value + " ")); return *this; }

  my_back_insert_iterator& operator*()
    { return *this; }

  my_back_insert_iterator& operator++()
    { return *this; }

  my_back_insert_iterator& operator++(int)
    { return *this; }
};
class my\u back\u insert\u迭代器:
公共标准:迭代器
{
受保护的:
std::string*容器;
公众:
typedef std::字符串容器类型;
显式my_back_insert_迭代器(std::string&x):容器(std::addressof(x)){
my_back_insert_迭代器和运算符=(常量std::字符串和值)
{container->append(value+“”);返回*this;}
我的返回插入迭代器和运算符=(标准::字符串和值)
{container->append(std::move(value+“”);返回*this;}
我的返回插入迭代器和运算符*()
{return*this;}
my_back_insert_迭代器和运算符++()
{return*this;}
my_back_insert_迭代器和运算符++(int)
{return*this;}
};
之后,您可以轻松地执行以下操作:

std::vector<std::string> strings = {"0", "1", "2", "3", "4",
                                    "5", "6", "7", "8", "9"};
std::ptrdiff_t from = 3ULL;
std::ptrdiff_t to = 6ULL;

std::string joinedStrings;
std::move(strings.begin()+from, strings.begin()+to,
          my_back_insert_iterator(joinedStrings));
joinedStrings += *(strings.begin()+to);
std::vector<std::string> strings = {"0", "1", "2", "3", "4",
                                    "5", "6", "7", "8", "9"};
std::ptrdiff_t from = 3ULL;
std::ptrdiff_t to = 6ULL;

std::stringstream ss;
std::copy(strings.begin()+from, strings.begin()+to,
          std::ostream_iterator<std::string>(ss, " "));
ss << *(strings.begin()+to);

std::string joinedStrings = ss.str();
std::向量字符串={“0”、“1”、“2”、“3”、“4”,
"5", "6", "7", "8", "9"};
std::ptrdiff_t from=3ULL;
标准:ptrdiff_t to=6ULL;
std::字符串连接字符串;
std::move(strings.begin()+from,strings.begin()+to,
my_back_insert_迭代器(JoinedString));
joinedStrings+=*(strings.begin()+to);
也试试这个

旧答案 您可以执行以下操作:

std::vector<std::string> strings = {"0", "1", "2", "3", "4",
                                    "5", "6", "7", "8", "9"};
std::ptrdiff_t from = 3ULL;
std::ptrdiff_t to = 6ULL;

std::string joinedStrings;
std::move(strings.begin()+from, strings.begin()+to,
          my_back_insert_iterator(joinedStrings));
joinedStrings += *(strings.begin()+to);
std::vector<std::string> strings = {"0", "1", "2", "3", "4",
                                    "5", "6", "7", "8", "9"};
std::ptrdiff_t from = 3ULL;
std::ptrdiff_t to = 6ULL;

std::stringstream ss;
std::copy(strings.begin()+from, strings.begin()+to,
          std::ostream_iterator<std::string>(ss, " "));
ss << *(strings.begin()+to);

std::string joinedStrings = ss.str();
std::向量字符串={“0”、“1”、“2”、“3”、“4”,
"5", "6", "7", "8", "9"};
std::ptrdiff_t from=3ULL;
标准:ptrdiff_t to=6ULL;
std::stringstream-ss;
std::copy(strings.begin()+from,strings.begin()+to,
std::ostream_迭代器(ss,“”);

ss为了减少开销,我建议为
std::string
实现一个自定义版本的
std::back\u insert\u迭代器。
所以你可以这样做:

class my_back_insert_iterator :
    public std::iterator<std::output_iterator_tag,void,void,void,void>
{
protected:
  std::string* container;

public:
  typedef std::string container_type;
  explicit my_back_insert_iterator(std::string& x) : container(std::addressof(x)) {}

  my_back_insert_iterator& operator=(const std::string& value)
    { container->append(value + " "); return *this; }

  my_back_insert_iterator& operator=(std::string&& value)
    { container->append(std::move(value + " ")); return *this; }

  my_back_insert_iterator& operator*()
    { return *this; }

  my_back_insert_iterator& operator++()
    { return *this; }

  my_back_insert_iterator& operator++(int)
    { return *this; }
};
class my\u back\u insert\u迭代器:
公共标准:迭代器
{
受保护的:
std::string*容器;
公众:
typedef std::字符串容器类型;
显式my_back_insert_迭代器(std::string&x):容器(std::addressof(x)){
my_back_insert_迭代器和运算符=(常量std::字符串和值)
{container->append(value+“”);返回*this;}
我的返回插入迭代器和运算符=(标准::字符串和值)
{container->append(std::move(value+“”);返回*this;}
我的返回插入迭代器和运算符*()
{return*this;}
my_back_insert_迭代器和运算符++()
{return*this;}
my_back_insert_迭代器和运算符++(int)
{return*this;}
};
之后,您可以轻松地执行以下操作:

std::vector<std::string> strings = {"0", "1", "2", "3", "4",
                                    "5", "6", "7", "8", "9"};
std::ptrdiff_t from = 3ULL;
std::ptrdiff_t to = 6ULL;

std::string joinedStrings;
std::move(strings.begin()+from, strings.begin()+to,
          my_back_insert_iterator(joinedStrings));
joinedStrings += *(strings.begin()+to);
std::vector<std::string> strings = {"0", "1", "2", "3", "4",
                                    "5", "6", "7", "8", "9"};
std::ptrdiff_t from = 3ULL;
std::ptrdiff_t to = 6ULL;

std::stringstream ss;
std::copy(strings.begin()+from, strings.begin()+to,
          std::ostream_iterator<std::string>(ss, " "));
ss << *(strings.begin()+to);

std::string joinedStrings = ss.str();
std::向量字符串={“0”、“1”、“2”、“3”、“4”,
"5", "6", "7", "8", "9"};
std::ptrdiff_t from=3ULL;
标准:ptrdiff_t to=6ULL;
std::字符串连接字符串;
std::move(strings.begin()+from,strings.begin()+to,
my_back_insert_迭代器(JoinedString));
joinedStrings+=*(strings.begin()+to);
也试试这个

旧答案 您可以执行以下操作:

std::vector<std::string> strings = {"0", "1", "2", "3", "4",
                                    "5", "6", "7", "8", "9"};
std::ptrdiff_t from = 3ULL;
std::ptrdiff_t to = 6ULL;

std::string joinedStrings;
std::move(strings.begin()+from, strings.begin()+to,
          my_back_insert_iterator(joinedStrings));
joinedStrings += *(strings.begin()+to);
std::vector<std::string> strings = {"0", "1", "2", "3", "4",
                                    "5", "6", "7", "8", "9"};
std::ptrdiff_t from = 3ULL;
std::ptrdiff_t to = 6ULL;

std::stringstream ss;
std::copy(strings.begin()+from, strings.begin()+to,
          std::ostream_iterator<std::string>(ss, " "));
ss << *(strings.begin()+to);

std::string joinedStrings = ss.str();
std::向量字符串={“0”、“1”、“2”、“3”、“4”,
"5", "6", "7", "8", "9"};
std::ptrdiff_t from=3ULL;
标准:ptrdiff_t to=6ULL;
std::stringstream-ss;
std::copy(strings.begin()+from,strings.begin()+to,
std::ostream_迭代器(ss,“”);

ss为了减少开销,我建议为
std::string
实现一个自定义版本的
std::back\u insert\u迭代器。
所以你可以这样做:

class my_back_insert_iterator :
    public std::iterator<std::output_iterator_tag,void,void,void,void>
{
protected:
  std::string* container;

public:
  typedef std::string container_type;
  explicit my_back_insert_iterator(std::string& x) : container(std::addressof(x)) {}

  my_back_insert_iterator& operator=(const std::string& value)
    { container->append(value + " "); return *this; }

  my_back_insert_iterator& operator=(std::string&& value)
    { container->append(std::move(value + " ")); return *this; }

  my_back_insert_iterator& operator*()
    { return *this; }

  my_back_insert_iterator& operator++()
    { return *this; }

  my_back_insert_iterator& operator++(int)
    { return *this; }
};
class my\u back\u insert\u迭代器:
公共标准:迭代器
{
受保护的:
std::string*容器;
公众:
typedef std::字符串容器类型;
显式my_back_insert_迭代器(std::string&x):容器(std::addressof(x)){
my_back_insert_迭代器和运算符=(常量std::字符串和值)
{container->append(value+“”);返回*this;}
我的返回插入迭代器和运算符=(标准::字符串和值)
{container->append(std::move(value+“”);返回*this;}
我的返回插入迭代器和运算符*()
{retur