代码之家  ›  专栏  ›  技术社区  ›  Prenom Nom

标准队列误解

  •  1
  • Prenom Nom  · 技术社区  · 1 年前

    我有一个简单的C++结构,可以构建一个 std::队列 使用主电源的argv。

    然后,它提供了一种方法 ::下一页 在将当前值从队列中弹出之前返回它。

    如果我将此方法作为函数的参数调用,它将把参数还原为:

    wut(cunext(),cunexts());

    以[1,2]作为main上的条目,它给了我以下输出:

    第一个参数是2
    第二个参数为1

    这是我对std::queue的误解吗?

    有罪代码:

    #include <iostream>
    #include <queue>
    
    using namespace std;
    
    struct Args
    {
        public:
            typedef std::string Arg;
    
            Args(int argc, char* argv[]);
    
            Arg current() const;
            Arg cunext();
    
        private:
            std::queue<std::string> q_args; ///< Queue of arguments as std::string objects.
    };
    
    Args::Args(int argc, char* argv[]):
        q_args()
    {
        for(int i=1; i<argc; i++)   // build queue for arguments
        {
            q_args.push(*(++argv));   // skip first argument (as for path)
        }
    };
    
    inline Args::Arg Args::current() const
    {
        return q_args.front();
    };
    
    inline Args::Arg Args::cunext()
    {
        Arg ret = current();
        q_args.pop();
        return ret;
    };
    
    void wut(std::string bad, std::string good)
    {
        cout << "first param is " << bad << endl;
        cout << "second param is " << good << endl;
    }
    
    int main(int argc, char* argv[])
    {
        Args args(argc, argv);
        wut(args.cunext(),args.cunext());
        return 0;
    }
    
    1 回复  |  直到 1 年前
        1
  •  7
  •   Alessandro Bertulli    1 年前

    您对 std::queue 是正确的,因为它 is pointed out in the documentation. 但是,请记住,在C/C++中,标准没有指定参数传递给函数的顺序(在ABI中定义),也没有 评估它们的顺序 。您使用的编译器决定从右向左计算它们,另一个编译器本可以选择从左向右。

    推荐文章