从STL复制函数以打印出矢量

Copy function from STL to print out vector

本文关键字:打印 STL 复制 函数      更新时间:2023-10-16

我正在尝试从STL打印出具有复制功能的矢量元素。理论上它应该在没有任何故障的情况下工作,但我收到了奇怪的通知,我在谷歌上搜索了一下,但我真的不明白我出了什么故障。让我展示一下我在做什么:

#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;

}

我得到以下错误:

从const char*"到char"的转换无效

我很想得到一些适当的(也很容易理解的)解释我的想法有什么问题;)

干杯!

1.更改:

copy(names.begin(), names.end(), ostream_iterator<string>(cout, ' ');

进入:

copy(names.begin(), names.end(), ostream_iterator<string>(cout, " "));

(字符串文字,而非字符)

2.添加缺失的paren。

3.并添加丢失的<algorithm><iterator><string>报头。

4.添加using namespace std;