c++ - 当放置在其他对象 vector 中时,随机数 vector 的值设置为 0。

标签 c++ c++11

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

#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;
}

和 ElementA 类:

#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 零创建一个 vector ,而它应该是

mSolnSpace(src.mSolnSpace)

复制 src.mSolnSpace

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

elemVec.emplace_back(bID, elemData);

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

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

关于c++ - 当放置在其他对象 vector 中时,随机数 vector 的值设置为 0。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27915933/

相关文章:

c++ - 为什么重复使用条件变量的互斥量会导致死锁?

c++ - C++ 引用是否保证使用指针 "internally"?

c++ - C++ 中的 golang 样式 "defer"

c++ - 有 C++11 临界区吗?

c++ - 顺时针旋转M*N矩阵90度,C++

c++ - 在现代 C++ 中实现专门的数据结构

com - std::unique_ptr和指针到指针

c++ - 将容器 value_type 作为模板参数传递?

c++ - QtCreator : symbol(s) not found for architecture x86_64 issue on Mac OS Mavericks

c++ - 使用 msbuild 通过命令行编译时出现 LNK2019 错误