나는 내가 만든 클래스의 객체 벡터를 가지고 있으며 각 객체에는 두 개의 double, int 및 bool의 4 개의 변수가 포함되어 있습니다. int는 ID 번호로 사용되어 어떤 객체가 벡터 내에 있는지 추적 할 수 있습니다. 생성 한 다른 클래스의 멤버 함수는 double에 계산을 수행하고 bool (true 및 false)을 조작합니다. random_shuffle을 사용하여 벡터에서 임의의 두 객체가 서로 상호 작용하도록 벡터를 재정렬했지만 문제가 발생합니다. random_shuffle이 작동 한 후에는 두 개의 double과 bool이 적절하게 값을 반환하지만 ID 번호 int는 첫 번째 ID 번호를 가진 객체를 제외한 모든 ID 번호에 대해 횡설수 (-858993460)로 반환됩니다. 다음은 일반적인 코드 버전입니다. 누구든지 ID 번호를 인쇄 할 때 나를 도울 수 있습니까? 대단히 감사합니다.C++ : vector 및 random_shuffle 문제
int main()
{
CalculationClass CalculationObject;
int vectorsize; // number of objects from class "Object" to create
vectorsize = PromptForVectorSize(); // user inputs number of objects to create
std::vector<Object> ObjectVector(vectorsize, Object());
int iterations; // number of loops to run
iterations = PromptForIterations(); // user inputs number of loops
for(int z = 0; z<ObjectVector.size(); z++) // gives signed/unsigned warning
{ // Gives each object an ID number //
CalculationObject.EstablishIDNumbers(ObjectVector[z], z);
}
for(int i = 0; i<iterations; i++)
{
cout << "=========================================" << endl;
cout << "RUN NUMBER " << i+1 << endl;
std::random_shuffle (ObjectVector.begin(), ObjectVector.end());
for(size_t x = 0; x<ObjectVector.size(); x+=2)
{
cout << ObjectVector[x].getIDNumber() << endl;
cout << ObjectVector[x].getObjectDouble() << endl;
cout << ObjectVector[x+1].getIDNumber() << endl;
cout << ObjectVector[x+1].getObjectDouble() << endl;
CalculationObject.Function(ObjectVector[x], ObjectVector[x+1]);
cout << ObjectVector[x].getIDNumber() << endl;
cout << ObjectVector[x].getObjectDouble() << endl;
cout << ObjectVector[x+1].getIDNumber() << endl;
cout << ObjectVector[x+1].getObjectDouble() << endl;
cout << "=========================================" << endl;
}
}
return 0;
}
'Object'와'CalculationClass :: EstablishIDNumbers()'의 정의를 보지 않고서는 이것을 진단하기가 어려울 것입니다. – cdhowie
void CalculationClass :: EstablishIDNumbers (Object & Object, int & i) const { \t Object.setIDNumber (i + 1); Object 클래스에서 } – user2690673
INT OpecAgent :: setAgentIDNumber (INT idnumber) \t \t \t \t \t \t \t \t \t AgentIDNumber = idnumber; \t return AgentIDNumber; \t \t \t \t \t \t \t } – user2690673