Skip to content

Instantly share code, notes, and snippets.

@liuqinh2s
Created December 27, 2016 02:12
Show Gist options
  • Save liuqinh2s/bd4afbf9eb6704a06d626b22546f03e8 to your computer and use it in GitHub Desktop.
Save liuqinh2s/bd4afbf9eb6704a06d626b22546f03e8 to your computer and use it in GitHub Desktop.
数组的转换接口,比如我们经常要用到[[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]],怎么把这一串字符串,传值给一个vector<pair<int, int>>& people;呢。这或许要用到字符串解析的代码,解析出来后还要一个一个传值,做轮子,不做白手起家的傻逼。
void form_fun(string s, vector<pair<int, int>>& people){
if(s.at(0)=='[' && s.at(1)=='[' && s.at(s.size()-2)==']' && s.at(s.size()-1)==']'){
pair<int,int> p;
for(int i=0;i<s.size();i++){
while(i<s.size() && (s.at(i)>'9' || s.at(i)<'0'))
i++;
if(i<s.size() && s.at(i)<='9' && s.at(i)>='0'){
string buffer="";
buffer += s.at(i);
while(i<s.size() && s.at(i+1)<='9' && s.at(i+1)>='0'){
buffer += s.at(i+1);
i++;
}
stringstream ss ;
ss << buffer;
ss >> p.first;
i++;
}
while(i<s.size() && (s.at(i)>'9' || s.at(i)<'0'))
i++;
if(i<s.size() && (s.at(i)<='9' && s.at(i)>='0')){
string buffer="";
buffer += s.at(i);
while(i<s.size() && s.at(i+1)<='9' && s.at(i+1)>='0'){
buffer += s.at(i+1);
i++;
}
stringstream ss ;
ss << buffer;
ss >> p.second;
}
if(i<s.size())
people.push_back(p);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment