程序代码:
#include <iostream>
#include <list>
#include <string>
struct teacher
{
std::string name;
std::string title;
teacher()
{
}
teacher( const std::string& name, const std::string& title ) : name(name),title(title)
{
}
friend std::istream& operator>>( std::istream& is, teacher& tcr );
friend std::ostream& operator<<( std::ostream& os, const teacher& tcr );
};
std::istream& operator>>( std::istream& is, teacher& tcr )
{
return is >> tcr.name >> tcr.title;
}
std::ostream& operator<<( std::ostream& os, const teacher& tcr )
{
return os << tcr.name << '\t' << tcr.title;
}
int main( void )
{
std::list<teacher> teachers;
std::cout << "请输入教师的名称与职称:\n";
for( teacher tcr; std::cin>>tcr; )
teachers.push_back( tcr );
std::cout << "您输入教师的名称与职称:\n";
for( std::list<teacher>::const_iterator itor=teachers.begin(); itor!=teachers.end(); ++itor )
std::cout << *itor << '\n';
}
输入:
张三 校长
李四 主任
王五 教工
^Z (Windows上是Ctrl+Z,linux上是Ctrl+D)
输出:
您输入教师的名称与职称:
张三 校长
李四 主任
王五 教工