[問題] 關於跑while 莫名奇妙跳出去的問題

看板C_and_CPP (C/C++)作者 (天堂來的憨子)時間16年前 (2009/05/23 12:40), 編輯推噓1(103)
留言4則, 2人參與, 最新討論串1/1
#include<iostream> #include<cstdlib> #include<vector> #include<fstream> #include<string> using namespace std; int main() { vector <string> temp_store; string temp_area; char *test; ifstream iFile; iFile.open ( "new.txt" ); getline (iFile ,temp_area ); test = strtok ( (char*)temp_area.c_str(), "< >" ); temp_store.push_back(test); while ( test != NULL ) { test = strtok (NULL, "< >"); temp_store.push_back(test); } cout <<temp_area <<endl; cout << temp_store[0]<<endl; cout << temp_store[1]<<endl; cout << temp_store[3]<<endl; cout << temp_store[4]<<endl; cout << temp_store[5]<<endl; system("pause"); return 0; } new.txt的內容 是類似<<12>> <<13>> <<14>> 這種東西一行 但是在while 把最後一個token(14) push_back到vector之後 while 就直接跳出去了...連下面的cout 都沒執行 想請問高手們 這是什麼狀況呢~"~ -- 龍五只要手上有槍 誰也殺不了他!! http://www.wretch.cc/album/adwind -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.128.41.103 ※ 編輯: adwind 來自: 140.128.41.103 (05/23 13:04)

05/23 13:14, , 1F
猜測:你把 NULL push 進 vector
05/23 13:14, 1F

05/23 13:15, , 2F
用 NULL 初始化 std::string 會是未定義行為
05/23 13:15, 2F

05/23 13:15, , 3F
while 的順序要改一下
05/23 13:15, 3F

05/26 21:39, , 4F
解決了 感謝指點^^
05/26 21:39, 4F
文章代碼(AID): #1A5tv41r (C_and_CPP)
文章代碼(AID): #1A5tv41r (C_and_CPP)