[問題] 一個句子(含space)的字串毒入函數[cin與getline]

看板C_and_CPP (C/C++)作者 (我們都是堅強的台灣人)時間16年前 (2009/09/28 11:49), 編輯推噓1(101)
留言2則, 2人參與, 最新討論串1/1
若我要輸入"CS101 Introduction to C++ Programming," 用cin>>string1; //只能讀到CS101 用getline(cin,string1); //可以讀完整個句子 C++螞蟻本 5E 在p79第一行有講到The rest of the course name would have to be read by subsequent input operations. 我搞不是很懂,是說第一個cin讀到CS101,第二個cin可讀到Introduction, 第三個cin讀to,... 是這樣嗎? 也就是說我想要用cin讀完整個"CS101 Introduction to C++ Programming," code要使用很多cin cin>>s1; cin>>s2; ... 然後,再把每個小的字串給strcat起來嗎???? thx -- -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.136.230.97

09/28 12:00, , 1F
因為 cin 預設會以空白來切割字串 你可以用個迴圈來讀
09/28 12:00, 1F

09/28 19:18, , 2F
或是用 std::copy
09/28 19:18, 2F
文章代碼(AID): #1Am38sb7 (C_and_CPP)
文章代碼(AID): #1Am38sb7 (C_and_CPP)