C++ 将用户输入附加到C++;
我试图编写一个程序,请求用户输入(一次一个,直到用户不知何故中断为止)并将其存储在数组中。在Python中,我可以很容易地将新的输入追加到现有列表的末尾,但这在C++中是不起作用的。在不涉及向量的情况下,最简单的方法是什么?我正在考虑每次删除数组并创建一个更大的新数组,但这似乎很痛苦。这是我为此编写的一些代码,它基本上创建一个新数组并从旧数组复制数据。希望这能帮助你。为了给你一个使用它的例子,有一个演示区,它从stdin输入数据,直到用户键入“END”并将其打印到stdout,不包括“END”C++ 将用户输入附加到C++;,c++,arrays,append,C++,Arrays,Append,我试图编写一个程序,请求用户输入(一次一个,直到用户不知何故中断为止)并将其存储在数组中。在Python中,我可以很容易地将新的输入追加到现有列表的末尾,但这在C++中是不起作用的。在不涉及向量的情况下,最简单的方法是什么?我正在考虑每次删除数组并创建一个更大的新数组,但这似乎很痛苦。这是我为此编写的一些代码,它基本上创建一个新数组并从旧数组复制数据。希望这能帮助你。为了给你一个使用它的例子,有一个演示区,它从stdin输入数据,直到用户键入“END”并将其打印到stdout,不包括“END”
#包括
#包括
#包括
//假设您正在存储字符串
//将此设置为适当的最大长度。这一名称可能会产生误导,
const int MAX_STRING_LENGTH=128//还有其他建议吗?
字符**用户输入;
int userInputUsed;
int userInputSize;
void append(char*text,int textLength){
//如果我们已经用完了数组中的所有空间
if(userInputUsed>=userInputSize){
//要与新阵列进行比较的大小
//原始大小(userInputSize)
int newArraySize=2*userInputSize;
//创建新数组
char**newUserInput=new char*[newArraySize];
//我们只创建阵列的新部分
//另一种方法是边走边创建字符串
for(int i=userInputUsed;i>温度;
//如果输入为“结束”,则结束
如果(strcmp(温度,“结束”)==0){
运行=错误;
}否则{
附加(临时,strlen(临时));
}
}
//打印出用户输入,以查看输入是否正确
for(int i=0;i std::为什么不使用向量?老实说,这是作业的一部分。我想知道是否有一种方法可以做到这一点,而不会偏离我们在课堂上讨论的内容太远。但如果使用向量是目前为止最好的方法,那就去做吧。当我们不知道你在课堂上学了什么时,很难回答。但很难理解GIN在不使用C++类的情况下教C++,但也许我给类似的问题贴上的答案会帮助你:如果你想成为一个聪明的驴,你可以使用一个STD::列表,它有一个与向量非常相似的接口,但它不是矢量。嘿,赋值仍然有效:PHere的任务的精确措辞:“不要使用标准模板库(STL)或其他外部库中的元素”
#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;
}