程序中的 C++ 向量 std::bad_alloc 错误
C++ vector std::bad_alloc error in a program
我试图根据一个值对具有两个值的用户定义数据类型的向量进行排序。但是我得到 bad_alloc 的错误。这是代码:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct s{
int value;
int weight;
};
bool comp(s a , s b){
return a.value>b.value;
}
int main(){
vector <s> p;
s temp;
int n;
cin>>n;
while(n>=1){
cin>>temp.value;
cin>>temp.weight;
p.push_back(temp);
}
sort(p.begin(), p.end(), comp);
vector <s> :: iterator it;
for(it = p.begin(); it != p.end();it++){
*it = temp;
cout<<temp.value<<endl;
}
}
在 运行 上:
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
有人能帮忙吗?
如果用户为 n
输入 1 或更高的值,循环将永远不会结束,填充向量直到用完所有可用内存。您没有在每次循环迭代中递减 n
,因此循环最终会中断:
while (n >= 1) {
cin >> temp.value;
cin >> temp.weight;
p.push_back(temp);
--n; // <-- add this
}
在这种情况下,for
循环比 while
循环更合适:
for (int i = 0; i < n; ++i) {
cin >> temp.value;
cin >> temp.weight;
p.push_back(temp);
}
我会通过为 struct s
定义自定义 operator>>
然后将 std::copy_n()
与 std::istream_iterator
和 [= 一起使用来完全摆脱手动循环23=]:
#include <iostream>
#include <algorithm>
#include <iterator>
istream& operator>>(istream &in, s &out) {
in >> out.value;
in >> out.weight;
return in;
}
int main() {
...
int n;
cin >> n;
copy_n(istream_iterator<s>(cin), n, back_inserter(p));
...
}
您填充向量的任何方式,您的 comp
函数都应通过引用获取其输入参数:
bool comp(s &a, s &b) {
return a.value > b.value;
}
此外,您的输出循环没有正确使用迭代器。您需要摆脱 *it =
分配并按原样输出引用值:
for(it = p.begin(); it != p.end(); ++it) {
//*it = temp; // <- get rid of this
cout << it->value << endl;
}
我看到的问题:
无限循环
循环中
while ( n >= 1)
{
...
}
您没有更改 n
的值。如果 n
比循环开始时的 1
大,则循环将永远不会结束。
不检查输入状态
你有
cin >> temp.value;
cin >> temp.weight;
您没有检查这些调用是否成功。您假设他们这样做并继续使用 temp
.
赋值方式错误
在最后一个循环中,您正在使用
*it = temp;
这将更改 vector
,而不是从向量中提取值。
这是 main
的更新版本,应该可以使用。
int main()
{
vector <s> p;
s temp;
int n;
cin>>n;
while(n>=1)
{
// If there is a problem reading, break.
if ( !(cin>>temp.value) )
{
break;
}
// If there is a problem reading, break.
if ( !(cin>>temp.weight) )
{
break;
}
p.push_back(temp);
// Decrement n
--n;
}
sort(p.begin(), p.end(), comp);
vector <s> :: iterator it;
for(it = p.begin(); it != p.end();it++)
{
// Extract the value from the vector and print it.
temp = *it;
cout<<temp.value<<endl;
}
}
我试图根据一个值对具有两个值的用户定义数据类型的向量进行排序。但是我得到 bad_alloc 的错误。这是代码:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct s{
int value;
int weight;
};
bool comp(s a , s b){
return a.value>b.value;
}
int main(){
vector <s> p;
s temp;
int n;
cin>>n;
while(n>=1){
cin>>temp.value;
cin>>temp.weight;
p.push_back(temp);
}
sort(p.begin(), p.end(), comp);
vector <s> :: iterator it;
for(it = p.begin(); it != p.end();it++){
*it = temp;
cout<<temp.value<<endl;
}
}
在 运行 上:
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
有人能帮忙吗?
如果用户为 n
输入 1 或更高的值,循环将永远不会结束,填充向量直到用完所有可用内存。您没有在每次循环迭代中递减 n
,因此循环最终会中断:
while (n >= 1) {
cin >> temp.value;
cin >> temp.weight;
p.push_back(temp);
--n; // <-- add this
}
在这种情况下,for
循环比 while
循环更合适:
for (int i = 0; i < n; ++i) {
cin >> temp.value;
cin >> temp.weight;
p.push_back(temp);
}
我会通过为 struct s
定义自定义 operator>>
然后将 std::copy_n()
与 std::istream_iterator
和 [= 一起使用来完全摆脱手动循环23=]:
#include <iostream>
#include <algorithm>
#include <iterator>
istream& operator>>(istream &in, s &out) {
in >> out.value;
in >> out.weight;
return in;
}
int main() {
...
int n;
cin >> n;
copy_n(istream_iterator<s>(cin), n, back_inserter(p));
...
}
您填充向量的任何方式,您的 comp
函数都应通过引用获取其输入参数:
bool comp(s &a, s &b) {
return a.value > b.value;
}
此外,您的输出循环没有正确使用迭代器。您需要摆脱 *it =
分配并按原样输出引用值:
for(it = p.begin(); it != p.end(); ++it) {
//*it = temp; // <- get rid of this
cout << it->value << endl;
}
我看到的问题:
无限循环
循环中
while ( n >= 1)
{
...
}
您没有更改 n
的值。如果 n
比循环开始时的 1
大,则循环将永远不会结束。
不检查输入状态
你有
cin >> temp.value;
cin >> temp.weight;
您没有检查这些调用是否成功。您假设他们这样做并继续使用 temp
.
赋值方式错误
在最后一个循环中,您正在使用
*it = temp;
这将更改 vector
,而不是从向量中提取值。
这是 main
的更新版本,应该可以使用。
int main()
{
vector <s> p;
s temp;
int n;
cin>>n;
while(n>=1)
{
// If there is a problem reading, break.
if ( !(cin>>temp.value) )
{
break;
}
// If there is a problem reading, break.
if ( !(cin>>temp.weight) )
{
break;
}
p.push_back(temp);
// Decrement n
--n;
}
sort(p.begin(), p.end(), comp);
vector <s> :: iterator it;
for(it = p.begin(); it != p.end();it++)
{
// Extract the value from the vector and print it.
temp = *it;
cout<<temp.value<<endl;
}
}