gpt4 book ai didi

c++ - 删除和移动数组 C++ 中的剩余元素

转载 作者:行者123 更新时间:2023-11-30 02:25:55 26 4
gpt4 key购买 nike

List.cpp(类定义)

我一直在编写旨在帮助熟悉类的代码。我的代码目前具有为用户显示选项菜单的功能。选项 [1] 假设添加一个字符串,选项 [2] 假设从列表中删除一个字符串,选项 [3] 打印字符串列表,选项 [4] 退出。我的选项 [1] 似乎工作正常,因为用户可以一次输入一个字符串,但我很难删除一个字符串。这些字符串当前存储在一个包含 10 个元素的数组中。我相信我为删除字符串而编写的函数是可以的,因为我已经对其进行了调试并且它似乎成功了,但是,我没有在我的控制台窗口中看到结果。

我的数组位于我类(class)的一个私有(private)类(class)中:string items[MAX_ITEMS];以及另一个名为:int totalItems;

然后使用 switch case 在我的主函数中调用该类:

//This code snippet below is located in a separate cpp file with main
cout << "Please enter the text you want to remove: " << endl;
cin >> userInput;
list1.remove(userInput);
////////////////////////////////////////////////////////////////////

//preprocessor directives
#include <iostream>
#include <string>

//header files
#include "list.h"

using namespace std;

List::List()
{
//clear array prior to starting (set everything to NULL)
for (int i = 0; i < MAX_ITEMS; i++)
{
items[i] = " ";
}

totalItems = 0;
}

//void List::init()
//{
// string items[MAX_ITEMS];
// totalItems = 0;
//}


bool List::insert(const string& data)
{

//verifies that string is not empty, not in the list, and not full
if (data.empty() == true || inList(data) == true || isFull() == true)
{
return false;
}
else
{
// items[isFull)] = data;
// totalItems++;
items[totalItems++] = data;
return true;
}
}


bool List::isEmpty() const
{
//runs through loop to verify array is empty
for (int i = 0; i < MAX_ITEMS; i++)
{
if (items[i].empty() != true)
{
return false;
}
}
return true;
}

//identifies whether the string is full or not
bool List::isFull() const
{
if (totalItems == MAX_ITEMS)
{
return true;
}
else
{
return false;
}
}

//identifies whether the string is already in the list or not
bool List::inList(const string& theList)
{
for (int i = 0; i < MAX_ITEMS; i++)
{
if (items[i] == theList)
{
return true;
}
}
return false;
}

bool List::remove(const string& data)
{
if (inList(data) == true || data.empty() == true)
{
return false;
}

for (int i = 0; i < MAX_ITEMS; i++)
{
if (items[i] == data)
{
items[i] == " ";
for (int j = i; j < MAX_ITEMS; j++)
{
items[j] = items[j + 1];
items[MAX_ITEMS - 1] == " ";
break;
}
}
}
totalItems--;
return true;
}

//prints list
void List::printList()
{
for (int i = 0; i < MAX_ITEMS; i++)
{
cout << i << items[i] << '\t';
}
}

list_test.cpp (main.cpp)

#include <iostream>
#include <string>

//header files
#include "list.h"

using namespace std;

//function prototypes
int showSelection();

int main()
{
List list1;

string userInput = "";
int userChoice;

// list1.init();

userChoice = showSelection();

while (userChoice != 4)
{
switch(userChoice)
{
case 1:
cout << "Please enter the text you want to add: " << endl;
cin >> userInput;
list1.insert(userInput);

/*if (list1.inList(userInput) == false)
{
cout << "Text is already entered in the list!" << endl;
}*/

if (list1.isFull() == true)
{
cout << "You have entered the MAXIMUM amount of elements!" << endl;
}
break;
case 2:
cout << "Please enter the text you want to remove: " << endl;
cin >> userInput;
list1.remove(userInput);
break;
case 3:
cout << "Printed list: " << endl;
list1.printList();
break;
}
userChoice = showSelection();
}

cout << "Goodbye. Please press enter to exit." << endl;
//TESTING PURPOSES FOR FUNCTIONS
cout << list1.insert(userInput) << endl;
cout << list1.isEmpty() << endl;
cout << list1.isFull() << endl;
cout << list1.inList(userInput) << endl;


return 0;
}

/* ===========================================
Name: showSelection
Desc: displays menu for user to choose options
for their inputted string(s).
Args: none
Retn: none
=========================================== */
int showSelection()
{
int userChoice;
bool exit = false;

while (exit == false)
{
cout << "\nTo select an option, please enter the corresponding number: " << endl;
cout << "[1] to add a string" << endl;
cout << "[2] to remove a string" << endl;
cout << "[3] to print a string" << endl;
cout << "[4] to exit" << endl << endl;
cin >> userChoice;

cout << "You entered option: " << userChoice << endl;
cout << '\n';

if (userChoice == 1 || userChoice == 2 || userChoice == 3 || userChoice == 4)
{
exit = true;
}
else
{
cout << "Invalid selection" << endl;
}
}`enter code here`
return userChoice;
}

最佳答案

以下是您的代码可能的样子:

bool List::remove(const string& data) {
// only check if the list is empty so you don't nececarily go through it
// you shoudn't ask here if the given string is in the list
// because you will search for it anyway just below
if (data.empty())
return false;

for (int i = 0; i < MAX_ITEMS; ++i) {
if (items[i] == data) { // now if it was found
items[i] = " "; // set it to your empty value
--totalItems; // prefix -- is faster then postfix one (no copy is created)
for (int j = i; j < MAX_ITEMS - 1; ++j) {
// stop at j < MAX_ITEMS - 1 because you wouldn't want
// to swap last element with the next because there
// is none behind it
if (items[j + 1] == " ")
// if the next item is already empty you don't need to shift any more
return true;

// swap the next item with much more
// efficient std::swap function
std::swap(items[j], items[j + 1]);
}
return true; // value is removed and items shifted so you can now return
}
}
// if function gets to this point that means the value wasn't found
return false;
}

如果你想让你的代码更有效率,我可以给你更多关于如何去做的建议。以上应该可以回答您的问题。

像这样对结构使用数组也不是最佳选择。使用链接列表意味着不需要转移。

编辑:用代码示例替换长文本Edit2:如果不再需要移动则添加 return 语句

关于c++ - 删除和移动数组 C++ 中的剩余元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43554685/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com