gpt4 book ai didi

c++ - 动态分配字符串数组

转载 作者:搜寻专家 更新时间:2023-10-31 00:30:57 24 4
gpt4 key购买 nike

这就是我目前所拥有的。我正在尝试在 C++ 中编辑动态分配的数组,但是,当 for 循环运行时,它会跳过第一项。我需要它来获得所有元素。任何帮助表示赞赏。提前致谢。

#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
using namespace std;


int main()
{
// Declare Variables
int userChoice = 0;
int numItems = 0;

cout << "How many items will be on your list? ";
cin >> numItems;

string *list = new string[numItems];

// Give the user some options
cout << "1. Add Item" << endl;
cout << "2. Remove Item" << endl;
cout << "3. Sort Items" << endl;
cout << "4. Exit" << endl;
cout << "Enter the number of the operation you wish to perform: ";
cin >> userChoice;
cout << endl;

// Perform the operation
switch(userChoice)
{
case 1:
{
cin.clear(); // Remove new line from cin

for(int i = 0; i < numItems; i++)
{
cout << "Item #" << i + 1 << " --> ";
getline(cin, list[i]);
}
}
break;
case 2:
{

}
break;
case 3:
{

}
break;
case 4:
{
return 0;
}
default:
{
cout << "Error! Invalid Selection" << endl;
}

}

// Output the list
cout << "-------Items-------" << endl
<< *list << endl << endl;

// free memory
delete [] list;

cout << "Enter the number of the operation you wish to perform: ";
cin >> userChoice;


return 0;
}

最佳答案

对我来说,将 STL 用于某些事物(如字符串)然后使用标准数组来保存字符串似乎很奇怪。此外,list 是 STL 中的标准对象类型,因此对于变量来说这不是一个好名字。修改后的代码修复了忽略第一行的问题,还使用了 vector 而不是 newdelete

#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
#include <vector>
#include <iterator>
using namespace std;

int main()
{
// Declare Variables
int userChoice = 0;
int numItems = 0;

cout << "How many items will be on your myList? ";
cin >> numItems;
cin.ignore ();

vector<string> myList;

while (true)
{
// Give the user some options
cout << "1. Add Item" << endl;
cout << "2. Remove Item" << endl;
cout << "3. Sort Items" << endl;
cout << "4. Exit" << endl;
cout << "Enter the number of the operation you wish to perform: ";
cin >> userChoice;
cin.ignore ();
cout << endl;

// Perform the operation
switch(userChoice)
{
case 1:
{
for(int i = 0; i < numItems; i++)
{
cout << "Item #" << i + 1 << " --> ";
string s;
getline(cin, s);
myList.push_back (s);
}
}
break;
case 2:
{

}
break;
case 3:
{
sort (myList.begin (), myList.end ());
}
break;
case 4:
{
return 0;
}
default:
{
cout << "Error! Invalid Selection" << endl;
}

}

// Output the myList
cout << "-------Items-------" << endl;
copy(myList.begin(), myList.end(), ostream_iterator<string>(cout, "\n"));

} // end of while
}

The project description explicitly says I can't use standard containers, sort functions, or smart pointers

重做下面不使用那些东西。 :)

#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
using namespace std;

int myCompare (const void * a, const void * b)
{

if ((*(string *) a) < *(string *) b) return -1;
if ((*(string *) a) > *(string *) b) return 1;
return 0;
}

int main()
{
// Declare Variables
int userChoice = 0;
int numItems = 0;

cout << "How many items will be on your myList? ";
cin >> numItems;
cin.ignore ();

string *myList = new string[numItems];

while (true)
{
// Give the user some options
cout << "1. Add Item" << endl;
cout << "2. Remove Item" << endl;
cout << "3. Sort Items" << endl;
cout << "4. Exit" << endl;
cout << "Enter the number of the operation you wish to perform: ";
cin >> userChoice;
cin.ignore ();
cout << endl;

// Perform the operation
switch(userChoice)
{
case 1:
{
for(int i = 0; i < numItems; i++)
{
cout << "Item #" << i + 1 << " --> ";
getline(cin, myList [i]);
}
}
break;
case 2:
{

}
break;
case 3:
{
qsort (myList, numItems, sizeof (string *), myCompare);
}
break;
case 4:
{
delete [] myList;
return 0;
}
default:
{
cout << "Error! Invalid Selection" << endl;
}

}

// Output the myList
cout << "-------Items-------" << endl;
for(int i = 0; i < numItems; i++)
cout << myList [i] << endl;

} // end of while
}

关于c++ - 动态分配字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35236353/

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