#
char *szSubFrame[64] ={0};
char *word;
int i, numString = 0;
char *sep = "$\0\r\n";
for(i=0, word = strtok(szBuff, sep);
word != NULL;
word = strtok(NULL, sep), i++)
{
szSubFrame[i] = new char[strlen(word)+1];
memcpy(szSubFrame[i], word, strlen(word));
numString++;
}
對于上述的代碼,如何用delete刪除動態建立的數組
for(i=0; i<numString; i++)
{
delete []szSubFrame[i];
}
這個可以嗎?
#include <string.h>
#include <stdio.h>
int main(void)
{
char input[16] = "abc,dhh,eee";
char *p;
/* strtok places a NULL terminator
in front of the token, if found */
p = strtok(input, ",");
if (p) printf("%s\n", p);
/* A second call to strtok using a NULL
as the first parameter returns a pointer
to the character following the token */
p = strtok(NULL, ",");
if (p) printf("%s\n", p);
p = strtok(NULL, ",");
if (p) printf("%s\n", p);
return 0;
}
MSDN上的原話:
On the first call to strtok, the function skips leading delimiters and returns a pointer to the first token in strToken, terminating the token with a null character. More tokens can be broken out of the remainder of strToken by a series of calls to strtok. Each call to strtok modifies strToken by inserting a null character after the token returned by that call. To read the next token from strToken, call strtok with a NULL value for the strToken argument. The NULL strToken argument causes strtok to search for the next token in the modified strToken. The strDelimit argument can take any value from one call to the next so that the set of delimiters may vary.
第二次參數竟然可以NULL
是因為strtok中用static指針記住了上次處理后的位置
我想是因為這個函數內部實現時,用到了靜態變量,而要不要修改這個變量,就是要根據第一個參數來確定!
當為NULL時,就不再修改Static變量的值了!
這個靜態變量的作用,就是記錄原始字符串的長度的!
new和delete運算符用于動態分配和撤銷內存的運算符
new用法:
1. 開辟單變量地址空間
1)new int; //開辟一個存放數組的存儲空間,返回一個指向該存儲空間的地址.int *a = new int 即為將一個int類型的地址賦值給整型指針a.
2)int *a = new int(5) 作用同上,但是同時將整數賦值為5
2. 開辟數組空間
一維: int *a = new int[100];開辟一個大小為100的整型數組空間
二維: int **a = new int[5][6]
三維及其以上:依此類推.
一般用法: new 類型 [初值]
delete用法:
1. int *a = new int;
delete a; //釋放單個int的空間
2.int *a = new int[5];
delete [] a; //釋放int數組空間
int *p1 = (int *)malloc(sizeof(int) * length);
int *p2 = new int[length];
運算符new比malloc要簡單多了,這是因為new內置了sizeof、類型轉換和類型安全檢查功能。對于非內部數據類型的對象而言,new在創建動態對象的同時完成了初始化工作。如果對象有多個構造函數,那么new的語句也可以有多種形式。例如
class Obj
{
public :
Obj(void); // 無參數的構造函數
Obj(int x); // 帶一個參數的構造函數
…
}
void Test(void)
{
Obj *a = new Obj;
Obj *b = new Obj(1); // 初值為1
…
delete a;
delete b;
}
如果用new創建對象數組,那么只能使用對象的無參數構造函數。例如
Obj *objects = new Obj[100]; // 創建100個動態對象
不能寫成
Obj *objects = new Obj[100](1);// 創建100個動態對象的同時賦初值1
在用delete釋放對象數組時,留意不要丟了符號‘[]’。例如
delete []objects; // 正確的用法
delete objects; // 錯誤的用法
后者相當于delete objects[0],漏掉了另外99個對象。
malloc 和free只是分配了空間.
而new和delete不僅分配了空間,還完成了對構造函數和析購函數的調用,當然這是對對象而言。
兩者在對基本數據類型操作時候,沒有多大區別,區別在于對對象進行操作時。malloc和free是函數,但new和delete是操作符
#include <iostream>
using namespace std;
void display_bits(unsigned u)
{
int t;
for(t = 128; t >= 1; t /= 2)\
{
if(u&t) cout<<"1 ";
else cout<<"0 ";
}
cout<<endl;
}
int main()
{
display_bits(10);
return 0;
}
#include <iostream>
using namespace std;
enum language{java, c, pascal, fortran, vc, vb, cpp} lang;
char name[][10] = {
"java",
"c",
"pascal",
"fortran",
"vc",
"vb",
"cpp"
};
int main()
{
lang = fortran;
cout << name[lang] << endl;
lang = java;
cout << name[lang] << endl;
lang = pascal;
cout << name[lang] << endl;
lang = (language) 6;//這個強轉經典
cout << name[lang] << endl;
return 0;
}
#include "iostream.h"
#include "iomanip.h"
int n;
int main(int argc, char* argv[])
{
void stu_average(int (*p)[6]);
void subject_average(int (*p)[6]);
int a[50][6];
int i,j;
int (*p)[6]=a;
cin>>n;
for (i=0;i<n;i++)
for (j=0;j<6;j++)
cin>>*(*(p+i)+j);
stu_average(a);
subject_average(a);
return 0;
}
void stu_average(int (*p)[6])
{
int sum=0;
int i,j;
for (i=0;i<n;i++)
{
cout<<*(*(p+i)+0)<<" ";
for (sum=0,j=1;j<6;j++)
{
sum+=*(*(p+i)+j);
cout<<setiosflags(ios::fixed)<<setprecision(1);
cout<<*(*(p+i)+j)<<" ";
}
cout<<sum/5.0<<endl;
}
}
void subject_average(int (*p)[6])
{
int i,j;
int sum=0;
for (j=1;j<6;j++)
{
for (i=0,sum=0;i<n;i++)
{
sum+=*(*(p+i)+j);
}
cout<<setiosflags(ios::fixed)<<setprecision(1);
cout<<(float)sum/n<<" ";
}
cout<<endl;
}
誰能給我講一下里面的指針哦?????
主要是對cin,cout之類的一些操縱運算子,比如setfill,setw,setbase,setprecision等等。它是I/O流控制頭文 件,就像C里面的格式化輸出一樣.以下是一些常見的控制函數的:
dec 置基數為10 相當于"%d"
hex 置基數為16 相當于"%X"
oct 置基數為8 相當于"%o"
setfill(c) 設填充字符為c
setprecision(n) 設顯示小數精度為n位
setw(n) 設域寬為n個字符
這個控制符的意思是保證輸出寬度為n。如:
cout<<setw(3)<<1<<setw(3)<<10<<setw(3)<<100; 輸出結果為
1 10100 (默認是右對齊)當輸出長度大于3時(<<1000),setw(3)不起作用。
setioflags(ios::fixed) 固定的浮點顯示
setioflags(ios::scientific) 指數表示
setiosflags(ios::left) 左對齊
setiosflags(ios::right) 右對齊
setiosflags(ios::skipws 忽略前導空白
setiosflags(ios::uppercase) 16進制數大寫輸出
setiosflags(ios::lowercase) 16進制小寫輸出
setiosflags(ios::showpoint) 強制顯示小數點
setiosflags(ios::showpos) 強制顯示符號
舉例:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout<<12345.0<<endl;//輸出"12345"
cout<<setiosflags(ios::fixed)<<setprecision(3)<<1.2345<<endl;輸出"1.235"
cout<<setiosflags(ios::scientific)<<12345.0<<endl;//輸出"1.234500e+004 "
cout<<setprecision(3)<<12345.0<<endl;//輸出"1.235e+004 "
return 0;
}
佛山和廣州之行讓我大開眼界:
原來愛可以那么簡單,那么開放!
原來這個世界上什么人都有,什么事都有!
.......
原來我很蠢?。。?!
將來步入社會不被淘汰才怪?。。?!
關于怎樣才可以實現自己心目中的夢想:
1、進國內有名大公司
2、全額獎學金+公費留學外國名校
想來想去,覺得還是不要好高騖遠,急功近利(這幾年我失敗的原因就在于此),所以,還是一個月一個月的安排好自己的時間最好,不然最后恐怕是“心比天高,命比紙薄”,到頭來還不是竹籃打水一場空!
這個月我的計劃是:
1、學習:每一科都要趕上去,現在看來只有用晚上的時間啦,因為白天不是上課就得來實驗室,以后要經常來實驗室多學習,多向師兄師姐和韓老師請教,多學點東西。期末我要考全年級第一?。。。?br>馬拉戈壁,我就不信我考不到!??!你看看你的那些同學,個個比你牛多了?。。。?br>2、弄ACM:革命尚未成功,同志仍需努力。。。比起別人來,真的差距好大?。。。?!都怪自己大一一年的時間荒廢了這么多,現在要趕上別人,當然困難啦?。。?!可有什么辦法,這年頭,只有玩命的弄才有希望??!
3、弄MCM:沒辦法啊,為了那點學分??!
4、學精C++:做項目的需要啊!要多和夏師兄溝通一下,把ACE和OGRE弄通,把項目平臺搭建起來!
5、情商:和人交往溝通的能力,作為領導的能力
6、說話:口才啊
7、做事
8、心理素質:自信、幽默、開心
這幾天學校因為鬧甲流鬧得很厲害,恰巧有人迫切想我去找她,就去了佛山和廣州,見到和經歷了太多的人和事。覺得自己很不爭氣,哥為了我和這個家拼命的付出,而我呢,不思進取,自甘墮落,貪玩好耍,虧我還有那么多好心人一直這樣關心我,呵護我!我怎么對得起他們?。?!我他媽的簡直連豬狗都不如!?。『喼本褪侨嗽械娜嗽。。。?br>
思來想去,我決定,一切重新開始。。。。。。