35题:C
#include<iostream> #include<vector> #include<string> #include<algorithm> std::string foo(const std::string& s) { std::string ret; ret.reserve(s.size()); for(size_ti=0;i<s.size();++i) { ret.push_back(s[i]|0x20); } return ret; } bool compare_string(const std::string& lhs,const std::string &rhs) { returnfoo(lhs)<foo(rhs); } int main(int argc,constchar* argv[]) { constchar* data[]={"Have","fun","in","Trendmicro"}; std::vector<std::string>vec(data,data+sizeof(data)/sizeof(data[0])); std::sort(vec.begin(),vec.end(),compare_string); std::copy(vec.begin(),vec.end(),std::ostream_iterator<std::string>(std::cout," ")); std::cout<<std::endl; return 0; }34:BDE(下面是改动后的代码)
#include<stdio.h> #include<exception> #include<stdlib.h> template<unsigned int size> class CTestObj { private: int *m_pvalue; public: CTestObj(int arr[size]) { m_pvalue=new int; *m_pvalue=0; for(unsigned int i=0;i<size;i++) { *m_pvalue+=arr[i]; } } ~CTestObj() { delete m_pvalue; } voidTest() { if(*m_pvalue>100) { throw std::exception(); } } }; int main(int argc,const char* argv[]) { try { int arr[]={1,2,3,4,500}; CTestObj<_countof(arr)> *pobj =new CTestObj<_countof(arr)>(arr); pobj->Test(); delete pobj; } catch(std::exception) { printf("exception "); // delete pobj; } printf("end "); return 0; }9:A(供參考)
#include<stdio.h> #include<iostream> usingnamespace std; #define BUFFER_SIZE 12 /*void Copydata(const char *imgdata,int width,int height) { charbuf[BUFFER_SIZE]; if(width*height>BUFFER_SIZE) return; const char*p=imgdata; for(inti=0;i<width;++i){ for(intj=0;j<height;++j){ buf[i*width+j]=*p++; } } cout<<buf<<endl; } void Copydata(const char *data,int len) { if(len>BUFFER_SIZE) return; charbuf[BUFFER_SIZE]; memcpy(buf,data,len); cout<<buf<<endl; }*/ void Copydata(const char *imgdata,int width,int height) { char *buf=(char *)malloc(width*height); if(NULL==buf)return; memcpy(buf,imgdata,width*height); cout<<buf<<endl; free(buf); } int main(int argc,const char* argv[]) { char *p="abcdefghijkl"; Copydata(p,3,4); return 0; }8:BC
#include<stdio.h> #include<iostream> usingnamespace std; int main(int argc,constchar* argv[]) { intarr[3][3]={{1,2,3},{4,5,6},{7,8,9}}; inti=1,j=2; cout<<**(arr+3*i+j)<<endl; cout<<*(*(arr+i)+j)<<endl; cout<<*(arr[i]+j)<<endl; cout<<*(arr+3*i+j)<<endl; return 0; }
7:A
#include<stdio.h> #include<iostream> using namespace std; void check() { unsigned int i=1; // char *cptr =(char *)i; char *cptr= (char *)&i; // char*cptr=&(char *)i; // char cptr =(char)&i; if(*cptr!=0) printf("1"); else printf("0"); } int main(int argc,const char* argv[]) { check(); return 0; }
6:BC
#include <stdio.h> #include <iostream> using namespace std; void f(char *s,char *t) { do{ *s++=*t++; }while(*t!='