Skip to content

Instantly share code, notes, and snippets.

@afunTW
Created May 2, 2016 06:42
Show Gist options
  • Save afunTW/1cd1808fb6a9f7ee31a34fd008dc998f to your computer and use it in GitHub Desktop.
Save afunTW/1cd1808fb6a9f7ee31a34fd008dc998f to your computer and use it in GitHub Desktop.
A small transform function from vector<string> to vector<vector<int>>
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
using namespace std;
vector< vector<int> > transform2IntVecVec (vector<string> vs){
vector< vector<int> > vvI;
// read the line by line
for (int i = 0 ; i < vs.size() ; ++i){
vector<int> tmp;
for (int j = 0 ; j <vs[i].length() ; ++j){
int deli = vs[i].find(",", j);
if (deli<0) {
break;
}
tmp.push_back(atoi(vs[i].substr(j, deli-j).c_str()));
j = deli;
}
vvI.push_back(tmp);
}
return vvI;
}
int main(){
vector<string> vs;
vector< vector<int> > vvI;
vs.push_back("9,13,10,11,14,7,10,7,12,15,7,11");
vvI = transform2IntVecVec(vs);
// for (std::vector<string>::iterator i = vs.begin(); i!=vs.end() ; ++i){
// cout<<*i<<endl;
// }
for (int i = 0 ; i<vvI.size() ; ++i){
for (int j = 0 ; j<vvI[i].size() ; ++j){
cout<<vvI[i][j]<<endl;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment