32

在我的代码中,有一个循环将“数字”之类的东西添加到字符串流中。当它结束时,如果要重复循环,我需要提取','添加'}'并添加'{'。

我以为我可以使用 ignore() 删除“,”,但它没有用。你知道我该怎么做我所描述的吗?

例子:

douCoh << '{';
for(unsigned int i=0;i<dataSize;i++)
  if(v[i].test) douCoh << i+1 << ',';
douCoh.get(); douCoh << '}';
4

9 回答 9

58

您可以使用 查找stringstream并返回 1 个字符stringstream::seekp。请注意,它不会删除最后一个字符,而只会移动写入头。在这种情况下这就足够了,因为我们用 . 覆盖了最后一个字符}

douCoh << '{';
for(unsigned int i=0;i<dataSize;i++)
  if(v[i].test) douCoh << i+1 << ',';
douCoh.seekp(-1,douCoh.cur); douCoh << '}';
于 2014-10-21T17:13:37.003 回答
41

您可以提取字符串(使用str()成员),删除最后一个字符,std::string::erase然后将新字符串作为缓冲区重置为std::ostringstream.

但是,更好的解决方案是不首先插入多余','的内容,方法是执行以下操作:

std::ostringstream douCoh;
const char* separator = "";

douCoh << '{';
for (size_t i = 0; i < dataSize; ++ i)
{
  if (v[i].test)
  {
    douCoh << separator << i + 1;
    separator = ",";
  }
}
douCoh << '}';
于 2010-12-28T13:55:07.197 回答
32

I have had this very problem and I found out that you can simply do:

douCoh.seekp(-1, std::ios_base::end);

And the keep inserting data. As others stated, avoiding inserting the bad data in the first place is probably the ideal solution, but in my case was the result of a 3rd party library function, and also I wanted to avoid copying the data to strings.

于 2016-02-22T17:48:28.533 回答
6
stringstream douCoh;
for(unsigned int i=0;i<dataSize;i++)
  if(v[i].test)
    douCoh << ( douCoh.tellp()==0 ? '{' : ',' ) << i+1;
douCoh << '}';
于 2011-12-07T23:37:14.107 回答
5

自 c++11 以来,我已经使用pop_back()字符串的方法找到了这种方式。可能不如上面更聪明的那些,但在更复杂的情况下和/或对懒惰的人有用。

douCoh << '{';
for(unsigned int i=0;i<dataSize;i++)
  if(v[i].test) douCoh << i+1 << ',';

string foo(douCoh.str());
foo.pop_back();
douCoh.str(foo);
douCoh.seekp (0, douCoh.end);  

douCoh << '}';
于 2013-04-10T10:35:33.250 回答
2

享受 std::copy、迭代器和特征的乐趣。您要么必须假设您的数据是反向可迭代的(end - 1),要么您的输出可以倒带。我选择倒带更容易。

#include <ostream>
#include <algorithm>
#include <iterator>

namespace My
{
  template<typename Iterator>
  void print(std::ostream &out, Iterator begin, Iterator end)
  {
    out << '{';
    if (begin != end) {
      Iterator last = end - 1;
      if (begin != last) {
        std::copy(begin, last, std::ostream_iterator< typename std::iterator_traits<Iterator>::value_type  >(out, ", "));
      }
      out << *last;
    }
    out << '}';
  }
}

#include <iostream>

int main(int argc, char** argv)
{
  My::print(std::cout, &argv[0], &argv[argc]);
  std::cout << '\n';
}
于 2010-12-28T15:33:11.250 回答
1

您可以使用std::string::erase直接从基础字符串中删除最后一个字符。

于 2010-12-28T12:33:38.170 回答
1

为什么不直接检查柜台?并且不插入','

douCoh << '{';
for(unsigned int i=0;i<dataSize;i++){
  if(v[i].test){
    douCoh << i+1;
    if(i != dataSize - 1) douCoh << ',';
  }
}
/*douCoh.get();*/ douCoh << '}';
于 2010-12-28T14:00:45.743 回答
1
#include <sstream>
#include <vector>
#include <iterator>
#include <algorithm>

template<typename T>
std::string implode(std::vector<T> vec, std::string&& delim) 
{
    std::stringstream ss;
    std::copy(vec.begin(), vec.end(), std::ostream_iterator<std::string>(ss, delim.c_str()));

    if (!vec.empty()) {
        ss.seekp(-1*delim.size(), std::ios_base::end);
        ss<<'\0';
    }

    return ss.str();
}

int main()
{
    std::cout<<implode(std::vector<std::string>{"1", "2", "3"}, ", ");

    return 0;   
}
于 2019-06-25T14:54:08.853 回答