"cout<<count<<endl;"没有打印任何内容
"cout<<count<<endl;" isn't printing anything
cout<<count<<endl;
sould 根据条件提供输出,但它没有打印任何内容,导致此类结果的代码中的错误/错误/缺陷是什么? 这是我的第一个问题,对不起,如果我不完全理解。
我使用了下面的代码,我无法理解这里发生了什么,这是一个简单的输入输出问题。 输出为我们提供了有关匹配两队制服的信息。
#include <stdio.h>
#include <iostream>
using namespace std;
main(){
int a;
cin>>a;
int **b;
b=new int*[a];
for (int i = 0; i < a; i++)
{
b[i]=new int[2];
for (int j = 0; j <2 ; j++)
{
cin>>b[i][j];
}
}
int count=0;
for (int i = 0; i < a*(a-1); i++)
{ for (int j = 0; j < a; j++)
if (b[i][0]==b[j][1])
count=count+1;
}
cout<<count<<endl;
for (size_t i = 0; i < a; i++)
{
delete b[i];
}
delete b;
}
输入:
3
1 2
2 4
3 4
输出不包含任何内容
您在delete
应该delete[]
.代码注释:
#include <iostream> // use the correct headers
#include <cstddef>
// not recommended: https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
using namespace std;
int main() { // main must return int
size_t a; // better type for an array size
cin >> a;
int** b;
b = new int*[a];
for(size_t i = 0; i < a; i++) {
b[i] = new int[2];
for(size_t j = 0; j < 2; j++) {
cin >> b[i][j];
}
}
int count = 0;
std::cout << a * (a - 1) << "n"; // will print 6 for the given input
for(size_t i = 0; i < a * (a - 1); i++) {
// i will be in the range [0, 5]
for(size_t j = 0; j < a; j++)
if(b[i][0] == b[j][1]) count = count + 1;
// ^ undefined behaviour
}
cout << count << endl;
for(size_t i = 0; i < a; i++) {
delete[] b[i]; // use delete[] when you've used new[]
}
delete[] b; // use delete[] when you've used new[]
}
相关文章:
- 如何循环打印顶点结构
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 如何在c++中打印目录
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 在线编译器中的分段C++没有打印消息
- 呼叫运营商<<临时
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 如何将结构插入到集合中并打印集合的成员
- 在循环C++中指定字符串之后,不会打印该字符串
- 以螺旋方式打印矩阵的程序.(工作不好)
- 从控制台中删除最后打印的元素
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 如何仅使用对象名称打印特定于对象的成员
- 过载输出<<用于类的运算符,以打印其中的元组
- 重载operator< & lt;作为会员打印
- 重载& lt; & lt;用于打印自定义异常的操作符
- 如何显示/打印字符串对象?cout & lt; & lt;Int工作,count <<字符串
- 什么会导致死亡<<变量& lt; & lt;“ t";当输出超过终端宽度时,打印空白而不是值