动态数组大小并在 getline() 处崩溃;
Dynamic Array Size and crashing at getline();
我最近一直在开发一个程序,它将名称作为输入并最终对它们进行排序和二进制搜索。然而,在尝试使数组成为动态大小(每次循环迭代都会增加一个)时,它 运行 陷入各种问题。
我可以制作由 20 个元素组成的字符串数组并且程序可以运行,但我的作业的额外功劳是使其成为动态大小。目前,程序在达到 "getline(cin, Names[x]);" 时崩溃,没有任何错误代码。 我一直在四处寻找,我知道在这种情况下做一个向量而不是一个数组会更容易,但是我不相信我可以在这个作业中使用向量。
谢谢
原码
using namespace std;
#include <iostream>
#include <string>
void main()
{
int x = 0;
string * Names = new string[x];
bool NameInputEnd(0);
cout << " Enter your names to be sorted\n";
cout << "To exit just press [Enter] at any time\n";
do
{
cout << x << endl;
cout << "\n< Name " << (x + 1) << " > = ";
!!**CRASHES HERE**!!
getline(cin, Names[x]);
if (Names[x].empty() || x == 19)
{
cout << "\nFinal Name Amount = " << (x + 1) << endl << endl;
NameInputEnd = 1;
continue;
}
x++;
} while (NameInputEnd == 0);
delete [] Names;
}
变化
int tempsize(1), x(0);
string * Names = new string[tempsize];
...
do
{
...
x++;
tempsize++;
}while (NameInputEnd == 0);
数组一旦创建就无法调整大小。您必须销毁它并使用现有数据的副本创建一个新数组。例如:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
void main()
{
int x = 0;
int capacity = 20;
string * Names = new string[capacity];
string Name;
cout << " Enter your names to be sorted\n";
cout << "To exit just press [Enter] at any time\n";
do
{
cout << x << endl;
cout << "\n< Name " << (x + 1) << " > = ";
if ((!getline(cin, Name)) || Name.empty())
break;
if (x == capacity)
{
int newCapacity = capacity + 20;
string *newNames = new string[newCapacity];
copy(Names, Names + x, newNames);
delete [] Names;
Names = newNames;
capacity = newCapacity;
}
Names[x] = Name;
++x;
}
while (true);
cout << "\nFinal Name Amount = " << x << endl << endl;
delete [] Names;
}
你真的应该使用 std::vector
,不过:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void main()
{
vector<string> Names;
string Name;
Names.reserve(20); // optional
cout << " Enter your names to be sorted\n";
cout << "To exit just press [Enter] at any time\n";
do
{
cout << Names.size() << endl;
cout << "\n< Name " << (Names.size() + 1) << " > = ";
if ((!getline(cin, Name)) || Name.empty())
break;
Names.push_back(Name);
}
while (true);
cout << "\nFinal Name Amount = " << Names.size() << endl << endl;
}
我最近一直在开发一个程序,它将名称作为输入并最终对它们进行排序和二进制搜索。然而,在尝试使数组成为动态大小(每次循环迭代都会增加一个)时,它 运行 陷入各种问题。
我可以制作由 20 个元素组成的字符串数组并且程序可以运行,但我的作业的额外功劳是使其成为动态大小。目前,程序在达到 "getline(cin, Names[x]);" 时崩溃,没有任何错误代码。 我一直在四处寻找,我知道在这种情况下做一个向量而不是一个数组会更容易,但是我不相信我可以在这个作业中使用向量。
谢谢
原码
using namespace std;
#include <iostream>
#include <string>
void main()
{
int x = 0;
string * Names = new string[x];
bool NameInputEnd(0);
cout << " Enter your names to be sorted\n";
cout << "To exit just press [Enter] at any time\n";
do
{
cout << x << endl;
cout << "\n< Name " << (x + 1) << " > = ";
!!**CRASHES HERE**!!
getline(cin, Names[x]);
if (Names[x].empty() || x == 19)
{
cout << "\nFinal Name Amount = " << (x + 1) << endl << endl;
NameInputEnd = 1;
continue;
}
x++;
} while (NameInputEnd == 0);
delete [] Names;
}
变化
int tempsize(1), x(0);
string * Names = new string[tempsize];
...
do
{
...
x++;
tempsize++;
}while (NameInputEnd == 0);
数组一旦创建就无法调整大小。您必须销毁它并使用现有数据的副本创建一个新数组。例如:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
void main()
{
int x = 0;
int capacity = 20;
string * Names = new string[capacity];
string Name;
cout << " Enter your names to be sorted\n";
cout << "To exit just press [Enter] at any time\n";
do
{
cout << x << endl;
cout << "\n< Name " << (x + 1) << " > = ";
if ((!getline(cin, Name)) || Name.empty())
break;
if (x == capacity)
{
int newCapacity = capacity + 20;
string *newNames = new string[newCapacity];
copy(Names, Names + x, newNames);
delete [] Names;
Names = newNames;
capacity = newCapacity;
}
Names[x] = Name;
++x;
}
while (true);
cout << "\nFinal Name Amount = " << x << endl << endl;
delete [] Names;
}
你真的应该使用 std::vector
,不过:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void main()
{
vector<string> Names;
string Name;
Names.reserve(20); // optional
cout << " Enter your names to be sorted\n";
cout << "To exit just press [Enter] at any time\n";
do
{
cout << Names.size() << endl;
cout << "\n< Name " << (Names.size() + 1) << " > = ";
if ((!getline(cin, Name)) || Name.empty())
break;
Names.push_back(Name);
}
while (true);
cout << "\nFinal Name Amount = " << Names.size() << endl << endl;
}