123
{4 <1,-1,-1> <1,1,-1> <-1,1,-1> <-1,-1,-1>} {4 <1,3,-1> <1,1,-1> <1,1,0.5> <1,3,0.5>} {4 <1,3,-1> <-1,3,-1> <-1,1,-1> <1,1,-1>}
12345678910111213141516171819202122232425
#include <iostream> #include <fstream> using namespace std; int main () { ifstream iss("a.txt"); char c[256]; unsigned int curLine = 0; int offset; string line; iss.seekg(0,iss.cur); iss.ignore(256,'<'); iss.get(c,256,'>'); char* search = c; while(iss.good()) { getline(iss,line); curLine++; if ((offset = line.find(search, 0)) != string::npos) { cout << "found: " << search << " line: " << curLine << endl; } } iss.close(); return 0; }
found: 1,-1,-1 line: 1 found: 1,-1,-1 line: 9 found: 1,-1,-1 line: 10
12345678910111213141516171819202122232425262728293031
#include <iostream> #include <fstream> using namespace std; int main () { ifstream iss("a.txt"); char c[256], c1[256]; //add c1[256] unsigned int curLine = 0; int offset; string line; iss.seekg(0,iss.cur); iss.ignore(256,'<'); iss.get(c,256,'>'); iss.seekg(0,iss.cur); // newly add iss.ignore(256,'<'); //newly add iss.get(c1,256,'>'); //newly add char* search = c1; while(iss.good()) { getline(iss,line); curLine++; if ((offset = line.find(search, 0)) != string::npos) { cout << "found: " << search << " line: " << curLine << endl; } } iss.close(); return 0; }
123456789
found: 1,1,-1 line: 1 found: 1,1,-1 line: 2 found: 1,1,-1 line: 3 found: 1,1,-1 line: 4 found: 1,1,-1 line: 10 found: 1,1,-1 line: 13 found: 1,1,-1 line: 16 found: 1,1,-1 line: 19
123456789101112131415161718192021222324252627282930
#include <iostream> #include <vector> #include <string> using namespace std; using vecstr = vector<string>; vecstr splitOutChevrons(const string &line) { size_t pos = 0; vecstr result; while ( (pos=line.find('<',pos)) != string::npos ) { size_t endpos = line.find('>',pos); if ( endpos != string::npos ) { string token = line.substr(pos+1,endpos-pos-1); result.push_back(token); } else { // broken line, > missing } pos = endpos; } return result; } int main ( ) { string test = "4 <1,3,-1> <-1,3,-1> <-1,1,-1> <1,1,-1>}"; vecstr result = splitOutChevrons(test); for ( auto s : result ) { cout << "Token===" << s << "===" << endl; } }
$ g++ -std=c++11 foo.cpp $ ./a.out Token===1,3,-1=== Token===-1,3,-1=== Token===-1,1,-1=== Token===1,1,-1===
while ( getline(iss,line) ) { result = splitOutChevrons(line); }