当放置在其他对象向量中时,随机数向量的值设置为 0。

Random number vector's values set to 0 when placed inside other objects vector.

你好有一个问题,下面的 <elemVec> 被放置在 <ElementA> 对象的另一个向量中,但内容是全部设置为 0。我有一个调试行显示 <elemData> 的内容,它清楚地表明值正在添加到向量中。

#include "ElementA.h"
#include <iostream>
#include <random>
#include <ctime>

using std::cout;
using std::endl;

int main(int argc, char** argv)
{
    vector<ElementA> elemVec;
    std::srand(time(NULL));

    //! Generate random sized vectors to populate elemVec.
    for (size numElems = 0; numElems < 40; ++numElems) {
        //! Populate data for ElementA::mSolnSpace
        size vecSize = rand() % 10;
        vector<size> elemData;
        for (size i = 0; i < vecSize; ++i) {
            elemData.push_back(i);
        }

        //! DEBUG: Output elemData.
        vector<size>::const_iterator it = elemData.cbegin();
        vector<size>::const_iterator end = elemData.cend();
        for (; it != end; ++it) {
            cout << *it << " ";
        }
        cout << endl;

        //! Create the ElementA objects in the vector.
        size bID = rand() % 10;
        elemVec.emplace_back(ElementA(bID, elemData));
    }


    vector<ElementA>::const_iterator it = elemVec.cbegin();
    vector<ElementA>::const_iterator end = elemVec.cend();
    for (; it != end; ++it) {
        it->output();
        cout << endl;
    }


    std::cin.get();
    return 0;
}

和元素 A class:

#ifndef ELEM_A_H
#define ELEM_A_H

#include <vector>
#include <iostream>

using std::vector;
using std::cout;
using std::endl;

//! Shorthand specifications for common Data/Container types.
typedef unsigned short int size;


/*****************************************************************************\
|                               Class Definitions                             |
\*****************************************************************************/
class ElementA
{
public:
    //! Constructor(s) & Destructor:
    ElementA() = delete; //! Explicitly delete the default CTOR.
    ElementA(const size& BlkID, const vector<size>& Vec);
    ElementA(const ElementA& src);
    virtual ~ElementA() {};

    //! View/Controller Method(s):
    size getID() const { return mBlkID; };
    size getSolnSize() const { return mSolnSize; };
    vector<size> getSoln() const { return mSolnSpace; };
    void removeSoln(const size& value);
    void output() const;

    //! Overloaded Operator(s):
    friend bool operator<(const ElementA& lhs, const ElementA& rhs);

protected:
    //! Data:
    size mBlkID;
    size mSolnSize;
    vector<size> mSolnSpace;

};


/*****************************************************************************\
|                                   Methods                                   |
\*****************************************************************************/


/************************************************************************/
/*  Constructor:Parameter based: Initializes parameters                 */
/*  Parameters: const size& RID, const size& CID, const size& BID,      */
/*              const vector<size>& VEC)                                */
/*  Auxiliary:  n/a                                                     */
/*  Returns:    n/a                                                     */
/*  Exceptions: n/a                                                     */
/************************************************************************/
ElementA::ElementA(const size& BlkID, const vector<size>& Vec) : mBlkID(BlkID), 
                                                                 mSolnSpace(Vec)
{
    mSolnSize = Vec.size();
}


/************************************************************************/
/*  Constructor:Performs copy construction using <src>.                 */
/*  -- (Copy)                                                           */
/*  Parameters: const ElementA& src                                     */
/*  Auxiliary:  n/a                                                     */
/*  Returns:    n/a                                                     */
/*  Exceptions: n/a                                                     */
/************************************************************************/
ElementA::ElementA(const ElementA& src) : mBlkID(src.mBlkID), 
                        mSolnSize(src.mSolnSize), mSolnSpace(src.mSolnSize)
{
}



/************************************************************************/
/*  removeSoln: Updates the ElementA object by removing a number from   */
/*              the Solution Space                                      */
/*  Parameters: n/a                                                     */
/*  Auxiliary:  n/a                                                     */
/*  Returns:    n/a                                                     */
/*  Exceptions: n/a                                                     */
/************************************************************************/
void ElementA::removeSoln(const size& value)
{
    vector<size>::iterator it = mSolnSpace.begin();
    vector<size>::iterator end = mSolnSpace.end();

    for (; it != end; ++it) {
        if (*it == value) {
            mSolnSpace.erase(it);
            --mSolnSize;
            break;
        }
    }

}


/************************************************************************/
/*  operator<:  Comparator tests whether <lhs> is strictly less than    */
/*              <rhs>.                                                  */
/*  Parameters: const ElementA& lhs, const ElementA& rhs                */
/*  Auxiliary:  n/a                                                     */
/*  Returns:    Boolean                                                 */
/*  Exceptions: n/a                                                     */
/************************************************************************/
bool operator<(const ElementA& lhs, const ElementA& rhs)
{
    return lhs.mSolnSize < rhs.mSolnSize;
}


/************************************************************************/
/*  output:     Outputs the contents of <this> Element.                 */
/*  Parameters: n/a                                                     */
/*  Auxiliary:  n/a                                                     */
/*  Returns:    n/a                                                     */
/*  Exceptions: n/a                                                     */
/************************************************************************/
void ElementA::output() const
{
    cout << "BlockID: " << mBlkID << "\tSize: " << mSolnSize << "\t\tSoln: ";

    vector<size>::const_iterator it = mSolnSpace.cbegin();
    vector<size>::const_iterator end = mSolnSpace.cend();

    for (; it != end; ++it) {
        cout << *it << " ";
    }
    cout << endl;
}

#endif

非常感谢您花时间和帮助,谢谢!

发生这种情况是因为您对 emplace_back 的调用是对复制构造函数的调用

elemVec.emplace_back(ElementA(bID, elemData));

并且在 ElementA 的复制构造函数的初始化列表中,

mSolnSpace(src.mSolnSize)

创建一个带有 src.mSolnSize 个零的向量,而它应该是

mSolnSpace(src.mSolnSpace)

复制 src.mSolnSpace.

要获得 emplace_back 的好处,您应该写

elemVec.emplace_back(bID, elemData);

它使用带有这些参数的构造函数。

当然,您还应该修复该复制构造函数。

您的错误在这里:

ElementA::ElementA(const ElementA& src) : mBlkID(src.mBlkID), 
                    mSolnSize(src.mSolnSize), mSolnSpace(src.mSolnSize)

应该是...mSolnSpace(src.mSolnSpace).