C++ C++;打印容器

C++ C++;打印容器,c++,iterator,containers,C++,Iterator,Containers,我正在尝试打印容器,如集合和地图。我正在使用的书中说以下代码是有效的: #include <iostream> #include <set> #include <iterator> #include <fstream> using namespace std; template <typename Container> void print (const Container & c, ostream & out = c

我正在尝试打印容器,如集合和地图。我正在使用的书中说以下代码是有效的:

#include <iostream>
#include <set>
#include <iterator>
#include <fstream>
using namespace std;

template <typename Container>
void print (const Container & c, ostream & out = cout)
{
    typename Container::const_iterator itr;
    for( itr=c.begin(); itr!=c.end(); ++itr)
        out << *itr << " ";
    out << endl;
}

int main()
{
    ifstream fin("Test.txt");
    set<string> s(  istream_iterator<string>(fin),
                    istream_iterator<string>() );
    print( s );

    return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
模板
无效打印(常量容器和c、ostream和out=cout)
{
typename容器::常量迭代器itr;
对于(itr=c.begin();itr!=c.end();++itr)
输出>x)
s、 插入(x);
编辑:

Visual Studio生成输出:

------ Build started: Project: project, Configuration: Debug Win32 ------
Build started 4/15/2012 1:19:25 PM.
InitializeBuildStatus:

  Touching "Debug\project.unsuccessfulbuild".

ClCompile:

  Project4.cpp

c:\users\admin\documents\visual studio 2010\projects\project\project\project4.cpp(11): error C2825: 'Container': must be a class or namespace when followed by '::'

          c:\users\admin\documents\visual studio 2010\projects\project\project\project4.cpp(22) : see reference to function template instantiation 'void print<std::set<_Kty>
(std::istream_iterator<_Ty>,std::istream_iterator<_Ty> (__cdecl *)(void))>(Container (__cdecl &),std::ostream &)' being compiled

          with

          [

              _Kty=std::string,

              _Ty=std::string,

              Container=std::set<std::string> (std::istream_iterator<std::string>,std::istream_iterator<std::string> (__cdecl *)(void))

          ]

c:\users\admin\documents\visual studio 2010\projects\project\project\project4.cpp(11): error C2039: 'const_iterator' : is not a member of '`global namespace''

c:\users\admin\documents\visual studio 2010\projects\project\project\project4.cpp(11): error C2146: syntax error : missing ';' before identifier 'itr'

c:\users\admin\documents\visual studio 2010\projects\project\project\project4.cpp(11): error C2065: 'itr' : undeclared identifier

c:\users\admin\documents\visual studio 2010\projects\project\project\project4.cpp(12): error C2065: 'itr' : undeclared identifier

c:\users\admin\documents\visual studio 2010\projects\project\project\project4.cpp(12): error C2228: left of '.begin' must have class/struct/union

          type is 'std::set<_Kty> (__cdecl &)'

          with

          [

              _Kty=std::string

          ]

c:\users\admin\documents\visual studio 2010\projects\project\project\project4.cpp(12): error C2065: 'itr' : undeclared identifier

c:\users\admin\documents\visual studio 2010\projects\project\project\project4.cpp(12): error C2228: left of '.end' must have class/struct/union

          type is 'std::set<_Kty> (__cdecl &)'

          with

          [

              _Kty=std::string

          ]

c:\users\admin\documents\visual studio 2010\projects\project\project\project4.cpp(12): error C2065: 'itr' : undeclared identifier

c:\users\admin\documents\visual studio 2010\projects\project\project\project4.cpp(13): error C2065: 'itr' : undeclared identifier



Build FAILED.



Time Elapsed 00:00:01.00

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
----构建已启动:项目:项目,配置:调试Win32------
构建开始于2012年4月15日下午1:19:25。
初始化BuildStatus:
触摸“Debug\project.unsuccessfulbuild”。
CLC编译:
项目4.cpp
c:\users\admin\documents\visual studio 2010\projects\project\project4.cpp(11):错误C2825:“容器”:后跟“:”时必须是类或命名空间
c:\users\admin\documents\visualstudio 2010\projects\project\project4.cpp(22):请参阅正在编译的函数模板实例化“void print(Container(u cdecl&),std::ostream&)”的参考
具有
[
_Kty=std::string,
_Ty=std::string,
Container=std::set(std::istream\u迭代器,std::istream\u迭代器(u\u cdecl*)(void))
]
c:\users\admin\documents\visual studio 2010\projects\project\project4.cpp(11):错误C2039:“常量迭代器”:不是“全局命名空间”的成员
c:\users\admin\documents\visualstudio 2010\projects\project\project4.cpp(11):错误C2146:语法错误:标识符“itr”之前缺少“;”
c:\users\admin\documents\visual studio 2010\projects\project\project4.cpp(11):错误C2065:“itr”:未声明的标识符
c:\users\admin\documents\visual studio 2010\projects\project\project4.cpp(12):错误C2065:“itr”:未声明的标识符
c:\users\admin\documents\visualstudio 2010\projects\project\project4.cpp(12):错误C2228:“.begin”的左边必须有class/struct/union
类型为“std::set(\uu cdecl&)”
具有
[
_Kty=std::string
]
c:\users\admin\documents\visual studio 2010\projects\project\project4.cpp(12):错误C2065:“itr”:未声明的标识符
c:\users\admin\documents\visualstudio 2010\projects\project\project4.cpp(12):错误C2228:“.end”的左边必须有class/struct/union
类型为“std::set(\uu cdecl&)”
具有
[
_Kty=std::string
]
c:\users\admin\documents\visual studio 2010\projects\project\project4.cpp(12):错误C2065:“itr”:未声明的标识符
c:\users\admin\documents\visual studio 2010\projects\project\project4.cpp(13):错误C2065:“itr”:未声明的标识符
生成失败。
时间流逝00:00:01.00
======生成:0成功,1失败,0最新,0跳过==========

它认为这是一个函数声明:

set<string> s(  istream_iterator<string>(fin),
                istream_iterator<string>() );
集合s(istream_迭代器(fin),
istream_迭代器();
添加一对额外的括号将更正它:

set<string> s( (istream_iterator<string>(fin)),
                istream_iterator<string>() );
set s((istream_迭代器(fin)),
istream_迭代器();
有很多这样的例子,所以如果你搜索

编辑:您还需要添加
#include

似乎编译器(也叫clang++)有些混乱。不过,以下内容对我很有用:

#include <iostream>                                                         
#include <set>                                                              
#include <iterator>                                                         
#include <fstream>                                                          

using namespace std;                                                        

template <typename Container>                                               
void print (const Container & c, ostream & out = cout)                      
{                                                                           
    typename Container::const_iterator itr;                                 
    for( itr=c.begin(); itr!=c.end(); ++itr)                                
        out << *itr << " ";                                                 
    out << endl;                                                            
}                                                                           

int main()                                                                  
{                                                                           
    ifstream fin("Test.txt");                                               
    istream_iterator<string> first(fin), last;                              
    set<string> s(first, last);                                             
    print(s);                                                               

    return 0;                                                               
}
#包括
#包括
#包括
#包括
使用名称空间std;
模板
无效打印(常量容器和c、ostream和out=cout)
{                                                                           
typename容器::常量迭代器itr;
对于(itr=c.begin();itr!=c.end();++itr)

您如何调用此函数?使用什么参数?代码看起来很好。可能是您没有使用容器调用
print
函数吗?您的容器是否包含iostream不理解的内容?也不是基元类型?似乎是因为VS编译器有问题。GCC编译了此项功能。原因是什么VS向您发送的错误消息?这与Mat中的代码具有相同的净效果:(请参阅下面的注释)。感谢您的尝试。从这里的响应来看,MSVS似乎是为了让我明白,因为它适用于GCC中的其他所有人。@AnthonyW:您的示例代码在MSVC++2010(警告级别/W4)上为我编译得非常干净在我添加了额外的括号之后,@我就注意到你还需要<代码>包含C++ >代码>我同时尖叫和哭诉。我在Mat的帖子和一大堆类似的帖子中引用了ror。这些帖子都没有指向我的程序本身的任何代码。对不起,你指的是什么?是使用
while
循环编辑的
main
函数吗?在这个帖子中,Mat没有发布任何东西,他也没有在帖子中提到“最烦人的解析”.Mat在他放弃时删除了他的帖子。我所指的错误的修复方法只是
#include
。但是错误太模糊了,我根本就不知道那是问题所在。很高兴你发现了错误。我还发现MSVC错误消息几乎难以理解。你可能想试试。这是最标准的我知道,IMHO会生成最好的错误消息(包括指向错误的颜色和箭头)。我可能必须试一试;我一直在使用Eclipse编写android应用程序,而且它是
#include <iostream>                                                         
#include <set>                                                              
#include <iterator>                                                         
#include <fstream>                                                          

using namespace std;                                                        

template <typename Container>                                               
void print (const Container & c, ostream & out = cout)                      
{                                                                           
    typename Container::const_iterator itr;                                 
    for( itr=c.begin(); itr!=c.end(); ++itr)                                
        out << *itr << " ";                                                 
    out << endl;                                                            
}                                                                           

int main()                                                                  
{                                                                           
    ifstream fin("Test.txt");                                               
    istream_iterator<string> first(fin), last;                              
    set<string> s(first, last);                                             
    print(s);                                                               

    return 0;                                                               
}