组合字符串向量 [英] Combining a vector of strings
问题描述
我一直在阅读加速的C ++ 和我必须说这是一本有趣的书。
在第6章中,我必须使用< algorithm>中的函数。从向量< string>串接变成一个字符串。我可以使用累加,但这无济于事,因为字符串容器只能输入push_back个字符。
int main(){
使用命名空间std;
string str =你好,世界!;
vector< string> vec(10,str);
//在此处串联?
返回0;
}
我如何将字符串连接在一起?
假设这是问题6.8,它并不表示您必须使用accumulate-它表示使用库算法。但是,您可以使用累加:
#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<< << std :: endl;
}
所有累加的操作都将 sum设置为第三个参数,然后对于从第一个参数到第二个参数的所有值'val',请执行以下操作:
sum = sum + val
然后返回 sum。尽管在< numeric>
中声明了累加,但它对实现 operator +()
的任何东西都有效p>
I've been reading Accelerated C++ and I have to say it's an interesting book.
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.
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?
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:
#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 = sum + val
it then returns 'sum'. Despite the fact that accumulate is declared in <numeric>
it will work for anything that implements operator+()
这篇关于组合字符串向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!