矢量未正确清除

时间:2016-04-08 20:00:01

标签: c++ vector tuples clear

我在清除矢量矢量时遇到了问题。

我使用元组函数返回多个变量。

我连续两次调用元组函数(每次使用不同的文件)。

当我检查函数的输出时,我得到了等价的

vector1.size = vector1.size 和         vector2.size = (vector1.size + vector2.size)

而不是 vector1.size= vector1.sizevector2.size = vector2.size

这让我相信某处一个向量在函数调用之间没有被正确清除,这导致第二个输出被写在第一个输出之上。

以下是我的代码:

main()和声明

tuple<vector<float>, vector<vector<float>>, int> ReadFile(string fileToBeRead);

vector<float> sadInterpolationStorage;
vector<vector<float>> sadInterpolationVectorStorage;
vector<float> userInterpolationStorage;
vector<vector<float>> userInterpolationVectorStorage;
int sadAnimationLength;
int inputAnimationLength;

int main() {

    tie(sadInterpolationStorage, sadInterpolationVectorStorage, sadAnimationLength) = ReadFile(sadWalkCycleMocapFile);

    tie(userInterpolationStorage, userInterpolationVectorStorage, inputAnimationLength) = ReadFile(inputMocapFile);

cout << sadInterpolationVectorStorage[1].size() << endl;
cout << userInterpolationVectorStorage[1].size() << endl;


return 0;
 }

