C++小題(九)

12045 ワード

/*
           ()
int main(int argc, char *argv[])
{
    char *s = "abcdefg";
    s += 2;
    fprintf(stderr, "%d
", s); return 0; } : C : C ( ) cde "c" "c" stdout -- 。 stderr -- 。 , 。stdout ,stderr 。 */ ////////////////////////////////////////////////////////////////// /* , ? : B : B ( ) A memcpy() B memmove() C memset() D strcpy() memmove src count dest, , memmove 。 src 。 memcpy 。 */ ////////////////////////////////////////////////////////////////// /* : A : A ( ) A B C++ C , D 、 , , , , ; , ; , , , , */ ////////////////////////////////////////////////////////////////// /* typedef int * T; T a[10]; a ? : B : B ( ) int ( * a ) [ 10 ] ; int * a [ 10 ] ; int * a ; int a [ 10 ] ; typedef , int * T , T a[ 10 ]; int * a[10]; B 。 */ ////////////////////////////////////////////////////////////////// /* : : A : A ( ) struct A{A _a}; struct A{A* _a;}; struct A{A& _a;}; struct B;struct A{B& _b;};struct B{A& _a;}; */ ////////////////////////////////////////////////////////////////// /* Which of the following calling convention(s) support(s) support variable-length parameter(e.g. printf)? : A : B ( ) cdecl stdcall pascal fastcall (calling convention) , , 。 , __cdecl,__fastcall __stdcall ,C++ thiscall , C/C++ naked call 。 cdecl /Gd。__cdecl C/C++ , C++ __stdcall __fastcall __cdecl , C , , , ( ) , __cdecl __stdcall , __cdecl , , printf windows API wsprintf __cdecl 。 */ ////////////////////////////////////////////////////////////////// /* : #define SIZE_20M (20*1024*1024) void func_a() { char *temp = malloc(SIZE_20M) return; } void func_b() { char temp[SIZE_20M]; //...do something using temp return; } , : B : B ( ) func_a 。 func_b , 。 func_b func_a func_b 0 A func_a() ,func_b() temp , , func_b() C func_b() , D 0, static 0 B 2M, */ ////////////////////////////////////////////////////////////////// /* double(8 ) ()。 : A B C 2 10 30 0.1 0.5 100 A 8 64 , , 1 ,11 ,52 B(2^90 < B < 2^100) , , ? B = 2^30*5^30 B , 30 0, 0 0 , 90-30 = 60 , 52 , , B C , C 10[0.1] = 2[0.00011001100110011.......], , 0.1 double */ ////////////////////////////////////////////////////////////////// /* C++ C struct , ? : A B C D A C++ , class private , struct public B class private ,struct public C c struct ,struct D C++ struct c , */ ////////////////////////////////////////////////////////////////// /* C++/JAVA static ? : C : C ( ) A static B static C static D static static static , , static , static static static */ ////////////////////////////////////////////////////////////////// /* c++ , const int i = 0; int *j = (int *) &i; *j = 1; printf("%d,%d", i, *j) ? : A A 0,1 B 1,1 C 1,0 D 0,0 const ,c++ , i , printf i 0。 , i , hack i printf 。 i volatile const int , i ,printf 1。 */ ////////////////////////////////////////////////////////////////// /* Which of following C++ code is correct? : C int f() { int *a = new int(3); return *a; } int *f() { int a[3] = {1, 2, 3}; return a; } vector<int> f() {vector<int> v(3); return v; } void f(int *ret) { int a[3] = {1, 2, 3}; ret = a; return; } C , , 。 A , delete,B , ,D B 。 */ ////////////////////////////////////////////////////////////////// /* int main(void) { vector<int>array; array.push_back(100); array.push_back(300); array.push_back(300); array.push_back(500); vector<int>::iterator itor; for (itor = array.begin(); itor != array.end(); itor++) { if (*itor == 300) { itor = array.erase(itor); } } for (itor = array.begin(); itor != array.end(); itor++) { cout << *itor << " "; } return 0; } : B 100 300 300 500 100 300 500 100 500 , array , , itor++ , 。 “300” 。 */ ////////////////////////////////////////////////////////////////// /* ? main() { char str[]="S\065AB"; printf("
%d", sizeof(str)); } : C 7 6 5 error \ddd 8 , 4 + '\0' 5 */ ////////////////////////////////////////////////////////////////// /* #include <stdio.h> #include <stdio.h> void fun( char *s ) { char a[10]; strcpy ( a, "STRING" ); s = a ; } main( ) { char *p= "PROGRAM" ; fun( p ); printf ( "%s
", p) ; } ( □ )? : D : D ( ) STRING STRING□□□□ STRING□□□ PROGRAM ,p fun ,p , D 。 fun() , ,s=a s ( fun s, STRING), p , 。 */ ////////////////////////////////////////////////////////////////// /* “ ” ? : B C D : B C D ( ) A B C , , ; , , , ; D , , 。 , 。 (1) 。 , , ( ) 。 (2) , , ; , , , ; , 。 , , 。 (3) , , , "* " , ; , , 。 , 。 */ ////////////////////////////////////////////////////////////////// /* : void test(int a){} void test(float a){} : : D test(1); test(‘c’); test(2+’d’) test(0.5) 0.5 double , int float , 0.5f */ ////////////////////////////////////////////////////////////////// /* : : B 1.234e04 1.234e0.4 1.234e+4 1.234e0 e , e n , */ ////////////////////////////////////////////////////////////////// /* C++ , new ( , ), () : B , , , , 。 , 。 , , ( ) , ( new) 。 */ ////////////////////////////////////////////////////////////////// /* #include <stdio.h> main() { FILE *fp; int i,a[ 6]={1,2,3,4,5,6},k; fp = fopen ("data.dat", "w+b"); for (i=0;i<6;i+ +) { fseek(fp,0L,0); fwrite(&a[5—i],sizeof(int),1,fp); } rewind(fp); fread(&k,sizeof(int),1,fp); fclose(fp); printf("%d",k); } ? : B 6 1 123456 21 fseek , 。rewind() , 1。 */ ////////////////////////////////////////////////////////////////// /* int p[][4]={{1},{3,2},{4,5,6},{0}}; ,p[1][2] () : B : B ( ) 1 0 6 2 0 */ ////////////////////////////////////////////////////////////////// /* ? : B D static static static static static static , 。 static static , , 。 */ ////////////////////////////////////////////////////////////////// /* CONTAINER::iterator iter , tempIt; for (iter = cont.begin() ; iter != cont.end() ; )    { tempIt = iter; ++iter; cont.erase(tempIt);    } cont CONTAINER , , CONTAINER : 1、vector 2、list 3、map 4、deque CONTAINER ? : A 1,4 2,3 1,3 2,4 ( map, set, multimap,multiset), iterator, iterator , erase , iterator 。 ( vector,deque), iterator iterator 。 vetor,deque , 。 erase iterator list , erase iterator。 */ ////////////////////////////////////////////////////////////////// /* ?: :: . .* */ ////////////////////////////////////////////////////////////////// /* : D : D ( ) 。 。 c++ , 、 、 、 4 。 , 。 , */ ////////////////////////////////////////////////////////////////// /* int func(int a) { int b; switch (a) { case 1: b = 30; case 2: b = 20; case 3: b = 16; default: b = 0; } return b; } func(1) = ? : D 30 20 16 0 break , , ,b 。 b=0 */ ////////////////////////////////////////////////////////////////// /* , , ( )。 : C */ ////////////////////////////////////////////////////////////////////// /* C++ ? : C : B ( ) 1. ; 2. , , 1. ; 2. , , ; 3. , , : “ (stack unwinding)”【 】 , ,C++ , C++ , , terminate() 。 , , , , */ //////////////////////////////////////////////////////// /* 、 ? : C D , , , , virtual , , " " , , virtual " " a. : (1) ( ); (2) ; (3) ; (4)virtual 。 b. , : (1) ( ); (2) ; (3) ; (4) virtual 。 c.“ ” , : (1) , 。 , virtual , ( )。 (2) , , virtual 。 , ( ) */ //////////////////////////////////////////////////////////////// /* A B , C A , () : B D B A C B C B B A (1) 。 (2) , 。 B A , A B , 。 (3) 。 B A , C B , C A , 。 , , 。 , : 。 , : , */ /////////////////////////////////////////////////////// /* #include<stdio.h> int main( ) { unsigned int a = 6; int b = -20; (a + b > 6) ? printf(">6") : printf("<=6"); return 0; } >6 , 。 。 int unsigned int 4 , int , , unsigned int , b unsigned int 4294967276, a+b>6 */