strtok函数用法

 1 #include <iostream>
 2 #include <cstring>
 3 #include <string>
 4 #include <vector>
 5 
 6 using namespace std;
 7 
 8 int main()
 9 {
10     vector<int> v;
11     string s;
12     getline(cin,s);    // 读取一行
13     char *p = nullptr;    // 存储分割完一次分出的字符数组
14     char *ss = s.data();   // const_cast<char *>(s.c_str() ) 的写法会让原string中的字符也改变
15     p = strtok(ss," ");
16     while(p)
17     {
18         cout << p << "----";
19         v.push_back(atoi(p) );
20         p = strtok(NULL," ");
21     }
22     cout << endl << "s:" << s << endl << "ss:" << ss << endl;
23     for(auto i:v) cout << i << " ";
24     cout << endl;
25     return 0;
26 }

 

posted @ 2020-06-07 18:04  黎曼猜想  阅读(363)  评论(0)    收藏  举报