• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            為生存而奔跑

               :: 首頁 :: 聯系 :: 聚合  :: 管理
              271 Posts :: 0 Stories :: 58 Comments :: 0 Trackbacks

            留言簿(5)

            我參與的團隊

            搜索

            •  

            積分與排名

            • 積分 - 328615
            • 排名 - 74

            最新評論

            閱讀排行榜

            評論排行榜

            百度一個筆試題
            #include<iostream>
            using namespace std;

            struct complex_t
            {
                
            int real;
                
            int imag;
            }
            ;

            int create(complex_t *p, unsigned int n)
            {
                p 
            = new complex_t[n];
                
            if(p == NULL)
                    
            return -1;
                
            else return 0;
            }


            int main()
            {
                complex_t 
            *comps = NULL;
                unsigned 
            int num = 0;
                cin
            >>num;
                
            if(create(comps,num) < 0)
                
            {
                    printf(
            "create failed\n");
                    
            return -1;
                }


                
            /*if(comps == NULL)
                {
                    cout<<"comps is NULL\n";
                    return -1;
                }
            */


                
            //comps = new complex_t[num];

                
            long long int sum = 0;
                unsigned 
            int pos = 0;
                cin
            >>pos;
                
            while(pos<num)
                
            {
                    cin
            >>comps[pos].real>>comps[pos].imag;
                    cin
            >>comps[pos+1].real>>comps[pos+1].imag;
                    sum 
            += comps[pos].real*comps[pos+1].real + comps[pos].imag*comps[pos+1].imag;
                    pos
            +=2;
                }

                cout
            <<"sum is "<<sum<<endl;
                
            return 0;
            }

            很容易被忽悠的一個地方是,create函數的第一個參數,類型時complex_t * p。 然后,在create里面給p分配了一塊存儲空間。 乍一看,因為是指針做參數,所以會傳遞回去。其實不然。在這里,相當于,首先,main函數中調用create函數時,把comps賦值給p。即指針p指向與comps相同的一段存儲空間。  但是,在create里面,p=new complex_t[n],使得p又指向了一塊新的存儲空間。而此時,comps還是指向原來的存儲空間。所以,在create里面對p做的更改對comps并沒有影響。

            一個解決方法是使用指向指針的指針。如下
            #include<iostream>
            using namespace std;

            struct complex_t
            {
                
            int real;
                
            int imag;
            }
            ;

            int create(complex_t **p, unsigned int n)
            {
                
            *= new complex_t[n];
                
            if(p == NULL)
                    
            return -1;
                
            else return 0;
            }


            int main()
            {
                complex_t 
            *comps = NULL;
                unsigned 
            int num = 0;
                cin
            >>num;
                
            if(create(&comps,num) < 0)
                
            {
                    printf(
            "create failed\n");
                    
            return -1;
                }


                
            /*if(comps == NULL)
                {
                    cout<<"comps is NULL\n";
                    return -1;
                }
            */


                
            //comps = new complex_t[num];

                
            long long int sum = 0;
                unsigned 
            int pos = 0;
                cin
            >>pos;
                
            while(pos<num)
                
            {
                    cin
            >>comps[pos].real>>comps[pos].imag;
                    cin
            >>comps[pos+1].real>>comps[pos+1].imag;
                    sum 
            += comps[pos].real*comps[pos+1].real + comps[pos].imag*comps[pos+1].imag;
                    pos
            +=2;
                }

                cout
            <<"sum is "<<sum<<endl;
                
            return 0;
            }

            另外一種方法是,在main函數中申請空間,而不是在create函數中。
            看下面的例子
            bool isDigit(char ch)
            {
                
            return ch>='0'&&ch<='9';
            }


            int maxContinueNum(const char * inputstr, char * outputstr)
            {
                
            int i,j;
                
            int maxlen=0;
                
            int start;
                i
            =0;
                
            while(inputstr[i]!='\0')
                
            {
                    
            if(isDigit(inputstr[i]))
                    
            {
                        j
            =i+1;
                        
            while(inputstr[j]!='\0' && isDigit(inputstr[j]))
                            j
            ++;
                        
            if(j-i>maxlen)
                        
            {
                            maxlen
            =j-i;
                            start
            =i;
                        }

                        i
            =j;
                    }

                    
            else i++;
                }

                
            for(i=0;i<maxlen;i++)
                    outputstr[i]
            =inputstr[i+start];
                outputstr[i]
            ='\0';
                
            return maxlen;
            }

            int main()
            {
                
            char input[]={"abcd12345ed125ss123456789"};
                
            char *output = new char[100];
                cout
            << maxContinueNum(input, output) << endl;
                
                cout
            <<output<<endl;
            }

            outputstr在main函數中申請內存,在maxContinueNum函數中更改其中的值。

            posted on 2011-05-04 23:43 baby-fly 閱讀(654) 評論(0)  編輯 收藏 引用 所屬分類: Effective STL / C++
            久久黄视频| 国产午夜福利精品久久2021| 久久精品国产清自在天天线| 精品久久综合1区2区3区激情| 久久99国产精品久久99小说| 99热成人精品热久久669| 久久免费视频6| 2021精品国产综合久久| 色天使久久综合网天天| 99久久超碰中文字幕伊人 | 亚洲av日韩精品久久久久久a| 九九精品99久久久香蕉| 亚洲成av人片不卡无码久久| 久久99热国产这有精品| 综合网日日天干夜夜久久 | 久久综合给合综合久久| 国产精品18久久久久久vr| 日本五月天婷久久网站| 国产精品VIDEOSSEX久久发布| 久久久久人妻一区精品色| 久久久久久免费视频| 久久人人超碰精品CAOPOREN| 91精品国产91久久综合| 色综合久久综合中文综合网 | 久久99久久99小草精品免视看| 日韩av无码久久精品免费| 一本一本久久a久久精品综合麻豆| 国产成人精品久久亚洲高清不卡 国产成人精品久久亚洲高清不卡 国产成人精品久久亚洲 | 久久久久国产精品嫩草影院| 2021少妇久久久久久久久久| 久久久久无码精品国产不卡| 色欲av伊人久久大香线蕉影院 | 亚洲精品蜜桃久久久久久| 午夜视频久久久久一区| 日韩亚洲国产综合久久久| 欧美午夜A∨大片久久 | 一本色道久久综合亚洲精品| 婷婷久久五月天| 99久久国产精品免费一区二区| 久久久久亚洲av综合波多野结衣| 久久午夜免费视频|