userInterpolationVectorStorage[1].size()将始终等于总和    (sadInterpolationVectorStorage[1].size()+ sadInterPolationVectorStorage[1].size()似乎某个矢量未被清除,但在ReadFile()中我为所有内容创建了新变量。

的ReadFile()

tuple<vector<float>, vector<vector<float>>, int> ReadFile(string fileToBeRead) {
string firstLineInFile;
string secondLineInFile;
string thirdLineInFile;
vector<float> interpolationStorage;
vector<vector<float>> interpolationVectorStorage; 
int animationLength = 0;
vector<vector<float>> vectorStorage;

//Create a vector to hold all the mocap data values in float form.
vector<float> floatTokens;
//Create a vector to hold an entire frame (the data and the words).
vector<string> oneFrame;

//Create an input file stream
ifstream in(fileToBeRead, ios::in);
//cases to handle the first 3 lines of the file (first 3 lines contain no data, but are necessary)
//extracts each line into an object and writes them to the top of the output file
getline(in, firstLineInFile);
getline(in, secondLineInFile);
getline(in, thirdLineInFile);


//loop until the end of the file is reached
while (in.eof() == 0) {

    //create a buffer to store each frame
    stringstream buffer;
    //write the frameID to the file.
    extractFrameID(in);
    //loop around the 29 lines in a frame, push each line into the vector.
    for (int i = 0; i < 29; i++) {
        string tempString;
        getline(in, tempString);

        //if the end of the file is reached (.empty() is used as the last line in the mocap file is always an empty line.
        if (tempString.empty()) {

#pragma region Storing all data vectors in a vector
            vectorStorage.push_back(rootXVector);
            vectorStorage.push_back(rootZVector);

            vectorStorage.push_back(lowerBackXVector);
            vectorStorage.push_back(lowerBackYVector);
            vectorStorage.push_back(lowerBackZVector);
            vectorStorage.push_back(upperBackXVector);
            vectorStorage.push_back(upperBackYVector);
            vectorStorage.push_back(upperBackZVector);
            vectorStorage.push_back(thoraxXVector);
            vectorStorage.push_back(thoraxYVector);
            vectorStorage.push_back(thoraxZVector);
            vectorStorage.push_back(lowerNeckXVector);
            vectorStorage.push_back(lowerNeckYVector);
            vectorStorage.push_back(lowerNeckZVector);
            vectorStorage.push_back(upperNeckXVector);
            vectorStorage.push_back(upperNeckYVector);
            vectorStorage.push_back(upperNeckZVector);
            vectorStorage.push_back(headXVector);
            vectorStorage.push_back(headYVector);
            vectorStorage.push_back(headZVector);
            vectorStorage.push_back(rClavicleYVector);
            vectorStorage.push_back(rClavicleZVector);
            vectorStorage.push_back(rHumerusXVector);
            vectorStorage.push_back(rHumerusYVector);
            vectorStorage.push_back(rHumerusZVector);
            vectorStorage.push_back(rRadiusXVector);
            vectorStorage.push_back(rWristYVector);
            vectorStorage.push_back(rHandXVector);
            vectorStorage.push_back(rHandYVector);
            vectorStorage.push_back(rFingersXVector);
            vectorStorage.push_back(rThumbXVector);
            vectorStorage.push_back(rThumbZVector);
            vectorStorage.push_back(lClavicleYVector);
            vectorStorage.push_back(lClavicleZVector);
            vectorStorage.push_back(lHumerusXVector);
            vectorStorage.push_back(lHumerusYVector);
            vectorStorage.push_back(lHumerusZVector);
            vectorStorage.push_back(lRadiusXVector);
            vectorStorage.push_back(lWristYVector);
            vectorStorage.push_back(lHandXVector);
            vectorStorage.push_back(lHandYVector);
            vectorStorage.push_back(lFingersXVector);
            vectorStorage.push_back(lThumbXVector);
            vectorStorage.push_back(lThumbZVector);
            vectorStorage.push_back(rFemurXVector);
            vectorStorage.push_back(rFemurYVector);
            vectorStorage.push_back(rFemurZVector);
            vectorStorage.push_back(rTibiaXVector);
            vectorStorage.push_back(rFootXVector);
            vectorStorage.push_back(rFootZVector);
            vectorStorage.push_back(rToesXVector);
            vectorStorage.push_back(lFemurXVector);
            vectorStorage.push_back(lFemurYVector);
            vectorStorage.push_back(lFemurZVector);
            vectorStorage.push_back(lTibiaXVector);
            vectorStorage.push_back(lFootXVector);
            vectorStorage.push_back(lFootZVector);
            vectorStorage.push_back(lToesXVector);
#pragma endregion   
            //loop for every data type, 58 or so
            for (size_t i = 0; i < vectorStorage.size(); i++) {
                //interpolate between each data value and store the result in a vector.
                //loop for every data value of every data type , 227 or so (1 per frame).
                for (size_t j = 1; j < vectorStorage[i].size(); j++)
                {

                    interpolationStorage.push_back(vectorStorage[i][j] - vectorStorage[i][j - 1]);

                }
                interpolationVectorStorage.push_back(interpolationStorage);
                interpolationStorage.clear();

            }   
            vectorStorage.clear();

            cout << "Reading of " << fileToBeRead << " completed" << endl;

            return make_tuple(interpolationStorage, interpolationVectorStorage, animationLength);
        }
        oneFrame.push_back(tempString);
    }

    //populate the buffer with the vector.
    copy(oneFrame.begin(), oneFrame.end(), ostream_iterator<string>(buffer, "\n"));

    //split the buffer up into tokens(objects) and store them into a vector 
    vector<string> mainTokenVector = split(buffer.str(), ' ');

    //defining  vectors.
    vector<float> floatTokenVector;
    vector<string> stringTokenVector;

    //loop to split up the token vector into strings and floats, and store them in vectors
    for (size_t i = 0; i < mainTokenVector.size(); i++) {
        //if the token is a string, put it in the string vector
        if (isFloat(mainTokenVector[i]) == 0) {
            stringTokenVector.push_back(mainTokenVector[i]);
        }
        //if the token is a float, put it in the float vector
        else if (isFloat(mainTokenVector[i]) == 1) {
            floatTokenVector.push_back(stof(mainTokenVector[i]));
        }

    }

#pragma region Pushing data values to vectors
    //pushing all data values to their responding vectors in order to interpolate between them later.

    rootXVector.push_back(floatTokenVector[0]);
    rootZVector.push_back(floatTokenVector[2]);

    lowerBackXVector.push_back(floatTokenVector[6]);
    lowerBackYVector.push_back(floatTokenVector[7]);
    lowerBackZVector.push_back(floatTokenVector[8]);
    upperBackXVector.push_back(floatTokenVector[9]);
    upperBackYVector.push_back(floatTokenVector[10]);
    upperBackZVector.push_back(floatTokenVector[11]);
    thoraxXVector.push_back(floatTokenVector[12]);
    thoraxYVector.push_back(floatTokenVector[13]);
    thoraxZVector.push_back(floatTokenVector[14]);
    lowerNeckXVector.push_back(floatTokenVector[15]);
    lowerNeckYVector.push_back(floatTokenVector[16]);
    lowerNeckZVector.push_back(floatTokenVector[17]);
    upperNeckXVector.push_back(floatTokenVector[18]);
    upperNeckYVector.push_back(floatTokenVector[19]);
    upperNeckZVector.push_back(floatTokenVector[20]);
    headXVector.push_back(floatTokenVector[21]);
    headYVector.push_back(floatTokenVector[22]);
    headZVector.push_back(floatTokenVector[23]);
    rClavicleYVector.push_back(floatTokenVector[24]);
    rClavicleZVector.push_back(floatTokenVector[25]);
    rHumerusXVector.push_back(floatTokenVector[26]);
    rHumerusYVector.push_back(floatTokenVector[27]);
    rHumerusZVector.push_back(floatTokenVector[28]);
    rRadiusXVector.push_back(floatTokenVector[29]);
    rWristYVector.push_back(floatTokenVector[30]);
    rHandXVector.push_back(floatTokenVector[31]);
    rHandYVector.push_back(floatTokenVector[32]);
    rFingersXVector.push_back(floatTokenVector[33]);
    rThumbXVector.push_back(floatTokenVector[34]);
    rThumbZVector.push_back(floatTokenVector[35]);
    lClavicleYVector.push_back(floatTokenVector[36]);
    lClavicleZVector.push_back(floatTokenVector[37]);
    lHumerusXVector.push_back(floatTokenVector[38]);
    lHumerusYVector.push_back(floatTokenVector[39]);
    lHumerusZVector.push_back(floatTokenVector[40]);
    lRadiusXVector.push_back(floatTokenVector[41]);
    lWristYVector.push_back(floatTokenVector[42]);
    lHandXVector.push_back(floatTokenVector[43]);
    lHandYVector.push_back(floatTokenVector[44]);
    lFingersXVector.push_back(floatTokenVector[45]);
    lThumbXVector.push_back(floatTokenVector[46]);
    lThumbZVector.push_back(floatTokenVector[47]);
    rFemurXVector.push_back(floatTokenVector[48]);
    rFemurYVector.push_back(floatTokenVector[49]);
    rFemurZVector.push_back(floatTokenVector[50]);
    rTibiaXVector.push_back(floatTokenVector[51]);
    rFootXVector.push_back(floatTokenVector[52]);
    rFootZVector.push_back(floatTokenVector[53]);
    rToesXVector.push_back(floatTokenVector[54]);
    lFemurXVector.push_back(floatTokenVector[55]);
    lFemurYVector.push_back(floatTokenVector[56]);
    lFemurZVector.push_back(floatTokenVector[57]);
    lTibiaXVector.push_back(floatTokenVector[58]);
    lFootXVector.push_back(floatTokenVector[59]);
    lFootZVector.push_back(floatTokenVector[60]);
    lToesXVector.push_back(floatTokenVector[61]);
#pragma endregion

    //clear the vectors to prepare them for the next frame.
    oneFrame.clear();
    mainTokenVector.clear();
    stringTokenVector.clear();
    floatTokenVector.clear();
    animationLength++;
}
//close the file currently associated with the object, disassociating it from the stream.
in.close();

}

我花了几个小时试图弄清楚为什么会这样,但没有成功,所以任何帮助都会非常感激。如果您有任何疑问,请告诉我。 干杯!

1 个答案:

答案 0 :(得分:1)

想出来。

我已经宣布了所有的身体向量(04-08 16:22:55.973: E/AndroidRuntime(4150): FATAL EXCEPTION: main 04-08 16:22:55.973: E/AndroidRuntime(4150): Process: com.example.project, PID: 4150 04-08 16:22:55.973: E/AndroidRuntime(4150): java.lang.IllegalStateException: Could not execute method of the activity 04-08 16:22:55.973: E/AndroidRuntime(4150): at android.view.View$1.onClick(View.java:3823) 04-08 16:22:55.973: E/AndroidRuntime(4150): at android.view.View.performClick(View.java:4438) 04-08 16:22:55.973: E/AndroidRuntime(4150): at android.view.View$PerformClick.run(View.java:18422) 04-08 16:22:55.973: E/AndroidRuntime(4150): at android.os.Handler.handleCallback(Handler.java:733) 04-08 16:22:55.973: E/AndroidRuntime(4150): at android.os.Handler.dispatchMessage(Handler.java:95) 04-08 16:22:55.973: E/AndroidRuntime(4150): at android.os.Looper.loop(Looper.java:136) 04-08 16:22:55.973: E/AndroidRuntime(4150): at android.app.ActivityThread.main(ActivityThread.java:5001) 04-08 16:22:55.973: E/AndroidRuntime(4150): at java.lang.reflect.Method.invokeNative(Native Method) 04-08 16:22:55.973: E/AndroidRuntime(4150): at java.lang.reflect.Method.invoke(Method.java:515) 04-08 16:22:55.973: E/AndroidRuntime(4150): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 04-08 16:22:55.973: E/AndroidRuntime(4150): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 04-08 16:22:55.973: E/AndroidRuntime(4150): at dalvik.system.NativeStart.main(Native Method) 04-08 16:22:55.973: E/AndroidRuntime(4150): Caused by: java.lang.reflect.InvocationTargetException 04-08 16:22:55.973: E/AndroidRuntime(4150): at java.lang.reflect.Method.invokeNative(Native Method) 04-08 16:22:55.973: E/AndroidRuntime(4150): at java.lang.reflect.Method.invoke(Method.java:515) 04-08 16:22:55.973: E/AndroidRuntime(4150): at android.view.View$1.onClick(View.java:3818) 04-08 16:22:55.973: E/AndroidRuntime(4150): ... 11 more 04-08 16:22:55.973: E/AndroidRuntime(4150): Caused by: java.lang.NullPointerException 04-08 16:22:55.973: E/AndroidRuntime(4150): at com.example.project.changePassword.updatePassword(changePassword.java:69) 04-08 16:22:55.973: E/AndroidRuntime(4150): ... 14 more            lowerBackXVector            函数外部lowerBackYVector)等,因此它们没有被重置。我在函数中定义了它们,现在它完美地运行了。谢谢你们。