0

スペースで区切られた整数入力から連結リストを作成しようとしています。

入力:

  1. ノード数
  2. スペース区切り入力

int main()
{
    int n;
    cout<<"Enter number of nodes";
    cin>>n;
    cout<<"\nEnter data"<<endl;
    int temp;
    lNode *head = NULL;
    while(cin>>temp)
    {
        CreateLinkedList(&head,temp);
    }
    PrintLinkedList(head);

    return 0;
}

ここでは、ユーザー入力を最初の入力として指定したノード数に制限する方法がわかりません。ユーザー入力を取得する他の方法はありますか?

4

1 に答える 1

1

入力を文字列として尋ねることができます:

string line;
getline(cin, line);

次に、stringstream を使用して行に入力した数値を区切ることができるため、sstream ライブラリを含める必要があります (例#include <sstream>: )。

stringstream ss(line);
int number;

while(ss >> number) {
    ... do whatever you want to do with the number here ...
}
于 2012-11-22T18:54:31.403 に答える