下面的代码使用strtok()
分割一个字符串转换成令牌和在载体中存储该令牌。
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
char one_line_string[] = "hello hi how are you nice weather we are having ok then bye";
char seps[] = " ,\t\n";
char *token;
int main()
{
vector<string> vec_String_Lines;
token = strtok(one_line_string, seps);
cout << "Extracting and storing data in a vector..\n\n\n";
while(token != NULL)
{
vec_String_Lines.push_back(token);
token = strtok(NULL, seps);
}
cout << "Displaying end result in vector line storage..\n\n";
for (int i = 0; i < vec_String_Lines.size(); ++i)
cout << vec_String_Lines[i] << "\n";
cout << "\n\n\n";
return 0;
}
你能否提供一个链接来讨论为什么使用'using namespace x'是不好的做法? – jli
@jli:添加了链接到我的答案。看见。 – Nawaz
@Nawaz谢谢,看看我的其他问题,我正在使用的语法以及我从uni的教师那里学习C++的方式非常可疑:S !!!!! –