C与C++中非常少犯的错误,犯了后却非常难找出的错误

1.continue,break类的错误HDU1877快哭了微笑

#include<iostream>
using namespace std;
int main()
{
    int a,b,m,sum;
    while(cin>>m,m)
    {
        int p[100]={0};
        cin>>a>>b;
        sum=a+b;
        if(!sum)//是0的话就不用再进行考虑了
        {
            cout<<0<<endl;
            continue;//闲着没事,做个简单题,却由于忘了   continue     而   Presentation Error    了非常多次 
        }
        int i=0;
        while(sum)
        {
            p[i]=sum%m;
            sum/=m;
            i++;
        }
        for(int j=i-1;j>=0;j--)
            cout<<p[j];
        cout<<endl;
    }
}

2.memset函数将数组初始化为1(from 百度文库难过大笑)

例如以下demo是能够的,能把数组中的元素值都设置成字符1,
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
    char a[5];
    memset(a,'1',5);
    for(int i=0;i<5;i++)
        cout<<a[i]<<"";
    system("pause");
    return 0;
}


而,例如以下程序想把数组中的元素值设置成1,却是不可行的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <cstring>
#include <windows.h>
using namespace std;
int main()
{
    int a[5];
    memset(a,1,20);
 
   
                    //也等价于memset(a,1,sizeof(a));.
    for(int i=0;i<5;i++)
        cout<<a[i]<<"";
    system("pause");
    return 0;
}


问题是:
1.第一个程序为什么能够,而第二个不行?
由于第一个程序的数组a是字符型的,字符型占领内存大小是1Byte,而memset函数也是以字节为单位进行赋值的。所以你输出没有问题。而第二个程序a是整型的。使用 memset还是按字节赋值,这样赋值完以后,每一个数组元素的值实际上是0x01010101即十进制的16843009。

假设用memset(a,1,20)。就是对a指向的内存的20个字节进行赋值。每一个都用数1去填充,转为二进制后,1就是00000001。占一个字节。一个int元素是4字节,合一起是0000 0001,0000 0001,0000 0001,0000 0001。转化成十六进制就是0x01010101。就等于16843009。就完毕了对一个int元素的赋值了。
3.while((c=cin.get())!='#')
本来想举个简单的样例。可是被这个坑得太慘了。所以。

。记住不是==,而是=。

c=getchar()一样。


HDOJ 2072

#include<iostream>  
#include<set>  
#include<string>  
using namespace std;  
int main()  
{  
    set<string> st;  
    string str;  
    char c;  
    str.clear();  
    while((c=cin.get())!='#')  
    {  
        while(c!=' '&&c!='
')  
        {  
            str+=c;  
            c=cin.get();  
        }  
        if(str.length())  
        {  
            st.insert(str);  
            str.clear();  
        }  
        if(c=='
')  
        {  
            cout<<st.size()<<endl;  
            st.clear();  
            str.clear();  
        }  
    }  
    return 0;  
} <span style="font-size:18px;"> 
</span>

4.c++程序中。假设用#include<iostream>和cin的话。就避免不了使用名字空间。using namespace std;。。。我竟
然有一次删掉部分原来的代码又一次敲代码的时候,忘掉了写上了,结果浪费了好长时间(夹杂其它事),记住了这个教训。不止一次了.

5.已经定义了全局变量,还傻冒似的又定义了局部变量,结果覆盖了全局变量的作用,还不报错,非常少犯啊。,。非常难找出来啊,,,

6.scanf("%d%d%d",&t1,&t2,&t3,&t4);     少个%d居然不报错,居然少了,这就是依照曾经代码改动的坏处吧。改不好就呜呜了,加强能力

7.数学公式运算

pow运用于整数型会出错。由于除以会取整,要用浮点型才可能得到正确结果。

pow(10.0,num++);

8.关于数据范围的错误,so能尽量打就尽量大吧

#include<cstdio>
using namespace std;
long long a[100005]={0};
int main()
{
	for(long long i=1;i<100002;i++)
	{
		if(i%3==0)
		a[i]=a[i-1]+i*i*i;//i的3次方
		else
		a[i]=a[i-1]+i;
	}
	int n;
	while(scanf("%d",&n)&&n>=0)
	{
		printf("%lld
",a[n]);
	}
	return 0;
}


9.在for循环中使用strlen()函数,假设循环次数过多,就会导致超时,所以还是回归原始可靠的用变量定义吧。

採用

int len = strlen(str);
for(int i = 0;i < len;i++)
{
    
}
pass掉

for(int i = 0;i < strlen(str);i++)
{
    
}


原文地址:https://www.cnblogs.com/yutingliuyl/p/7306375.html