C++ 组合字符串向量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1985978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:45:47  来源:igfitidea点击:

Combining a vector of strings

c++stringvectorstl

提问by Bogdan

I've been reading Accelerated C++and I have to say it's an interesting book.

我一直在阅读Accelerated C++,不得不说这是一本有趣的书。

In chapter 6, I have to use a function from <algorithm> to concatenate from a vector<string> into a single string. I could use accumulate, but it doesn't help because string containers can only push_back characters.

在第 6 章中,我必须使用 <algorithm> 中的函数将 vector<string> 连接成单个字符串。我可以使用累积,但它没有帮助,因为字符串容器只能 push_back 字符。

int main () {
  using namespace std;
  string str = "Hello, world!";
  vector<string>  vec (10, str);
  // Concatenate here?

  return 0;
}

How do I join the strings together?

如何将字符串连接在一起?

回答by

Assuming this is question 6.8, it doesn't say you have to use accumulate - it says use "a library algorithm". However, you can use accumulate:

假设这是问题 6.8,它并没有说您必须使用累积 - 它说使用“​​库算法”。但是,您可以使用累积:

#include <numeric>

int main () {
    std::string str = "Hello World!";
    std::vector<std::string> vec(10,str);
    std::string a = std::accumulate(vec.begin(), vec.end(), std::string(""));
    std::cout << a << std::endl;
}

All that accumulate does is set 'sum' to the third parameter, and then for all of the values 'val' from first parameter to second parameter, do:

累积所做的只是将“sum”设置为第三个参数,然后对于从第一个参数到第二个参数的所有值“val”,请执行以下操作:

sum = sum + val

it then returns 'sum'. Despite the fact that accumulate is declared in <numeric>it will work for anything that implements operator+()

然后返回“总和”。尽管在其中声明了积累这一事实,<numeric>但它适用于任何实现operator+()

回答by Sanjaya R

How about std::copy?

std::copy 怎么样?

std::ostringstream os;
std::copy( vec_strings.begin(), vec_string.end(), ostream_iterator<string>( os ) );
cout << os.str() << endl;

回答by pixelgrease

The following snippet compiles in Visual C++ 2012 and uses a lambda function:

以下代码段在 Visual C++ 2012 中编译并使用 lambda 函数:

int main () {
    string str = "Hello World!";
    vector<string>  vec (10,str);

    stringstream ss;
    for_each(vec.begin(), vec.end(), [&ss] (const string& s) { cat(ss, s); });

    cout << ss.str() << endl;
}

The accumulateexample in the 1st answer is elegant, but as sellibitzepointed out, it reallocates with each concatenation and scales at O(N2). This for_eachsnippet scales at about O(N). I profiled both solutions with 100K strings; the accumulateexample took 23.6 secs, but this for_eachsnippet took 0.054 sec.

accumulate一个答案中的示例很优雅,但正如sellibitze指出的那样,它重新分配每个连接并按 O(N2) 进行缩放。此for_each代码段的缩放比例约为 O(N)。我用 100K 字符串分析了两种解决方案;该accumulate示例耗时 23.6 秒,但此for_each代码段耗时 0.054 秒。

回答by Prasoon Saurav

I am not sure about your question.Where lies the problem? Its just a matter of a loop.

我不确定你的问题。问题出在哪里?它只是一个循环的问题。

#include<vector>
#include<string>
#include<iostream>

int main () 
{
    std::string str = "Hello World!";
    std::vector<string>  vec (10,str);

    for(size_t i=0;i!=vec.size();++i)
        str=str+vec[i];
    std::cout<<str;
}

EDIT :

编辑 :

Use for_each()from <algorithm>

使用for_each()<algorithm>

Try this:

尝试这个:

#include<vector>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
string i;
void func(string &k)
{
  i+=k;
}
int main () {
    string str = "Hello World!";
    vector<string>  vec (10,str);

    for_each(vec.begin(),vec.end(),func);
    cout<<i;
    return 0;
  }