2015-12-03 163 views

回答

2

这里有一个选项:

std::ostringstream stream; 
std::copy(aSet.begin(), aSet.end(), std::ostream_iterator<std::string>(stream, ",")); 
std::string result = stream.str(); 
1

accumulate example有代码来连接整数字符串,它可以很容易地转换为你的目的的载体:

std::string s = std::accumulate(std::begin(aSet), 
           std::end(aSet), 
           std::string{}, 
           [](const std::string& a, const std::string &b) { 
            return a.empty() ? b 
              : a + ',' + b; }); 
+0

'O(n2)'复杂性。 – chqrlie

1

这里有没有什么东西可以简单易读的方式花式:

string s; 

for (auto const& e : aSet) 
{ 
    s += e; 
    s += ','; 
} 

s.pop_back(); 
+0

对于大集合来说效率极低。 – chqrlie

+0

是的,一如既往地进行分析,如果证明效率低下:优化。可读性首先。 – emlai

+0

C++编译器可能能够优化这种方法的'O(n2)'固有的复杂性,但我对此非常怀疑。 – chqrlie

相关问题