안녕은 < 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 목록에서
필요한 비트 수를 줄일 수 있습니까? 또한 C++ 11 (이 질문은로 태그가 붙어 있습니다)에는 표준에 [멋진 C++ 유사 난수 라이브러리] (http://en.cppreference.com/w/cpp/numeric/random)가 있습니다. – 5gon12eder
고마워, MT19937을 사용하려고했는데이 경우에는 kill 이상이 될 것이므로 다른 SSCCE의 데이터로 사용할 벡터를 채울 필요가있다. – SegFault