我有一个函数MakeElementfromString( string k ){},我想拆分字符串,并用它生成struct element{int nr, string s}。我能用什么来做到这一点呢?找到了strtok,但无法使用它,或者我不知道如何使用它,因为它是针对char和一些stringstream方法的。对我来说没什么用谁能告诉我一个主意吗?我不是c++专家,所以请解释一下:)谢谢
发布于 2011-05-11 02:51:38
感谢你的帮助
struct Telem {
int nrte;
string s;
int dims;
};我需要从文件中读取一行,并将该行转换为我希望的元素,希望它能帮助其他人解决类似的问题
Telem TelemDinString( string k )
{
Telem a;
Init(a);
string buf;
stringstream ss(k);
vector<string> tokens;
while ( ss >> buf )
tokens.push_back(buf);
int nr;
stringstream convert( tokens[0] );
if ( !( convert >> nr ) )
nr=-1;
a.nrte = nr;
a.s = tokens[1];
a.dims=a.s.length();
return a;
}https://stackoverflow.com/questions/5936631
复制相似问题