C ++打印容器

时间:2012-04-15 16:59:16

标签: c++ iterator containers

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

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

然而,我从Visual Studio的编译器中得到了一个错误。 我缺少什么?我知道它可能是简单的包含但我不熟悉STL容器和C ++迭代器。

我已经有#include <iterator>

错误:

  • 'Container':后跟'::'
  • 时必须是类或命名空间
  • 'itr':未声明的标识符
  • 'const_iterator':不是'`global namespace''
  • 的成员

还有一些我肯定是第一个结果。

修改

根据教科书,以下代码应与我的主编相同。我无法让它工作但它可能会有所帮助:

ifstream fin("Test.txt");
string x;
set<string> s;
while( fin >> x)
    s.insert(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 ==========

2 个答案:

答案 0 :(得分:2)

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

set<string> s(  istream_iterator<string>(fin),
                istream_iterator<string>() );

添加一对额外的括号将纠正它:

set<string> s( (istream_iterator<string>(fin)),
                istream_iterator<string>() );

如果您搜索Most Vexing Parse,则有很多这方面的例子。

修改:您还需要添加#include <string>

答案 1 :(得分:0)

似乎编译器(也是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;                                                               
}

但是,我建议将print函数转换为接受使用std::copy输出的迭代器范围的函数,如下所示:http://www.sgi.com/tech/stl/ostream_iterator.html