[英]About array in stringstream in C++
我想設計一個程序,輸入帶空格的字符串和 output 拆分字符串和字母數,但我不知道“while (.ss.eof()){}”中的“word[]”是什么意思, 比如我輸入“Programming is fun”,結果是
Programming
is
fun
Length:18
當我將“word[i]”更改為 word[0] 結果是
fun
Lenght:18
詞[1]是
fun
Lenght:18
有人可以解釋一下嗎?
這是我的代碼:
#include<iostream>
#include<sstream>
using namespace std;
void printStringArray(string word[], int size)
{
for (int i = 0;i < size;i++)
{
cout << word[i] << endl;
}
}
int main()
{
string word[10];
string str;
getline(cin, str);
stringstream ss(str);
int i = 0;
while (!ss.eof())
{
ss >> word[i];//P
i++;
}
printStringArray(word, 3);
cout << "Lenght:" << str.size();
return 0;
}
這個循環:
while (!ss.eof())
{
ss >> word[0];
i++;
}
將數組中的第一項設置為 3 個不同的值,在每次迭代中,該值都會被一個新值替換,因此您將在數組的第一個 position 中留下最后一個單詞,其他條目未設置(空)。
當您將word[0]
更改為word[1]
時,除了陣列的第二個 position 之外,會發生同樣的事情。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.