[英]Copy function from STL to print out vector
I'm trying to print out vector elements with copy function from STL. 我正在尝试使用STL的复制功能打印出矢量元素。 Theoretically it supposed to work without any malfunction, but I got weird notification, I google it but I really can't comprehend what failure did I make. 从理论上讲它应该可以正常工作,但是我收到了奇怪的通知,我用谷歌搜索了一下,但是我真的不理解我做了什么失败。 Let me show what I'm on about: 让我展示一下我在做什么:
#include <iostream>
#include <vector>
int main(void)
{
vector<string> names;
names.push_back("Jeremy");
names.push_back("James");
names.push_back("Richard");
copy(names.begin(), names.end(), ostream_iterator<string>(cout, ' ');
// this line cause some problems;
}
I got the following error: 我收到以下错误:
invalid conversion from const char*' to char' 从const char *'到char'的无效转换
I would love to obtain some proper (easy to understand as well) explanation what is wrong with my idea ;) 我很想获得一些适当的(也易于理解的)解释,我的想法有什么问题;)
Cheers! 干杯!
1.Change: 1.更改:
copy(names.begin(), names.end(), ostream_iterator<string>(cout, ' ');
into: 成:
copy(names.begin(), names.end(), ostream_iterator<string>(cout, " "));
(string literal, not character) (字符串文字,而不是字符)
2.add missing paren. 2.添加缺少的括号。
3.and add missing <algorithm>
, <iterator>
and <string>
headers. 3.添加缺少的<algorithm>
, <iterator>
和<string>
标头。
4.add using namespace std;
4. using namespace std;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.