注册 登录
编程论坛 C++教室

求助:stl中 MAP怎么用copy以迭代的方式输出,代码已贴出,无法编译通过

纯黑色 发布于 2013-08-01 19:54, 604 次点击
程序代码:

#include <iostream>
#include <map>
#include <iterator>

using namespace std;

int main()
{
    typedef map<int,string> map_test;
    typedef pair<int,string> pair_test;
    typedef ostream_iterator <pair> ostream_itr;
   
    map_test te1;
    pair_test p1(1,"ti");
    for(int i=0;i<5;i++)
    te1.insert(p1);
   
    copy(te1.begin(),te1.end(),ostream_itr(cout,"\n"));
    return 0;
}

求大神帮忙
3 回复
#2
peach54602013-08-02 06:21
http://www.
#3
rjsp2013-08-02 08:49
程序代码:
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
#include <iterator>

typedef std::map<int,std::string> map_test;
typedef std::pair<int,std::string> pair_test;
typedef std::ostream_iterator<pair_test> ostream_itor;

namespace std
{
    std::ostream& operator<<( std::ostream& os, const pair_test& obj )
    {
        return os << obj.first << ", " << obj.second;
    }
}

using namespace std;

int main()
{
    map_test te;
    for( size_t i=0; i<5; ++i )
        te.insert( make_pair(i,"abc") );

    copy( te.begin(), te.end(), ostream_itor(cout,"\n") );

    return 0;
}
#4
纯黑色2013-08-02 09:34
回复 3楼 rjsp
是利用重载自己写一个map的 输出吗?
PS:操作符重载这块儿,学语法的时候我没认真学,my fault.
1