輸入字符串,分解字符串數組

#include <iostream>
#include<vector>
#include<string>
#include<cstring>
using namespace std;

int main()
{
   vector<string> str;
   char str1[200];
   char *result = NULL;
   cin.getline(str1,200);
   result = strtok(str1," ");
   while(result)
   {
        str.push_back((string)result);
        result = strtok(NULL," ");
   }
   for(int i=0;i<str.size();i++)
    cout<<str[i]<<" ";
   return 0;
}

輸入
the ship is sinking
輸出
the ship is sinking(字符數組)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章