2015-01-13 2 views
0

안녕은 < elemVec를가> 아래 < ElementA> 객체의 다른 벡터에 emplaced 도착하는 데 문제가 있지만, 내용은 모두 내가 < elemData의 내용을 표시하는 것 디버그 라인이 0으로 설정되고있다>을 보면 값이 벡터에 추가되고 있음이 명확하게 표시됩니다. 다른 객체 벡터 내부에 배치하면 난수 벡터의 값이 0으로 설정됩니다.

#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 년대의 initialiser 목록에서

이 생성자 복사 복사 생성자의 호출이기 때문에

+0

필요한 비트 수를 줄일 수 있습니까? 또한 C++ 11 (이 질문은로 태그가 붙어 있습니다)에는 표준에 [멋진 C++ 유사 난수 라이브러리] (http://en.cppreference.com/w/cpp/numeric/random)가 있습니다. – 5gon12eder

+0

고마워, MT19937을 사용하려고했는데이 경우에는 kill 이상이 될 것이므로 다른 SSCCE의 데이터로 사용할 벡터를 채울 필요가있다. – SegFault

답변

1

mSolnSpace(src.mSolnSize) 

src.mSolnSize 제로와 벡터를 생성, 발생 , 있어야합니다.

mSolnSpace(src.mSolnSpace) 

src.mSolnSpace을 복사합니다.

는 당신이 그 매개 변수를 사용하여 생성자를 사용

elemVec.emplace_back(bID, elemData); 

를 작성해야, emplace_back의 혜택을 얻을합니다.

물론 복사 생성자를 고쳐야합니다.

+0

감사합니다. 특히 emplace_back() 팁을 고맙게 생각합니다! – SegFault

0

버그는 여기에 있습니다 :

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

이 있어야한다 ... mSolnSpace(src.mSolnSpace).

+0

또는'= default'. – Jarod42

+0

감사합니다. 문제가 해결되었습니다. – SegFault

관련 문제