简体   繁体   English

如何将矢量对象从一类传递到另一类

[英]how to pass vector objects from one class to another

This is how I had tried sending the vector using one of the answers I got., but has not worked out yet.Should copy whn it gets to auctioneer.I want to display. 这是我尝试使用获得的答案之一发送矢量的方法,但尚未解决。应将其复制给拍卖人。我想显示。

class Trader {   
private:   
        int nextBidId; 

public:   

        void loadRange( vector <Bid> & bids ) {} ;

class Simulator //Receives from the Trader,doent modify 
{ 
    vector <Bid> bids;
    Trader trader; 
    Auctioneer auctioneer; 

public: 
    void run(); 
}; 

void Simulator::run() { 
    trader.loadRange(vector<Bid> & bids);
    auctioneer.accept_bids(bid_vector::const_iterator begin, bid_vector::const_iterator end);
    auctioneer.displayBids();
} 

class Auctioneer // Has to receive from the simulator class 
{ 

public: 
    vector <Bid> bids,v2;
    void accept_bids(vector<Bid> & bids);
    void displayBids(){cout << "\tBid\t(" <<  setw(3) << bids.bidId << "\t " << setw(3) << bids.trdId  << "\t "  
                                          << setw(3) <<  bids.type <<"\t " << setw(3) << bids.qty <<"\t "  << setw(3) 
                                          << bids.price <<")\t\n "  ;    }
}; 

UPDATE 更新

I have just copied a vector from another class, am now trying to see its contednts.its returning an error: 我刚刚从另一个类复制了一个向量,现在试图查看其contednts.it返回错误:

Error: - begin has not been declared.how do I decalre bigin for the vector? 错误:-尚未声明begin。如何为矢量去除比金?

void Auctioneer::accept_bids(const BidList& bid){ 
    vector<Auctioneer> *list;
    vector<Auctioneer>::iterator itr; // create an iterator

     for ( itr = list.begin();
     itr != list.end(); ++itr )
     cout << *itr << ' ';
}

Option 1: Just pass a copy of the vector 选项1:只需传递向量的副本

Simply pass the vector into a function in class B (eg B::accept_bids(const bid_vector& bids) , or perhaps using iterators B::accept_bids(bid_vector::const_iterator begin, bid_vector::const_iterator end) ), and have that function copy the vector into a private data member with the assignment operator (or std::copy() if you use iterators). 只需将向量传递到B类的函数中(例如B::accept_bids(const bid_vector& bids) ,或使用迭代器B::accept_bids(bid_vector::const_iterator begin, bid_vector::const_iterator end) ),然后复制该函数使用赋值运算符将向量转换成私有数据成员std::copy()如果使用迭代器,则为std::copy() )。 This is the simplest and most straightforward solution. 这是最简单,最直接的解决方案。

Option 2: Your vector is large, and the costs (allocation time, copy time, memory use) of copying it are high 选项2:向量很大,复制的成本(分配时间,复制时间,内存使用量)很高

Pass the vector as above, but instead of taking a copy of the vector, store the reference (or the iterators) and ensure that class A's copy of the original vector does not go away or change after it has been passed to class B. 如上传递向量,但不要复制向量,而要存储引用(或迭代器),并确保原始向量的A类副本在传递给B类后不会消失或改变。

Alternately, pass it using std::auto_ptr and make it clear (with a function name such as give_bid_vector(std::auto_ptr<bid_vector> bids) ) that class B is taking ownership of the vector. 或者,使用std::auto_ptr传递它,并清楚地表明(具有函数名称,例如give_bid_vector(std::auto_ptr<bid_vector> bids) ),则B类正在拥有该向量的所有权。 This means class A is no longer the owner of the vector and should not delete it. 这意味着类A不再是向量的所有者,并且不应删除它。

I'm assuming this is the bit of code you are talking about: 我假设这是您正在谈论的一些代码:

{
    trader.loadRange(bidlist, NUMBIDS); 
    auctioneer.receiveBids(bidlist, NUMBIDS);
    auctioneer.displayBids(string ss){return str;}
}

In which case you need to make the three functions look something like this: 在这种情况下,您需要使这三个函数看起来像这样:

class Trader {
   ...
   void loadRange( vector <Bid> & bids ) {
      // do stuff with bids
   }
};

the other functions look the same. 其他功能看起来相同。 Then modify the calling code: 然后修改调用代码:

{
    vector <Bid> bidList;
    trader.loadRange(bidlist ); 
    auctioneer.receiveBids(bidlist);
    auctioneer.displayBids(string ss){return str;}
}

Note there is no need to pass the size around - vectors have their own size() member. 请注意,不需要传递大小-向量具有自己的size()成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM