在C ++中将用户输入附加到数组

时间:2014-04-04 04:27:48

标签: c++ arrays append

我试图编写一个程序,要求用户输入(一次一个,并一直持续到用户以某种方式中断)并将它们存储在一个数组中。在python中,我可以轻松地将新输入附加到现有列表的末尾,但这在C ++中不起作用。在没有涉及向量的情况下,最简单的方法是什么?我想每次删除数组并创建一个更大的数组,但这似乎很痛苦。

1 个答案:

答案 0 :(得分:0)

这是我编写的一些代码,它基本上创建了一个新数组并复制旧数组中的数据。希望这可以帮助你。为了给你一个它的用法示例,有一个演示区域,从stdin输入数据,直到用户键入“END”并将其打印到stdout,不包括“END”。

#include <cstdio>
#include <iostream>
#include <cstring>

//Assuming you are storing strings
//Set this to the appropriate max length. The name of this may be misleading,
const int MAX_STRING_LENGTH = 128;                   //any other suggestions?
char** userInput;
int userInputUsed;
int userInputSize;

void append (char* text, int textLength) {
    //If we have used up all the space in the array
    if (userInputUsed >= userInputSize) {
        //How large you want the new array to be compared to
        //the original size (userInputSize)
        int newArraySize = 2*userInputSize;
        //Create the new array
        char** newUserInput = new char*[newArraySize];
        //We are only creating the new part of the array
        //Another way you could do this is to create the strings as you go
        for (int i = userInputUsed;i < newArraySize;i++) {
            newUserInput[i] = new char[MAX_STRING_LENGTH];
        }
        //Copy everything over, I am setting our pointers to the old data
        for (int i = 0;i < userInputUsed;i++) {
            newUserInput[i] = userInput[i];
        }
        //Delete the old array
        delete[] userInput;
        //Set the new array to the old array
        userInput = newUserInput;
        //Update the size of our array;
        userInputSize = newArraySize;
    }
    //Copy the input to userInput
    memcpy(userInput[userInputUsed], text, textLength);
    userInputUsed++;
}

int main () {

    //Initialise userInput, initialise to whatever size you deem fit
    userInputSize = 1;
    userInput = new char*[userInputSize];
    for (int i = 0;i < userInputSize;i++) {
        userInput[i] = new char[MAX_STRING_LENGTH];
    }

    //Start of demonstration
    //Get input until the user types "END"
    for (bool running = true;running;) {
        char temp[MAX_STRING_LENGTH];
        //Scans in until some whitespace, this may not work if
        //you want to scan in whole lines which end in '\n'
        //scanf("%s", temp);
        //or
        std::cin >> temp;
        //End if input is "END"
        if (strcmp(temp, "END") == 0) {
            running = false;
        } else {
            append(temp, strlen(temp));
        }
    }

    //Print out the user input, to see that it is inputed correctly
    for (int i = 0;i < userInputUsed;i++) {
        //printf("%s\n", userInput[i]);
        //or
        std::cout << userInput[i] << std::endl;
    }
    //End of demonstration

    //Cleanup our user input
    for (int i = 0;i < userInputSize;i++) {
        delete[] userInput[i];
    }
    delete[] userInput;

    //Stop the program from ending, you may not need this
    //while(true);

    return 0;
}

随意评论或建议改进此答案。