簡體   English   中英

如何訪問 STL 堆棧 C++ 中的結構屬性?

[英]How to access the struct attributes inside an STL Stack C++?

這是我的代碼

struct vidstruct {
   int Video_ID;  // -> Increment 
   string movietitle = "";
   string genre = "";
   string prod = "";
   int numberOfCopies;
   string MovImg_name;
};

struct customerstruct {
   int Customer_ID;
   string name;
   string adress;
   stack <vidstruct> cstack;  // -> I want to access this one

// here is my declaration of methods
public:
   VideoClass();
   void returnVideo(vidstruct info,int sc); // ---

這是我正在嘗試訪問它的方法:

    void VideoClass::returnVideo(vidstruct info,int sc) {
    int top;
    list<customerstruct>::iterator iter;
    
    for (iter = myListCustomer.begin(); iter != myListCustomer.end(); iter++) { 

        if (sc == iter->Customer_ID) {
            cout << iter->Customer_ID << " " << endl;
            cout << iter->name << " " << endl;
            cout << iter->adress << " " << endl;
            cout << "Videos Rented: " << endl;

            while (!iter->cstack.empty()) {
                iter->cstack.top();
                cout << info.Video_ID << "   " << info.movietitle; // This is what I'm trying to do but it just gives me a random output.

                iter->cstack.pop();

            }

    
   

我想訪問 vidstruct 結構的 Video_ID 和 movietitle 屬性,但我真的不知道該怎么做。

在這里您可以找到一個完整的(但最少的)示例,它將演示您正在尋找的內容(也許)。 我試圖讓事情盡可能接近你的例子。

https://wandbox.org/permlink/88WSws0aM8hVpsDh

請(請:!): go 並了解stacklist的工作方式(以及何時應該使用另一個), structclass之間有什么區別,僅提及我認為可以改進的很少的事情。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM