2

ベクターコピーで複数のコピーを避けるために、私の質問は以下のとおりです。

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

class DataValue {
public:

    DataValue() { std::cout << "DataValue constructor called" << std::endl; }
    DataValue(DataValue const& other) { cout << "DataValue copy constructor called" << std::endl; }
    ~DataValue() { std::cout << "DataValue destructor is called" << std::endl; }
private:

};

class ItemDataHistory {
public:
    ItemDataHistory() { std::cout << "ItemDataHistory constructor called" << std::endl; }
    // ItemDataHistory(ItemDataHistory const & other) { std::cout << "ItemDataHistory copy constructor called" << std::endl; }
    ~ItemDataHistory() { std::cout << "ItemDataHistory destructor called" << std::endl; }
    std::vector<DataValue>& GetVecDataValues() { return m_vecDataValues; }  

private:
    std::vector<DataValue> m_vecDataValues;
};

class DataReply {
public:

    DataReply() { std::cout << "Data reply constructor is called "<< std::endl; }
    ~DataReply() { std::cout << "Data reply destructor is called "<< std::endl; }
    DataReply(const DataReply& ) { std::cout << "Data reply copy constructor is called "<< std::endl; }

    std::vector<ItemDataHistory>& GetItemDataHistories() { return m_vecItemData; }  

private:
    // The list of DataValue
    std::vector<ItemDataHistory> m_vecItemData;
};



void main()
{

    DataValue dv1, dv2, dv3;
    ItemDataHistory itmDH;
    itmDH.GetVecDataValues().reserve(3);
    itmDH.GetVecDataValues().push_back(dv1);
    itmDH.GetVecDataValues().push_back(dv2);
    itmDH.GetVecDataValues().push_back(dv3);

    DataReply dr;
    dr.GetItemDataHistories().reserve(1);
    dr.GetItemDataHistories().push_back(itmDH); // Here copy consturtor of itemdatahistory is called and all data values are copied.
                                                    // Here I want to avoid data values constructor to be called again how can I avoid this
                                                    // How can I directly insert values of dv1, dv2, dv3 into "dr" with out using "itmDH"?
    return;
}

ここで、上記の std::vector m_vecItemData; ではポインターを使用できないことに注意してください。これらはライブラリからのインターフェイスクラスであり、それを制御できないため、データ応答クラスで関数を呼び出しているため、データがスコープ内にある間、関数はデータを使用する可能性があります

私の質問は、コード内の上記のコメントにあります。理由は、何千ものデータ値があるからです。データ値の複数のコンストラクターが呼び出されるのを避けるために、データ値をデータ応答に直接挿入したい (つまり、itmDH ローカル変数を使用しない)

そして他の質問は

データ応答内のデータ値のスペースを予約するにはどうすればよいですか?

4

2 に答える 2

0

C++11 では、次の 2 つのオプションがあります。

  • タイプItemDataHistoryを移動可能にし、データを(可能であれば)移動しますdr.GetItemDataHistories().push_back(std::move(itmDH));
  • コンテナーの新しいメンバー関数を調べますemplace_back()
于 2013-03-08T06:14:38.323 に答える
0

C++11 では、移動セマンティクスを使用できます。

これを行う代わりに:

itmDH.GetVecDataValues().push_back(dv1);
itmDH.GetVecDataValues().push_back(dv2);
itmDH.GetVecDataValues().push_back(dv3);

あなたはこれを行うことができます:

itmDH.GetVecDataValues().push_back(std::move(dv1));
itmDH.GetVecDataValues().push_back(std::move(dv2));
itmDH.GetVecDataValues().push_back(std::move(dv3));

値をコピーする代わりに、単純にベクトルに移動します。

そしてitmDHをコピーする代わりに

dr.GetItemDataHistories().push_back(itmDH);

あなたもそれを動かすことができます:

dr.GetItemDataHistories().push_back(std::move(itmDH));

さらに、移動コンストラクターも必要です。次に例を示します。

DataValue(DataValue&& other){
    std::cout << "DataValue move constructor called" << std::endl;
}

移動代入演算子を宣言して定義することもできます。

DataValue& operator=(DataValue&& other){
    std::cout << "DataValue move assigment operator is called" << std::endl;
    return *this;
}

移動のセマンティクス (および右辺値の参照) を完全に理解するには、次のリンクを参照してください。

http://www.cprogramming.com/c++11/rvalue-references-and-move-semantics-in-c++11.html http://thbecker.net/articles/rvalue_references/section_01.html

于 2013-03-08T06:30:20.440 に答える