<legend id='Qm3gh'><style id='Qm3gh'><dir id='Qm3gh'><q id='Qm3gh'></q></dir></style></legend>

      <small id='Qm3gh'></small><noframes id='Qm3gh'>

    1. <tfoot id='Qm3gh'></tfoot>

        <bdo id='Qm3gh'></bdo><ul id='Qm3gh'></ul>

    2. <i id='Qm3gh'><tr id='Qm3gh'><dt id='Qm3gh'><q id='Qm3gh'><span id='Qm3gh'><b id='Qm3gh'><form id='Qm3gh'><ins id='Qm3gh'></ins><ul id='Qm3gh'></ul><sub id='Qm3gh'></sub></form><legend id='Qm3gh'></legend><bdo id='Qm3gh'><pre id='Qm3gh'><center id='Qm3gh'></center></pre></bdo></b><th id='Qm3gh'></th></span></q></dt></tr></i><div id='Qm3gh'><tfoot id='Qm3gh'></tfoot><dl id='Qm3gh'><fieldset id='Qm3gh'></fieldset></dl></div>

      为什么 ostream_iterator 不能按预期工作?

      Why does ostream_iterator not work as expected?(为什么 ostream_iterator 不能按预期工作?)
          <bdo id='UiFsw'></bdo><ul id='UiFsw'></ul>

          <legend id='UiFsw'><style id='UiFsw'><dir id='UiFsw'><q id='UiFsw'></q></dir></style></legend>

        • <i id='UiFsw'><tr id='UiFsw'><dt id='UiFsw'><q id='UiFsw'><span id='UiFsw'><b id='UiFsw'><form id='UiFsw'><ins id='UiFsw'></ins><ul id='UiFsw'></ul><sub id='UiFsw'></sub></form><legend id='UiFsw'></legend><bdo id='UiFsw'><pre id='UiFsw'><center id='UiFsw'></center></pre></bdo></b><th id='UiFsw'></th></span></q></dt></tr></i><div id='UiFsw'><tfoot id='UiFsw'></tfoot><dl id='UiFsw'><fieldset id='UiFsw'></fieldset></dl></div>
            <tbody id='UiFsw'></tbody>
          <tfoot id='UiFsw'></tfoot>

                <small id='UiFsw'></small><noframes id='UiFsw'>

                本文介绍了为什么 ostream_iterator 不能按预期工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                以下代码不用多说:

                #include <utility>
                #include <vector>
                #include <iostream>
                #include <iterator>
                
                using namespace std;
                
                typedef pair<char, char> PAIR;
                
                ostream& operator <<(ostream& os, const PAIR& r)
                {
                    return os << r.first;
                }
                
                int main() 
                {
                    vector<PAIR> coll; 
                
                    cout << coll[0]; // OK. 
                
                    // The following line will cause a compilation error! Why???
                    copy(coll.begin(), coll.end(), ostream_iterator<PAIR>(cout)); 
                }
                

                推荐答案

                问题是名称查找没有找到你的operator<<(ostream& os, const PAIR& r).尝试调用 operator<< 的代码位于 ostream_iterator<> 内的某处,它本身位于 std 命名空间内.名称查找在 ostream_iterator<>std 命名空间中寻找正确的函数;参数相关查找在这里没有帮助,因为这两个参数也都在 std 命名空间中.

                The problem is that the name lookup does not find your operator<<(ostream& os, const PAIR& r). The code that tries to invoke the operator<< is in somewhere inside the ostream_iterator<> which is itself inside the std namespace. The name lookup looks around for the right function inside ostream_iterator<> and the std namespace; the argument dependent lookup does not help here because both of the parameters are in the std namespace, too.

                所以,我的建议是 (1) 要么将您的运算符包装到 namespace std { } 中,但那是 UB、IIRC.或者 (2) 创建一个继承自 std::pair 的结构体以在您的命名空间中定义一个新类型,并使用 ADL 查找您的 operator<<().

                So, my suggestion is (1) either to wrap your operator into namespace std { }, but that is UB, IIRC. Or (2) create a struct inheriting from std::pair to define a new type in your namespace, and using the ADL to find your operator<<().

                更新:

                我的第三个建议是使用自定义操纵器打印出该对.

                My 3rd suggestion is to use a custom manipulator to print out the pair.

                至于我的第二个建议,如果您可以使用 C++11,那么从 std::pair 继承应该很容易(未经测试):

                As for my 2nd suggestion, if you can use C++11, inheriting from std::pair should be easy (untested):

                struct PAIR : std::pair
                {
                  using std::pair::pair;
                };
                

                如果您不能使用 C++11,那么我建议您使用自定义操纵器.

                If you cannot use C++11, then I suggest using a custom manipulator.

                这篇关于为什么 ostream_iterator 不能按预期工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)
                How should a size-limited stl-like container be implemented?(应该如何实现大小受限的 stl 类容器?)
                Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)
                STL BigInt class implementation(STL BigInt 类实现)
                Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)
                Move list element to the end in STL(在 STL 中将列表元素移动到末尾)

                  <bdo id='vRONc'></bdo><ul id='vRONc'></ul>

                    1. <tfoot id='vRONc'></tfoot>
                        • <legend id='vRONc'><style id='vRONc'><dir id='vRONc'><q id='vRONc'></q></dir></style></legend>

                          <small id='vRONc'></small><noframes id='vRONc'>

                          <i id='vRONc'><tr id='vRONc'><dt id='vRONc'><q id='vRONc'><span id='vRONc'><b id='vRONc'><form id='vRONc'><ins id='vRONc'></ins><ul id='vRONc'></ul><sub id='vRONc'></sub></form><legend id='vRONc'></legend><bdo id='vRONc'><pre id='vRONc'><center id='vRONc'></center></pre></bdo></b><th id='vRONc'></th></span></q></dt></tr></i><div id='vRONc'><tfoot id='vRONc'></tfoot><dl id='vRONc'><fieldset id='vRONc'></fieldset></dl></div>

                            <tbody id='vRONc'></tbody>