假如我是池中的一朵荷花作文
以下三條輸出語(yǔ)句分別輸出什么?【基礎】

char str1[] = “abc”;
char str2[] = “abc”;
const char str3[] = “abc”;
const char str4[] = “abc”;
const char* str5 = “abc”;
const char* str6 = “abc”;
cout << boolalpha << (str1==str2) << endl; /pic/p>
cout << boolalpha << (str3==str4) << endl; /pic/p>
cout << boolalpha << (str5==str6) << endl; /pic/p>
答:輸出為:false、false、true。
以下反向遍歷array 數組的方法有什么錯誤?【基礎】
vector array;
array.push_back(1);
array.push_back(2);
array.push_back(3);
/pic/p>
for(vector::size_type i=array.size()-1; i>=0; –i){
cout << array[i] << endl;
}
答:for 循環(huán)中的變量i 的類(lèi)型不應定義為vector::size_type,
因為該類(lèi)型為無(wú)符號數值類(lèi)型,故循環(huán)條件將恒成立,為死循環(huán),應將其類(lèi)型定
義為有符號的int 類(lèi)型。
以下代碼有什么問(wèn)題?【基礎】
cout << (true ? 1 : “1″) << endl;
答:運算符中兩個(gè)可選值的類(lèi)型不同。
以下代碼有什么問(wèn)題?【基礎】
typedef vector IntArray;
IntArray array;
array.push_back(1);
array.push_back(2);
array.push_back(2);
array.push_back(3);
/pic/p>
for(IntArray::iterator itor=array.begin(); itor!=array.end();
++itor){
if(2==*itor) {
array.erase(itor);
}
}
答:for 循環(huán)中的if 語(yǔ)句后的array.erase(itor)語(yǔ)句,它將迭代器itor 所指
向的元素刪除后會(huì )自動(dòng)下移一位,故應在其后加上語(yǔ)句:itor–;
以下代碼中的兩個(gè)sizeof 用法有問(wèn)題嗎?【基礎】
void upperCase(char str[]){ /pic/p>
for(int i=0; i if(‘a’<=str[i] && str[i]<=’z')
str[i] -= (‘a’-'A’);
}
}
int main(){
char str[] = “aBcDe”;
cout << “str 字符串長(cháng)度為:” << sizeof(str)/sizeof(str[0]);
cout << endl;
upperCase(str);
cout << str << endl;
return 0;
}
答:在upperCase 方法中,for 循環(huán)的sizeof(str)的值將總是4,所以該方法
只能將參數中的字符串的前四個(gè)字符轉換成大寫(xiě)字母。
以下代碼能夠編譯通過(guò)嗎?為什么?【基礎】
unsigned int const size1 = 2;
char str1[size1];
unsigned int temp = 0;
cin >> temp;
unsigned int const size2 = temp;
char str2[size2];
答:能;
以下代碼有什么問(wèn)題?【基礎】
struct Test{
Test(int){}
Test(){}
void fun(){}
};
void main(void){
Test a(1);
a.fun();
Test b();
b.fun();
}
答:main 函數的返回類(lèi)型應為int;不能對b 調用fun()方法。
以下代碼中的輸出語(yǔ)句輸出0 嗎?為什么?【基礎】
struct CLS{
int m_i;
CLS(int i):m_i(i){ }
CLS(){ CLS(0);}
};
int main(){
CLS obj;
cout <
}
答:輸出不是0;
C++中的空類(lèi),默認產(chǎn)生哪些類(lèi)成員函數?【基礎】
答:空類(lèi)中默認包含的成員函數如下:
class Empty{
public:
Empty(); /pic/p>
Empty( const Empty& ); /pic/p>
~Empty(); /pic/p>
Empty& operator=( const Empty& ); /pic/p>
Empty* operator&(); /pic/p>
const Empty* operator&() const; /pic/p>
};
統計一篇文章中單詞個(gè)數!净A】
答:代碼如下:
include
#include
using namespace std;
int main(){
ifstream fin(“t.txt”);
if(!fin){
cout<<”can’t open file”< return -1;
}
int count = 0;
char buf[256];
memset(buf, 0, 256);
while(1){
fin2>>buf;
if(fin2.eof())
break;
count++;
}
cout<<”The number of the words is : “< fin2.close();
return 0;
}
寫(xiě)一個(gè)函數,完成內存之間的拷貝!局械入y度】
答:代碼如下:
void* mymemcpy(void* dest, const void* src, size_t count){
char* pdest = static_cast(dest);
const char* psrc = static_cast(src);
if(pdest>psrc && pdest for(size_t i=count-1; i!=-1; –i){
pdest[i] = psrc[i];
}
}else{
for(size_t i=0; i pdest[i] = psrc[i];
}
}
return dest;
}
int main(){
char str[] = “0123456789″;
mymemcpy(str+1, str+0, 9);
cout << str << endl; /pic/p>
return 0;
}
非C++內建類(lèi)型A 和B,在哪幾種情況下B 能隱式轉化為A?【較難】
答:a)class B : public A{……}/pic/p>
b)class B{operator A();}/pic/p>
c)class A{ A(const B&);}/pic/p>
(可以有其他帶帶默認值的參數)
d)A& operator= (const A&);/pic/p>
但也可以勉強算一個(gè)
以下代碼有什么問(wèn)題?【較難】
void char2Hex(char c){ /pic/p>
char ch = c/0×10 + ’0′;
if(ch>’9′) ch += (‘A’-’9′-1);
char cl = c%0×10 + ’0′;
if(cl>’9′) cl += (‘A’-’9′-1);
cout << ch << cl << ‘ ‘;
}
int main(){
char str[] = “I love 中國”;
for(size_t i=0; i char2Hex(str[i]);
cout << endl;
return 0;
}
答:
以下兩條輸出語(yǔ)句分別輸出什么?【較難】
float a = 1.0f;
cout << (int)a << endl;
cout << (int&)a << endl;
cout << boolalpha << ((int)a==(int&)a) << endl; /pic/p>
float b = 0.0f;
cout << (int)b << endl;
cout << (int&)b << endl;
cout << boolalpha << ((int)b==(int&)b) << endl;/pic/p>
答:第一處輸出false,第二處輸出true。
【假如我是池中的一朵荷花作文】相關(guān)文章:
假如我是一朵荷花作文四篇12-11
一朵荷花作文11-27
假如我是一朵菊花作文08-06
假如我是一朵小花作文03-19
作文假如我是一朵云03-25
假如我是一朵云05-05
假如我是荷花作文(精選20篇)02-25
假如我是一朵綠色的云06-09
假如我是一朵花兒作文03-24
- 相關(guān)推薦