`
zuroc
  • 浏览: 1290191 次
  • 性别: Icon_minigender_1
  • 来自: 江苏
社区版块
存档分类
最新评论

C++ std名字空间ostream_iterator与的诡异问题

    博客分类:
  • C++
阅读更多
为了方便显示map而自定义的两个函数,出现了一个诡异的问题,感谢jinq0123@163.com告之解决方法,但是我对问题的原因还是不太清楚.

这里的Blog好像有问题,我把<该为了《........................

cpp 代码
 
  1. #include 《map》  
  2. #include 《string》  
  3. #include 《iterator》  
  4. #include 《iostream》  
  5.   
  6. using namespace std;  
  7. namespace std     
  8. {     
  9.      //如果不将该函数放入名字空间std则使用ostream_iterator不能通过编译  
  10.      //但是可以直接用cout输出pair  
  11.   
  12.     templateclass _Elem,class _Traits,class K,class V》  
  13.     basic_ostream《_Elem,_Traits》& operator《《(  
  14.         basic_ostream《_Elem,_Traits》& s  
  15.         ,  
  16.         const pair《K,V》& p  
  17.         )  
  18.     {  
  19.         return s《《p.first《《" : "《《p.second;  
  20.     }  
  21.   
  22. }  
  23.   
  24. templateclass _Elem,class _Traits,class K,class V》  
  25. basic_ostream《_Elem,_Traits》&  operator《《(  
  26.     basic_ostream《_Elem,_Traits》& s  
  27.     ,   
  28.     const map《K,V》& m  
  29. )  
  30. {  
  31.     copy(m.begin(), m.end(), ostream_iterator《pair《K,V》 》(s, "\n"));  
  32.     return s;  
  33. }  
  34.   
  35. int main()  
  36. {  
  37.     using namespace std;  
  38.     map《string,string》 a;  
  39.     a["key1"]="value1";  
  40.     a["key2"]="value2";  
  41.     cout《《a;  
  42.     system("pause");  
  43.     return 0;  
  44. }  
分享到:
评论
3 楼 zuroc 2007-05-15  

> using namespace std;
> namespace std
> {
>        //If don't put this function into namespace std , there will be
> a
> compile error .

>        template<class _Elem,class _Traits,class K,class V>
>        basic_ostream<_Elem,_Traits>& operator<<(
>                basic_ostream<_Elem,_Traits>& s
>                ,
>                const pair<K,V>& p
>                )
>        {
>                return s<<p.first<<" : "<<p.second;
>        }

> }

> template<class _Elem,class _Traits,class K,class V>
> basic_ostream<_Elem,_Traits>&  operator<<(
>        basic_ostream<_Elem,_Traits>& s
>        ,
>        const map<K,V>& m
> )
> {
>        copy(m.begin(), m.end(), ostream_iterator<pair<K,V> >(s,
> "\n"));
>        return s;

> }

> int main()
> {
>        using namespace std;
>        map<string,string> a;
>        a["key1"]="value1";
>        a["key2"]="value2";
>        cout<<a;
>        system("pause");
>        return 0;

> }

We had a similar discussion some time ago.
Please read http://tinyurl.com/yvca6w for the
details (If this link does not lead to the correct
entry, please read contribution 14).

The short version is: Don't do that! You are not
allowed to add such an overload to namespace
std which brings you in the realm of undefined
behaviour. The above quoted thread shows in
contribution 14 that there are at least two clean
solutions:

1) Change your operator template overload by a corresponding function template (e.g. "print") in your user namespace. Then replace your std::copy invocation by a std::for_each call (which uses a special print functor and *not* operator<< on std::pair) or use an explicit loop.

2) Use a better customizable iterator like boost::transform_iterator which uses the print-functor mentioned in (1).

Greetings from Bremen,

Daniel Krügler
2 楼 shifan3 2007-05-15  
因为otream_iterator的定义在std里,所以它首先会在std里查找operator》,只要找到一个名字一样的(尽管可能参数不一样),就不会继续到global namespace里查找了
这个确实是标准规定的
1 楼 Elminster 2007-05-10  
名称查找的问题吧 …… 可能是类模板中对引用名字做两段式查找的时候按照类模板定义点来了,不太确定这个行为是否符合标准。

转去 www.cpper.com/c/ 问这个问题吧。

相关推荐

    istream_iterator 读取输入文件

    C++ primer 第五版 中文版 练习 10.33 题目:编写程序,接受三个参数:一个输入文件和两个输出...使用ostream_iterator将奇数写入第一个输出文件,每个值后都跟一个空格。将偶数写入第二个输出文件,每个值都独占一行。

    audio_ostream_src.zip_C# TTS_tts

    TTS,TTS To Speech,Read the text using the microsoft TTS engine,and ACE libraly

    caffe分类器动态库编译

    动态库编译好了,提供dll,lib,.h.以及测试代码、只需要自己的训练的分类器,网络结构均值,类别就可以试用了

    Xcode10,真机模拟器运行错误(std::basic_ostream)解决办法-附件资源

    Xcode10,真机模拟器运行错误(std::basic_ostream)解决办法-附件资源

    二维vector源码实现

     ostream_iterator&lt;int&gt; os(cout, " ");  Matrix[0][2] = 4;  //交换矩阵的两行  Matrix[0].swap(Matrix[1]);  //交换矩阵第二行的两个元素  swap(Matrix[1][0], Matrix[1][2]);  for (uint i = 0; i (); i++...

    C++ 模板写的短小字符串类,用于替换字符数组和std::string

    friend std::ostream & operator(std::ostream& os, const TinyString&lt;K&gt;& str); template&lt;size_t K, size_t L&gt; friend bool operator == (const TinyString&lt;K&gt;& s1, const TinyString&lt;L&gt;& s2); //...... ...

    myostream:方便的输出,适用于所有可迭代项目的容器类型

    C ++标准要求:&gt; = C ++ 11 支持的容器或类似容器的类型: std :: pair std :: tuple std :: array std :: deque std :: forward_list std :: initializer_list std :: list std :: vector std :: set std :: ...

    STL copy()函数用法

    #include #include #include using namespace std;... ostream_iterator ofile&#40;cout, &#41;; //将数值复制到vector里,参数依次是开始,结束,vector数组的开始 //(看起来src+7越界了)src+7,表示结束,sr

    libb64.zip - 用于Base64编码、解码的程序

    一个用于Base64编码、解码的程序, int encode(char value_in); int encode(const char* code_in, const int length_in, char* plaintext_out...void encode(std::istream& istream_in, std::ostream& ostream_in);

    C++中的模拟class string类的代码 cpp

    friend std::ostream & operator(std::ostream &theStream,const string & str); friend const string & operator+(const string &s1,const char * s2); friend const string & operator+(const char * s2,const ...

    ostream用法解释

    C++的ostream类的使用方法,很详细的。。

    STL 源码剖析(侯捷先生译著)

    8.3.3 stream iterators (istream_iterator, ostream_iterator) 442 8.4 function adapters 448 8.4.1 对传回值进行逻辑否定:not1, not2 450 8.4.2 对参数进行系结(绑定):bind1st, bind2nd 451 8.4.3 用于...

    c++教师工资管理系统

    反反复复include #include #include #include #include #include ...using std::ostream; using std::endl; using std::string; using std::vector; using std::iterator; using std::ofstream;

    C++标准库使用范例

    * 功能与使用方法 ***************************/ #include &lt;vector&gt; #include &lt;algorithm&gt; #include &lt;functional&gt; #include &lt;iostream&gt; #include &lt;ostream&gt; using namespace std; bool mod_equal ( int elem1...

    md5算法的c++实现

    friend std::ostream& operator(std::ostream&, MD5 md5); private: void init(); typedef unsigned char uint1; // 8bit typedef unsigned int uint4; // 32bit enum {blocksize = 64}; // VC6 won't eat a ...

    STL源码剖析.pdg

    8.3.3 stream iterators (istream_iterator, ostream_iterator) 442 8.4 function adapters 448 8.4.1 对传回值进行逻辑否定:not1, not2 450 8.4.2 对参数进行系结(绑定):bind1st, bind2nd 451 8.4.3 用于...

    C语言头文件 OSTREAM.H

    C语言头文件 OSTREAM.HC语言头文件 OSTREAM.HC语言头文件 OSTREAM.HC语言头文件 OSTREAM.HC语言头文件 OSTREAM.HC语言头文件 OSTREAM.HC语言头文件 OSTREAM.HC语言头文件 OSTREAM.HC语言头文件 OSTREAM.HC语言头文件...

    C和C++头文件对比一览

    2. 新的C++头文件如iostream虽然提供了和旧有头文件相同的功能,但他的内容都并入了namespace std中,从而有效避免了名字污染的问题。 3. 标准C的头文件如stdio.h继续获得支持,这类文件的内容并未放在std中。 4...

    流类库与输入输出(ostream,ofstream,ostringstream)

    三个重要的输出流: – ostream – ofstream – ostringstream

Global site tag (gtag.js) - Google Analytics